|
1 | | -# Lazy loading localization |
| 1 | +# Lazy loading |
2 | 2 |
|
3 | | -TODO: |
| 3 | +Loading all of your localization files at once is overkill and unnecessary. |
| 4 | + |
| 5 | +Lazy loading or asynchronously loading the localization files is really easy when using bundler. |
| 6 | + |
| 7 | +Let´s assume we have a project directory similar to the one below: |
| 8 | + |
| 9 | +``` |
| 10 | +├── dist |
| 11 | +├── index.html |
| 12 | +├── package.json |
| 13 | +├── src |
| 14 | +│ ├── App.vue |
| 15 | +│ ├── components |
| 16 | +│ ├── i18n.js |
| 17 | +│ ├── index.css |
| 18 | +│ ├── locales |
| 19 | +│ │ ├── en.json |
| 20 | +│ │ └── ja.json |
| 21 | +│ ├── main.js |
| 22 | +│ ├── pages |
| 23 | +│ │ ├── About.vue |
| 24 | +│ │ └── Home.vue |
| 25 | +│ └── router.js |
| 26 | +``` |
| 27 | + |
| 28 | +The `pages` folder is where our arbitrary Vue component files like the `About.vue`, router inits, i18n inits and other reside. The `locales` folder is where all of our localization files reside, and In `i18n.js`, the functions for i18n-related process are defined as follows: |
| 29 | + |
| 30 | +```js |
| 31 | +import { createI18n } from 'vue-i18n' |
| 32 | + |
| 33 | +export function setupI18n(options = { locale: 'en' }) { |
| 34 | + const i18n = createI18n(options) |
| 35 | + setI18nLanguage(i18n, options.locale) |
| 36 | + return i18n |
| 37 | +} |
| 38 | + |
| 39 | +export function setI18nLanguage(i18n, locale) { |
| 40 | + if (i18n.mode === 'legacy') { |
| 41 | + i18n.global.locale = locale |
| 42 | + } else { |
| 43 | + i18n.global.locale.value = locale |
| 44 | + } |
| 45 | + /** |
| 46 | + * NOTE: |
| 47 | + * If you need to specify the language setting for headers, such as the `fetch` API, set it here. |
| 48 | + * The following is an example for axios. |
| 49 | + * |
| 50 | + * axios.defaults.headers.common['Accept-Language'] = locale |
| 51 | + */ |
| 52 | + document.querySelector('html').setAttribute('lang', locale) |
| 53 | +} |
| 54 | + |
| 55 | +export async function loadLocaleMessages(i18n, locale) { |
| 56 | + // load locale messages |
| 57 | + if (!i18n.global.availableLocales.includes(locale)) { |
| 58 | + const messages = await import( |
| 59 | + /* webpackChunkName: "locale-[request]" */ `./locales/${locale}.json` |
| 60 | + ) |
| 61 | + i18n.global.setLocaleMessage(locale, messages.default) |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +The following three functions are exported: |
| 67 | + |
| 68 | +- `setupI18n` |
| 69 | +- `setI18nLanguage` |
| 70 | +- `loadLocaleMessages` |
| 71 | + |
| 72 | +The `setupI18n` function takes the same options as `createI18n`, creates an instance of i18n with those options, executes the `setI18nLanguage` function, and returns the i18n instance. |
| 73 | + |
| 74 | +The `setI18nLanguage` function sets the language by setting the locale of the parameter `i18n` to the value of the parameter `locale`. Besides, this function has the utility of setting the `lang` attribute of the HTML doocument to the value of the parameter `locale`. As noted in the comments, like the HTTP client, you can also set the language |
| 75 | + |
| 76 | +The `loadLocaleMessages` function is what we will actually use to change the languages. Loading the new files is done via the `import` function, which is generously provided by webpack and it allows us to load files dynamically, and because it uses promises we can easily wait for the loading to finish. |
| 77 | + |
| 78 | +You can learn more about the import function in the [Webpack documentation](https://webpack.js.org/guides/code-splitting/#dynamic-imports). |
| 79 | + |
| 80 | +Using the `loadLocaleMessages` function is straightforward. A common use case is inside a vue-router beforeEach hook. |
| 81 | + |
| 82 | +Here the code for the vue-router beforeEach hook part of `router.js`: |
| 83 | + |
| 84 | +```js |
| 85 | + // navigation guards |
| 86 | + router.beforeEach((to, from, next) => { |
| 87 | + const locale = to.params.locale |
| 88 | + |
| 89 | + // check locale |
| 90 | + if (!SUPPORT_LOCALES.includes(locale)) { |
| 91 | + return false |
| 92 | + } |
| 93 | + |
| 94 | + // load locale messages |
| 95 | + loadLocaleMessages(i18n, locale) |
| 96 | + |
| 97 | + // set i18n language |
| 98 | + setI18nLanguage(i18n, locale) |
| 99 | + |
| 100 | + return next() |
| 101 | + }) |
| 102 | +``` |
0 commit comments