Skip to content

Commit

Permalink
fix: fix loading fallback locale with lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Aug 28, 2019
1 parent 634690a commit d8db5b1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ export default async (context) => {
// Lazy-loading enabled
if (lazy) {
const { loadLanguageAsync } = require('./utils')

// Load fallback locale.
if (app.i18n.fallbackLocale && newLocale !== app.i18n.fallbackLocale) {
await loadLanguageAsync(context, app.i18n.fallbackLocale)
}

await loadLanguageAsync(context, newLocale)
}

Expand Down
6 changes: 6 additions & 0 deletions test/fixture/basic/lang/en-US.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
home: 'Homepage',
about: 'About us',
posts: 'Posts',
untranslated: 'in english'
}
Empty file.
3 changes: 3 additions & 0 deletions test/fixture/basic/pages/fallback.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<h1>{{ $t('untranslated') }}</h1>
</template>
48 changes: 48 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,54 @@ describe('basic', () => {
})
})

describe('lazy loading', () => {
let nuxt

beforeAll(async () => {
const override = {
i18n: {
lazy: true,
langDir: 'lang/',
vueI18n: {
fallbackLocale: 'en'
}
}
}

const testConfig = loadConfig(__dirname, 'basic', override, { merge: true })

// Override those after merging to overwrite original values.
testConfig.i18n.locales = [
{
code: 'en',
iso: 'en-US',
name: 'English',
file: 'en-US.js'
},
{
code: 'fr',
iso: 'fr-FR',
name: 'Français',
file: 'fr-FR.js'
}
]
testConfig.i18n.vueI18n.messages = null

nuxt = (await setup(testConfig)).nuxt
})

afterAll(async () => {
await nuxt.close()
})

test('shows fallback string', async () => {
const html = await get('/fr/fallback')
const dom = getDom(html)
const title = dom.querySelector('h1')
expect(title.textContent).toBe('in english')
})
})

describe('no_prefix strategy', () => {
let nuxt

Expand Down

0 comments on commit d8db5b1

Please sign in to comment.