@@ -45,6 +45,7 @@ export function injectDefaultLocale() {
4545export class TranslationStore {
4646 private readonly cache = createIntlCache ( ) ;
4747 private readonly config = injectIntlConfig ( ) ;
48+ readonly loadQueue = signal < string [ ] > ( [ ] ) ;
4849 readonly locale = signal ( inject ( LOCALE_ID ) ) ;
4950 private readonly defaultLocale = injectDefaultLocale ( ) ;
5051 private readonly translations = signal <
@@ -73,18 +74,8 @@ export class TranslationStore {
7374 { } ,
7475 ) ;
7576
76- private isInit = true ;
77- private readonly skipInitLocale = computed ( ( ) => {
78- // should load new loaders if switched back to initial locale, so we do it like this
79- if ( this . isInit ) {
80- this . isInit = false ;
81- return null ;
82- }
83- return this . locale ( ) ;
84- } ) ;
85-
8677 readonly dynamicLocaleLoader = resource ( {
87- params : ( ) => this . skipInitLocale ( ) ,
78+ params : computed ( ( ) => this . loadQueue ( ) . at ( 0 ) ?? null ) ,
8879 loader : async ( { params : newLocale , abortSignal } ) => {
8980 if ( ! newLocale ) return ;
9081
@@ -159,12 +150,17 @@ export class TranslationStore {
159150 )
160151 return ;
161152 const dynamicLocales = this . dynamicLocaleLoader . value ( ) ;
153+
162154 if ( ! dynamicLocales ) return ;
163155 for ( const locale of dynamicLocales . locales ) {
164156 this . register ( locale . namespace , {
165157 [ dynamicLocales . locale ] : locale . flat ,
166158 } ) ;
167159 }
160+ this . loadQueue . update ( ( q ) =>
161+ q . filter ( ( l ) => l !== dynamicLocales . locale ) ,
162+ ) ;
163+ this . locale . set ( dynamicLocales . locale ) ;
168164 } ) ;
169165 }
170166
@@ -250,22 +246,25 @@ export function injectDynamicLocale(): WritableSignal<string> & {
250246} {
251247 const store = inject ( TranslationStore ) ;
252248
253- const source = store . locale as WritableSignal < string > & {
249+ const source = computed ( ( ) => store . locale ( ) ) as WritableSignal < string > & {
254250 isLoading : Signal < boolean > ;
255251 } ;
256252
257- const originalSet = source . set ;
258-
259- source . set = ( value : string ) => {
260- if ( value === untracked ( source ) || ! store . hasLocaleLoaders ( value ) ) return ;
261-
262- originalSet ( value ) ;
253+ const set = ( value : string ) => {
254+ if (
255+ value === untracked ( source ) ||
256+ ! store . hasLocaleLoaders ( value ) ||
257+ untracked ( store . loadQueue ) . includes ( value )
258+ )
259+ return ;
260+ store . loadQueue . update ( ( q ) => [ ...q , value ] ) ;
263261 } ;
264-
262+ source . set = set ;
265263 source . update = ( updater : ( value : string ) => string ) => {
266264 const next = updater ( untracked ( source ) ) ;
267265 source . set ( next ) ;
268266 } ;
267+ source . asReadonly = ( ) => source ;
269268
270269 source . isLoading = store . dynamicLocaleLoader . isLoading ;
271270
0 commit comments