Skip to content

Commit

Permalink
feat: added support for lastLocaleField
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Sep 6, 2019
1 parent 9b4b44b commit f158402
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Redirects user with permanent `302` redirect to their detected locale if a valid

It also sets the cookie `locale` for future requests to their detected locale.

This also stores the `last_locale` for a user via `ctx.state.user.save()`.
This also stores the `last_locale` (or whatever you configure the property name to be in the config option `lastLocaleField`) for a user via `ctx.state.user.save()`.


## Options
Expand Down Expand Up @@ -122,7 +122,8 @@ const i18n = new I18N({
__h: 'th',
__mf: 'tmf'
},
register: i18n.api
register: i18n.api,
lastLocaleField: 'last_locale'
});
```

Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class I18N {
__h: 'th',
__mf: 'tmf'
},
register: i18n.api
register: i18n.api,
lastLocaleField: 'last_locale'
},
config
);
Expand Down Expand Up @@ -204,9 +205,9 @@ class I18N {
debug('set cookies for locale "%s"', locale);

// if the user is logged in and ctx.isAuthenticated() exists,
// then save it as `last_locale`
// then save it as `last_locale` (variable based off lastLocaleField)
if (isFunction(ctx.isAuthenticated) && ctx.isAuthenticated()) {
ctx.state.user.last_locale = locale;
ctx.state.user[this.config.lastLocaleField] = locale;
try {
await ctx.state.user.save();
} catch (err) {
Expand Down

0 comments on commit f158402

Please sign in to comment.