Skip to content

Commit

Permalink
[react-intl] Store current language in cookie (#638)
Browse files Browse the repository at this point in the history
Store currently selected locale in cookie after successfull
language change by redux action. This makes selection persistent.
  • Loading branch information
langpavel authored and koistya committed May 4, 2016
1 parent 68e31a6 commit a8fb68a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/actions/intl.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export function setLocale({ locale }) {
messages,
},
});

// remember locale for every new request
if (process.env.BROWSER) {
const maxAge = 3650 * 24 * 3600; // 10 years in seconds
document.cookie = `lang=${locale};path=/;max-age=${maxAge}`;
}
} catch (error) {
dispatch({
type: SET_LOCALE_ERROR,
Expand Down
2 changes: 1 addition & 1 deletion src/components/LanguageSwitcher/LanguageSwitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function LanguageSwitcher({ currentLocale, availableLocales, setLocale }) {
<span>{locale}</span>
) : (
<a
href={`?locale=${locale}`}
href={`?lang=${locale}`}
onClick={(e) => {
setLocale({ locale });
e.preventDefault();
Expand Down
7 changes: 5 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ app.use(requestLanguage({
languages: locales,
queryName: 'lang',
cookie: {
name: 'language',
options: { maxAge: 24 * 3600 * 1000 },
name: 'lang',
options: {
path: '/',
maxAge: 3650 * 24 * 3600 * 1000, // 10 years in miliseconds
},
url: '/lang/{language}',
},
}));
Expand Down

0 comments on commit a8fb68a

Please sign in to comment.