From 8f89121f171db636b2217b5daab4b04b9430c7f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Tue, 2 Jun 2020 22:16:32 +0200 Subject: [PATCH] feat: pass to-be-loaded locale when lazy-loading from exported function When using the function export for lazy-loaded locales, the function will now also receive locale code, apart from context. Resolves #742 --- docs/es/lazy-load-translations.md | 12 ++++++------ docs/lazy-load-translations.md | 12 ++++++------ src/templates/utils.js | 2 +- test/fixture/basic/lang/fr-FR.js | 8 ++++++++ test/fixture/basic/pages/locale.vue | 7 +++++++ test/module.test.js | 14 ++++++++++++++ 6 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 test/fixture/basic/pages/locale.vue diff --git a/docs/es/lazy-load-translations.md b/docs/es/lazy-load-translations.md index afa7ed16c..e52c39bdd 100644 --- a/docs/es/lazy-load-translations.md +++ b/docs/es/lazy-load-translations.md @@ -51,14 +51,14 @@ Ejemplo de archivo de idioma: ```js // lang/[lang].js -export default (context) => { - return new Promise(function (resolve) { - resolve({ - welcome: 'Welcome' - }) - }); +export default async (context, locale) => { + await resolve({ + welcome: 'Welcome' + }) } + // or + export default { welcome: 'Welcome' } diff --git a/docs/lazy-load-translations.md b/docs/lazy-load-translations.md index d1f2b1d98..33d1643b5 100755 --- a/docs/lazy-load-translations.md +++ b/docs/lazy-load-translations.md @@ -51,14 +51,14 @@ Language file example: ```js // lang/[lang].js -export default (context) => { - return new Promise(function (resolve) { - resolve({ - welcome: 'Welcome' - }) - }); +export default async (context, locale) => { + await resolve({ + welcome: 'Welcome' + }) } + // or + export default { welcome: 'Welcome' } diff --git a/src/templates/utils.js b/src/templates/utils.js index 1875eda1c..97cb8e021 100755 --- a/src/templates/utils.js +++ b/src/templates/utils.js @@ -26,7 +26,7 @@ export async function loadLanguageAsync (context, locale) { try { const module = await import(/* webpackChunkName: "lang-[request]" */ '~/<%= options.langDir %>' + file) const messages = module.default ? module.default : module - const result = typeof messages === 'function' ? await Promise.resolve(messages(context)) : messages + const result = typeof messages === 'function' ? await Promise.resolve(messages(context, locale)) : messages app.i18n.setLocaleMessage(locale, result) app.i18n.loadedLanguages.push(locale) } catch (error) { diff --git a/test/fixture/basic/lang/fr-FR.js b/test/fixture/basic/lang/fr-FR.js index e69de29bb..02a2bec90 100644 --- a/test/fixture/basic/lang/fr-FR.js +++ b/test/fixture/basic/lang/fr-FR.js @@ -0,0 +1,8 @@ +export default (_context, locale) => { + return { + home: 'Accueil', + about: 'À propos', + posts: 'Articles', + locale + } +} diff --git a/test/fixture/basic/pages/locale.vue b/test/fixture/basic/pages/locale.vue new file mode 100644 index 000000000..0d64dc1f2 --- /dev/null +++ b/test/fixture/basic/pages/locale.vue @@ -0,0 +1,7 @@ + + + diff --git a/test/module.test.js b/test/module.test.js index f6d2ae4e1..1dba7373f 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -567,6 +567,20 @@ describe('lazy loading', () => { const title = dom.querySelector('h1') expect(title.textContent).toBe('in english') }) + + test('loads strings from file exporting a function', async () => { + const html = await get('/fr/simple') + const dom = getDom(html) + const container = dom.querySelector('#container') + expect(container.textContent).toBe('Accueil') + }) + + test('exported function gets passed locale to load', async () => { + const html = await get('/fr/locale') + const dom = getDom(html) + const container = dom.querySelector('#t') + expect(container.textContent).toBe('fr') + }) }) describe('with empty configuration', () => {