@@ -20,7 +20,7 @@ type LocaleModule = { default?: LocaleResources } & LocaleResources
2020const LOCALE_PATH_PATTERN = / \. \/ l o c a l e s \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \. j s o n $ /
2121
2222const eagerLocaleFiles = import . meta. glob < LocaleModule > ( "./locales/en/*.json" , { eager : true } )
23- const lazyLocaleFiles = import . meta. glob < LocaleModule > ( "./locales/**/*.json" )
23+ const lazyLocaleFiles = import . meta. glob < LocaleModule > ( [ "./locales/**/*.json" , "!./locales/en/*.json" ] )
2424
2525const pendingLanguages = new Map < string , Promise < void > > ( )
2626const loadedLanguages = new Set < string > ( [ FALLBACK_LANGUAGE ] )
@@ -67,27 +67,19 @@ export function loadLanguage(language: string): Promise<void> {
6767 return pending
6868 }
6969
70- const loaders = Object . entries ( lazyLocaleFiles )
71- . filter ( ( [ path ] ) => parseLocalePath ( path ) ?. language === language )
72- . map ( ( [ path , load ] ) => load ( ) . then ( ( module ) => ( { path, module } ) ) )
70+ const loaders = Object . entries ( lazyLocaleFiles ) . flatMap ( ( [ path , load ] ) => {
71+ const parsed = parseLocalePath ( path )
7372
74- // Unknown language: leave the fallback bundles in place so i18next resolves
75- // every key through `fallbackLng`.
76- if ( loaders . length === 0 ) {
77- loadedLanguages . add ( language )
78- return Promise . resolve ( )
79- }
73+ return parsed ?. language === language ? [ load ( ) . then ( ( module ) => ( { namespace : parsed . namespace , module } ) ) ] : [ ]
74+ } )
8075
76+ // An unknown language has no loaders, so `Promise.all` resolves at once and
77+ // the fallback bundles stay in place: i18next resolves every key through
78+ // `fallbackLng`.
8179 const promise = Promise . all ( loaders )
8280 . then ( ( modules ) => {
83- modules . forEach ( ( { path, module } ) => {
84- const parsed = parseLocalePath ( path )
85-
86- if ( ! parsed ) {
87- return
88- }
89-
90- i18next . addResourceBundle ( parsed . language , parsed . namespace , module . default || module , true , true )
81+ modules . forEach ( ( { namespace, module } ) => {
82+ i18next . addResourceBundle ( language , namespace , module . default || module , true , true )
9183 } )
9284
9385 loadedLanguages . add ( language )
@@ -110,8 +102,7 @@ i18next.use(initReactI18next).init({
110102 lng : FALLBACK_LANGUAGE , // Default language (will be overridden)
111103 fallbackLng : FALLBACK_LANGUAGE ,
112104 debug : false ,
113- // Handed to `init` rather than added afterwards so the first render already
114- // resolves keys; `addResourceBundle` before init settles would be dropped.
105+ // Handed to `init` so the first render already resolves keys.
115106 resources : collectLocaleResources ( eagerLocaleFiles ) ,
116107 interpolation : {
117108 escapeValue : false , // React already escapes by default
0 commit comments