@@ -8,30 +8,33 @@ export function createGetScopedI18n<Locales extends ImportedLocales, Locale exte
8
8
locales : Locales ,
9
9
config : I18nServerConfig ,
10
10
) {
11
- const localeCache = new Map < string , ReturnType < typeof createT < Locale , undefined > > > ( ) ;
11
+ const localeCache = new Map < string , Promise < ReturnType < typeof createT < Locale , undefined > > > > ( ) ;
12
12
13
13
return async function getScopedI18n < Scope extends Scopes < Locale > > (
14
14
scope : Scope ,
15
15
) : Promise < ReturnType < typeof createT < Locale , Scope > > > {
16
- const locale = getLocaleCache ( ) ;
16
+ const locale = await getLocaleCache ( ) ;
17
17
const cacheKey = `${ locale } -${ scope } ` ;
18
18
const cached = localeCache . get ( cacheKey ) ;
19
19
20
20
if ( cached ) {
21
- return cached ;
21
+ return ( await cached ) as ReturnType < typeof createT < Locale , Scope > > ;
22
22
}
23
23
24
- const localeFn = createT (
25
- {
26
- localeContent : flattenLocale ( ( await locales [ locale ] ( ) ) . default ) ,
27
- fallbackLocale : config . fallbackLocale ? flattenLocale ( config . fallbackLocale ) : undefined ,
28
- locale,
29
- } as LocaleContext < Locale > ,
30
- scope ,
31
- ) ;
24
+ const localeFnPromise = ( async ( ) => {
25
+ const localeModule = await locales [ locale ] ( ) ;
26
+ return createT (
27
+ {
28
+ localeContent : flattenLocale ( localeModule . default ) ,
29
+ fallbackLocale : config . fallbackLocale ? flattenLocale ( config . fallbackLocale ) : undefined ,
30
+ locale,
31
+ } as LocaleContext < Locale > ,
32
+ scope ,
33
+ ) ;
34
+ } ) ( ) ;
32
35
33
- localeCache . set ( cacheKey , localeFn ) ;
36
+ localeCache . set ( cacheKey , localeFnPromise ) ;
34
37
35
- return localeFn ;
38
+ return await localeFnPromise ;
36
39
} ;
37
40
}
0 commit comments