@@ -5,6 +5,7 @@ import { LanguageJsonFileInterface } from './interfaces/language-json-file'
5
5
import { ReplacementsInterface } from './interfaces/replacements'
6
6
import { choose } from './pluralization'
7
7
import { avoidExceptionOnPromise , avoidException } from './utils/avoid-exceptions'
8
+ import { hasPhpTranslations } from './utils/has-php-translations'
8
9
9
10
const isServer = typeof window === 'undefined'
10
11
@@ -41,6 +42,23 @@ export function isLoaded(lang?: string): boolean {
41
42
return loaded . some ( ( row ) => row . lang . replace ( / [ - _ ] / g, '-' ) === lang . replace ( / [ - _ ] / g, '-' ) )
42
43
}
43
44
45
+ /**
46
+ * Loads the language async.
47
+ */
48
+ function loadLanguage ( lang : string , dashLangTry : boolean = false ) : void {
49
+ const loadedLang : LanguageInterface = loaded . find ( ( row ) => row . lang === lang )
50
+
51
+ if ( loadedLang ) {
52
+ setLanguage ( loadedLang )
53
+
54
+ return
55
+ }
56
+
57
+ const { default : messages } = resolveLang ( options . resolve , lang )
58
+
59
+ applyLanguage ( lang , messages , dashLangTry , loadLanguage )
60
+ }
61
+
44
62
/**
45
63
* Loads the language file.
46
64
*/
@@ -51,23 +69,37 @@ export function loadLanguageAsync(lang: string, dashLangTry = false): Promise<st
51
69
return Promise . resolve ( setLanguage ( loadedLang ) )
52
70
}
53
71
54
- return resolveLang ( options . resolve , lang ) . then ( ( { default : messages } ) => {
55
- if ( Object . keys ( messages ) . length < 1 ) {
56
- if ( / [ - _ ] / g. test ( lang ) && ! dashLangTry ) {
57
- return loadLanguageAsync (
58
- lang . replace ( / [ - _ ] / g, ( char ) => ( char === '-' ? '_' : '-' ) ) ,
59
- true
60
- )
61
- }
62
- if ( lang !== options . fallbackLang ) {
63
- return loadLanguageAsync ( options . fallbackLang )
64
- }
72
+ return resolveLangAsync ( options . resolve , lang ) . then ( ( { default : messages } ) =>
73
+ applyLanguage ( lang , messages , dashLangTry , loadLanguageAsync )
74
+ )
75
+ }
76
+
77
+ /**
78
+ * Applies the language data and saves it to the loaded storage.
79
+ */
80
+ function applyLanguage (
81
+ lang : string ,
82
+ messages : { [ key : string ] : string } ,
83
+ dashLangTry : boolean = false ,
84
+ callable : Function
85
+ ) : string {
86
+ if ( Object . keys ( messages ) . length < 1 ) {
87
+ if ( / [ - _ ] / g. test ( lang ) && ! dashLangTry ) {
88
+ return callable (
89
+ lang . replace ( / [ - _ ] / g, ( char ) => ( char === '-' ? '_' : '-' ) ) ,
90
+ true
91
+ )
65
92
}
66
93
67
- const data : LanguageInterface = { lang, messages }
68
- loaded . push ( data )
69
- return setLanguage ( data )
70
- } )
94
+ if ( lang !== options . fallbackLang ) {
95
+ return callable ( options . fallbackLang )
96
+ }
97
+ }
98
+
99
+ const data : LanguageInterface = { lang, messages }
100
+ loaded . push ( data )
101
+
102
+ return setLanguage ( data )
71
103
}
72
104
73
105
/**
@@ -142,53 +174,58 @@ function setLanguage({ lang, messages }: LanguageInterface): string {
142
174
}
143
175
144
176
/**
145
- * It resolves the language file or data, from direct data, require or Promise .
177
+ * It resolves the language file or data, from direct data, syncrone .
146
178
*/
147
- async function resolveLang ( callable : Function , lang : string ) : Promise < LanguageJsonFileInterface > {
148
- const hasPhpTranslations =
149
- typeof process !== 'undefined' && process . env ?. LARAVEL_VUE_I18N_HAS_PHP
150
- ? true
151
- : /** @ts -ignore */
152
- typeof import . meta. env !== 'undefined' && import . meta. env . VITE_LARAVEL_VUE_I18N_HAS_PHP
153
- ? true
154
- : false
155
-
156
- let data = avoidException ( callable , lang )
179
+ function resolveLang (
180
+ callable : Function ,
181
+ lang : string ,
182
+ data : { [ key : string ] : string } = { }
183
+ ) : LanguageJsonFileInterface {
184
+ if ( ! Object . keys ( data ) . length ) {
185
+ data = avoidException ( callable , lang )
186
+ }
157
187
158
- if ( data instanceof Promise ) {
159
- if ( hasPhpTranslations ) {
160
- const phpLang = await avoidExceptionOnPromise ( callable ( `php_${ lang } ` ) )
161
- const jsonLang = await avoidExceptionOnPromise ( data )
162
-
163
- return new Promise ( ( resolve ) =>
164
- resolve ( {
165
- default : {
166
- ...phpLang ,
167
- ...jsonLang
168
- }
169
- } )
170
- )
188
+ if ( hasPhpTranslations ( isServer ) ) {
189
+ return {
190
+ default : {
191
+ ...data ,
192
+ ...avoidException ( callable , `php_${ lang } ` )
193
+ }
171
194
}
195
+ }
172
196
173
- return new Promise ( async ( resolve ) =>
174
- resolve ( {
175
- default : await avoidExceptionOnPromise ( data )
176
- } )
177
- )
197
+ return { default : data }
198
+ }
199
+
200
+ /**
201
+ * It resolves the language file or data, from direct data, require or Promise.
202
+ */
203
+ async function resolveLangAsync ( callable : Function , lang : string ) : Promise < LanguageJsonFileInterface > {
204
+ let data = avoidException ( callable , lang )
205
+
206
+ if ( ! ( data instanceof Promise ) ) {
207
+ return resolveLang ( callable , lang , data )
178
208
}
179
209
180
- if ( hasPhpTranslations ) {
210
+ if ( hasPhpTranslations ( isServer ) ) {
211
+ const phpLang = await avoidExceptionOnPromise ( callable ( `php_${ lang } ` ) )
212
+ const jsonLang = await avoidExceptionOnPromise ( data )
213
+
181
214
return new Promise ( ( resolve ) =>
182
215
resolve ( {
183
216
default : {
184
- ...data ,
185
- ...avoidException ( callable , `php_ ${ lang } ` )
217
+ ...phpLang ,
218
+ ...jsonLang
186
219
}
187
220
} )
188
221
)
189
222
}
190
223
191
- return new Promise ( ( resolve ) => resolve ( { default : data } ) )
224
+ return new Promise ( async ( resolve ) =>
225
+ resolve ( {
226
+ default : await avoidExceptionOnPromise ( data )
227
+ } )
228
+ )
192
229
}
193
230
194
231
/**
@@ -235,6 +272,11 @@ export const i18nVue: Plugin = {
235
272
app . config . globalProperties . $t = ( key : string , replacements : ReplacementsInterface ) => trans ( key , replacements )
236
273
app . config . globalProperties . $tChoice = ( key : string , number : number , replacements : ReplacementsInterface ) =>
237
274
transChoice ( key , number , replacements )
238
- loadLanguageAsync ( options . lang || options . fallbackLang )
275
+
276
+ if ( isServer ) {
277
+ loadLanguage ( options . lang || options . fallbackLang )
278
+ } else {
279
+ loadLanguageAsync ( options . lang || options . fallbackLang )
280
+ }
239
281
}
240
282
}
0 commit comments