Skip to content

Commit

Permalink
fix: lazy loading for fallback locale (#1694)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Dec 5, 2022
1 parent 563d636 commit d43ee72
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default defineNuxtConfig({
vueI18n: {
legacy: false,
locale: 'en',
fallbackLocale: 'en'
fallbackLocale: 'fr'
// messages: {
// ja: {
// hello: 'こんにちは!'
Expand Down
8 changes: 0 additions & 8 deletions src/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,10 @@ export function generateLoaderOptions(
) {
let genCode = ''
const localeInfo = options.localeInfo || []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { fallbackLocale } = (isObject(options.nuxtI18nOptions?.vueI18n) ? options.nuxtI18nOptions?.vueI18n : {}) as any
const syncLocaleFiles = new Set<LocaleInfo>()
const asyncLocaleFiles = new Set<LocaleInfo>()

if (langDir) {
if (fallbackLocale && isString(fallbackLocale)) {
const localeObject = localeInfo.find(l => l.code === fallbackLocale)
if (localeObject) {
syncLocaleFiles.add(localeObject)
}
}
for (const locale of localeInfo) {
if (!syncLocaleFiles.has(locale) && !asyncLocaleFiles.has(locale)) {
;(lazy ? asyncLocaleFiles : syncLocaleFiles).add(locale)
Expand Down
22 changes: 15 additions & 7 deletions test/__snapshots__/gen.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
exports[`basic 1`] = `
"import locale_en from \\"../locales/en.json\\" assert { type: \\"json\\" };
import locale_ja from \\"../locales/ja.json\\" assert { type: \\"json\\" };
import locale_fr from \\"../locales/fr.json\\" assert { type: \\"json\\" };
export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
export const localeMessages = {
\\"en\\": () => Promise.resolve(locale_en),
\\"ja\\": () => Promise.resolve(locale_ja),
\\"fr\\": () => Promise.resolve(locale_fr),
}
export const additionalMessages = Object({})
export const resolveNuxtI18nOptions = async (context) => {
const nuxtI18nOptions = Object({})
nuxtI18nOptions.defaultLocale = \\"en\\"
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"en\\",\\"messages\\": Object({\\"en\\":{
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{
\\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})()
},}),})
nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context)
Expand All @@ -30,20 +32,20 @@ export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
`;

exports[`lazy 1`] = `
"import locale_en from \\"../locales/en.json\\" assert { type: \\"json\\" };
export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
"export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
export const localeMessages = {
\\"en\\": () => Promise.resolve(locale_en),
\\"en\\": () => import(\\"../locales/en.json\\" /* webpackChunkName: \\"lang-en\\" */),
\\"ja\\": () => import(\\"../locales/ja.json\\" /* webpackChunkName: \\"lang-ja\\" */),
\\"fr\\": () => import(\\"../locales/fr.json\\" /* webpackChunkName: \\"lang-fr\\" */),
}
export const additionalMessages = Object({})
export const resolveNuxtI18nOptions = async (context) => {
const nuxtI18nOptions = Object({})
nuxtI18nOptions.defaultLocale = \\"en\\"
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"en\\",\\"messages\\": Object({\\"en\\":{
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{
\\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})()
},}),})
nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context)
Expand All @@ -63,14 +65,16 @@ export const additionalMessages = Object({})
export const resolveNuxtI18nOptions = async (context) => {
const nuxtI18nOptions = Object({})
nuxtI18nOptions.defaultLocale = \\"en\\"
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"en\\",\\"messages\\": Object({\\"en\\":{
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{
\\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})()
},}),})
nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context)
nuxtI18nOptions.locales = [Object({\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"path\\":\\"/path/to/en.json\\",\\"testFunc\\":((prop) => {
return \`Hello \${prop}\`;
})}),Object({\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"path\\":\\"/path/to/ja.json\\",\\"testFunc\\":((prop) => {
return \`Hello \${prop}\`;
})}),Object({\\"code\\":\\"fr\\",\\"file\\":\\"fr.json\\",\\"path\\":\\"/path/to/fr.json\\",\\"testFunc\\":((prop) => {
return \`Hello \${prop}\`;
})})]
return nuxtI18nOptions
}
Expand All @@ -90,14 +94,16 @@ export const additionalMessages = Object({})
export const resolveNuxtI18nOptions = async (context) => {
const nuxtI18nOptions = Object({})
nuxtI18nOptions.defaultLocale = \\"en\\"
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"en\\",\\"messages\\": Object({\\"en\\":{
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{
\\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})()
},}),})
nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context)
nuxtI18nOptions.locales = [Object({\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"path\\":\\"/path/to/en.json\\",\\"testFunc\\":(function (prop) {
return \`Hello \${prop}\`;
})}),Object({\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"path\\":\\"/path/to/ja.json\\",\\"testFunc\\":(function (prop) {
return \`Hello \${prop}\`;
})}),Object({\\"code\\":\\"fr\\",\\"file\\":\\"fr.json\\",\\"path\\":\\"/path/to/fr.json\\",\\"testFunc\\":(function (prop) {
return \`Hello \${prop}\`;
})})]
return nuxtI18nOptions
}
Expand All @@ -112,11 +118,13 @@ export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
exports[`vueI18n: path 1`] = `
"import locale_en from \\"../locales/en.json\\" assert { type: \\"json\\" };
import locale_ja from \\"../locales/ja.json\\" assert { type: \\"json\\" };
import locale_fr from \\"../locales/fr.json\\" assert { type: \\"json\\" };
export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
export const localeMessages = {
\\"en\\": () => Promise.resolve(locale_en),
\\"ja\\": () => Promise.resolve(locale_ja),
\\"fr\\": () => Promise.resolve(locale_fr),
}
export const additionalMessages = Object({\\"en\\":[() => Promise.resolve({
Expand Down
7 changes: 6 additions & 1 deletion test/gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const LOCALE_INFO = [
code: 'ja',
file: 'ja.json',
path: '/path/to/ja.json'
},
{
code: 'fr',
file: 'fr.json',
path: '/path/to/fr.json'
}
]
const ADDITIONAL_MESSAGES = {
Expand All @@ -33,7 +38,7 @@ const NUXT_I18N_OPTIONS = {
defaultLocale: 'en',
vueI18n: {
locale: 'en',
fallbackLocale: 'en',
fallbackLocale: 'fr',
messages: {
en: { hello: 'Hello!' }
}
Expand Down

0 comments on commit d43ee72

Please sign in to comment.