Skip to content

Commit

Permalink
feat: pass to-be-loaded locale when lazy-loading from exported functi…
Browse files Browse the repository at this point in the history
…on (#752)

When using the function export for lazy-loaded locales, the function
will now also receive locale code, apart from context.

Resolves #742
  • Loading branch information
rchl committed Jun 2, 2020
1 parent 53888db commit 145f3b2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
12 changes: 6 additions & 6 deletions docs/es/lazy-load-translations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
12 changes: 6 additions & 6 deletions docs/lazy-load-translations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions test/fixture/basic/lang/fr-FR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default (_context, locale) => {
return {
home: 'Accueil',
about: 'À propos',
posts: 'Articles',
locale
}
}
7 changes: 7 additions & 0 deletions test/fixture/basic/pages/locale.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<template>
<div id="t">{{ $t('locale') }}</div>
</template>

<script>
export default {}
</script>
14 changes: 14 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 145f3b2

Please sign in to comment.