Web Application
For the web application, i18n has two goals:
- Translate the user interface.
- Tell the API which locale the user has selected (for validations errors, etc.).
We use the nuxt/i18n module, and we configured it in the src/webapp/nuxt.config.js file.
#
Basic Usage#
Locales FolderFolder src/webapp/locales contains one JavaScript file per locale.
Each of these files exports an object with translation keys and the associated texts.
For instance:
note
📣  All files should have the same organization (same translations keys, identical sorting, etc.).
#
Browser Language DetectionThe first time the user lands on your application's root pages (/x
, but not /x/y
), it automatically detects the
browser language and sets the cookie i18n_redirected
with the corresponding locale.
If your application does not support the browser language, it uses the default locale instead.
Next time the user lands on your application, it will use cookie i18n_redirected
's value to translate the
user interface to the correct locale automatically (and redirect the user to the right path - see below).
#
RoutingThe router prefixes all routes with the locale. For instance, the login page is available on the paths /en/login
and
/fr/login
.
Wherever you need to push a route in your router, use the global method localePath({ name: 'route_name' })
.
For instance:
If you don't know the name of your route, take a look at the router file Nuxt.js generates:
The name of the route here is update-password-id-token
.
note
📣  If a route has parameters, you have to give them to the localePath
method.
#
Update LocaleThe file src/webapp/components/layouts/Header.vue provides an example of how to update the locale:
Here calling switchLocalePath
will update the current locale in the i18n
store (from nuxt-i18n),
plus the value of the cookie i18_redirected
. It will also redirect the user to the correct route.
Our custom plugin src/webapp/plugins/i18n.js hooks itself on these events for:
- Updating the HTTP header
Accept-Language
for next GraphQL requests (more on that in the next chapter). - Updating the user's locale in the database if authenticated.
note
📣  We configured nuxt-i18n to lazy load the locales files, meaning it will only load the current locale file instead of all locales files. You may have to refresh a page in development to see changes made in the corresponding locale file.