Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass to-be-loaded locale when lazy-loading from exported function #752

Merged
merged 1 commit into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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