@@ -12,7 +12,7 @@ import type { LocaleObject, NuxtI18nOptions, VueI18nConfigPathInfo } from './typ
1212
1313const debug = createDebug ( '@nuxtjs/i18n:layers' )
1414
15- export const checkLayerOptions = ( _options : NuxtI18nOptions , nuxt : Nuxt ) => {
15+ export function checkLayerOptions ( _options : NuxtI18nOptions , nuxt : Nuxt ) {
1616 const logger = useLogger ( NUXT_I18N_MODULE_ID )
1717 const project = nuxt . options . _layers [ 0 ]
1818 const layers = nuxt . options . _layers
@@ -21,32 +21,28 @@ export const checkLayerOptions = (_options: NuxtI18nOptions, nuxt: Nuxt) => {
2121 const layerI18n = getLayerI18n ( layer )
2222 if ( layerI18n == null ) continue
2323
24- const configLocation = project . config . rootDir === layer . config . rootDir ? 'project layer ' : 'extended layer '
25- const layerHint = `In ${ configLocation } (\`${ resolve ( project . config . rootDir , layer . configFile ) } \`) -`
24+ const configLocation = project . config . rootDir === layer . config . rootDir ? 'project' : 'extended'
25+ const layerHint = `In ${ configLocation } layer (\`${ resolve ( project . config . rootDir , layer . configFile ) } \`) -`
2626
2727 try {
2828 // check `langDir` option
2929 if ( layerI18n . langDir ) {
30- const locales = layerI18n . locales || [ ]
31-
3230 if ( isString ( layerI18n . langDir ) && isAbsolute ( layerI18n . langDir ) ) {
3331 logger . warn (
3432 `${ layerHint } \`langDir\` is set to an absolute path (\`${ layerI18n . langDir } \`) but should be set a path relative to \`srcDir\` (\`${ layer . config . srcDir } \`). ` +
3533 `Absolute paths will not work in production, see https://i18n.nuxtjs.org/options/lazy#langdir for more details.`
3634 )
3735 }
3836
39- for ( const locale of locales ) {
37+ for ( const locale of layerI18n . locales ?? [ ] ) {
4038 if ( isString ( locale ) ) {
4139 throw new Error ( 'When using the `langDir` option the `locales` must be a list of objects.' )
4240 }
43-
44- if ( ! ( locale . file || locale . files ) ) {
45- throw new Error (
46- 'All locales must have the `file` or `files` property set when using `langDir`.\n' +
47- `Found none in:\n${ JSON . stringify ( locale , null , 2 ) } .`
48- )
49- }
41+ if ( locale . file || locale . files ) continue
42+ throw new Error (
43+ 'All locales must have the `file` or `files` property set when using `langDir`.\n' +
44+ `Found none in:\n${ JSON . stringify ( locale , null , 2 ) } .`
45+ )
5046 }
5147 }
5248 } catch ( err ) {
@@ -56,19 +52,7 @@ export const checkLayerOptions = (_options: NuxtI18nOptions, nuxt: Nuxt) => {
5652 }
5753}
5854
59- /**
60- * Merges `locales` configured by each layer and resolves the locale `files` to absolute paths.
61- *
62- * This overwrites `options.locales`
63- */
64- export const applyLayerOptions = ( options : NuxtI18nOptions , nuxt : Nuxt ) => {
65- const mergedLocales = mergeLayerLocales ( options , nuxt )
66- debug ( 'merged locales' , mergedLocales )
67-
68- options . locales = mergedLocales
69- }
70-
71- export const mergeLayerPages = ( analyzer : ( pathOverride : string ) => void , nuxt : Nuxt ) => {
55+ export function mergeLayerPages ( analyzer : ( pathOverride : string ) => void , nuxt : Nuxt ) {
7256 const project = nuxt . options . _layers [ 0 ]
7357 const layers = nuxt . options . _layers
7458
@@ -86,7 +70,6 @@ export function resolveI18nDir(layer: NuxtConfigLayer, i18n: NuxtI18nOptions, fr
8670 if ( i18n . restructureDir !== false ) {
8771 return resolve ( layer . config . rootDir , i18n . restructureDir ?? 'i18n' )
8872 }
89-
9073 return resolve ( layer . config . rootDir , fromRootDir ? '' : layer . config . srcDir )
9174}
9275
@@ -96,25 +79,25 @@ function resolveLayerLangDir(layer: NuxtConfigLayer, i18n: NuxtI18nOptions) {
9679 return resolve ( resolveI18nDir ( layer , i18n ) , i18n . langDir )
9780}
9881
99- const mergeLayerLocales = ( options : NuxtI18nOptions , nuxt : Nuxt ) => {
100- debug ( 'project layer `lazy` option' , options . lazy )
82+ /**
83+ * Merges `locales` configured by each layer and resolves the locale `files` to absolute paths.
84+ * This overwrites `options.locales`
85+ */
86+ export function applyLayerOptions ( options : NuxtI18nOptions , nuxt : Nuxt ) {
10187 options . locales ??= [ ]
10288
10389 const configs : LocaleConfig [ ] = [ ]
104-
10590 for ( const layer of nuxt . options . _layers ) {
10691 const i18n = getLayerI18n ( layer )
10792 if ( i18n ?. locales == null ) continue
108-
109- configs . push ( assign ( { } , i18n , { langDir : resolveLayerLangDir ( layer , i18n ) } ) )
93+ configs . push ( assign ( { } , i18n , { langDir : resolveLayerLangDir ( layer , i18n ) , locales : i18n . locales } ) )
11094 }
11195
112- const installModuleConfigMap = new Map < string , LocaleConfig > ( )
113-
11496 /**
11597 * Collect any locale files that are not provided by layers these are added when
11698 * installing through `installModule` and should have absolute paths.
11799 */
100+ const installModuleConfigMap = new Map < string , LocaleConfig > ( )
118101 outer: for ( const locale of options . locales ) {
119102 if ( isString ( locale ) ) continue
120103
@@ -136,27 +119,28 @@ const mergeLayerLocales = (options: NuxtI18nOptions, nuxt: Nuxt) => {
136119
137120 configs . unshift ( ...installModuleConfigMap . values ( ) )
138121
139- return mergeConfigLocales ( configs )
122+ debug ( 'merged locales' , configs )
123+ options . locales = mergeConfigLocales ( configs )
140124}
141125
142126export async function resolveLayerVueI18nConfigInfo ( options : NuxtI18nOptions ) {
143127 const logger = useLogger ( NUXT_I18N_MODULE_ID )
144128 const nuxt = useNuxt ( )
145129
146- const resolveArr = nuxt . options . _layers . map ( async layer => {
147- const i18n = getLayerI18n ( layer )
148- const i18nDirPath = resolveI18nDir ( layer , i18n || { } , true )
149- const res = await resolveVueI18nConfigInfo ( i18nDirPath , i18n ?. vueI18n )
150-
151- if ( res == null && i18n ?. vueI18n != null ) {
152- logger . warn ( `Vue I18n configuration file \`${ i18n . vueI18n } \` not found in \`${ i18nDirPath } \`. Skipping...` )
153- return undefined
154- }
130+ const resolved = await Promise . all (
131+ nuxt . options . _layers . map ( async layer => {
132+ const i18n = getLayerI18n ( layer )
133+ const i18nDirPath = resolveI18nDir ( layer , i18n || { } , true )
134+ const res = await resolveVueI18nConfigInfo ( i18nDirPath , i18n ?. vueI18n )
155135
156- return res
157- } )
136+ if ( res == null && i18n ?. vueI18n != null ) {
137+ logger . warn ( `Vue I18n configuration file \`${ i18n . vueI18n } \` not found in \`${ i18nDirPath } \`. Skipping...` )
138+ return undefined
139+ }
158140
159- const resolved = await Promise . all ( resolveArr )
141+ return res
142+ } )
143+ )
160144
161145 // use `vueI18n` passed by `installModule`
162146 if ( options . vueI18n && isAbsolute ( options . vueI18n ) ) {
0 commit comments