17
17
18
18
import i18n from 'i18next' ;
19
19
import { getNativeLocale } from '@/api/nativelocale' ;
20
+ import appTranslationsAR from '@/locales/ar/app.json' ;
21
+ import appTranslationsCS from '@/locales/cs/app.json' ;
22
+ import appTranslationsDE from '@/locales/de/app.json' ;
23
+ import appTranslationsEN from '@/locales/en/app.json' ;
24
+ import appTranslationsFR from '@/locales/fr/app.json' ;
25
+ import appTranslationsJA from '@/locales/ja/app.json' ;
26
+ import appTranslationsRU from '@/locales/ru/app.json' ;
27
+ import appTranslationsMS from '@/locales/ms/app.json' ;
28
+ import appTranslationsNL from '@/locales/nl/app.json' ;
29
+ import appTranslationsPT from '@/locales/pt/app.json' ;
30
+ import appTranslationsHI from '@/locales/hi/app.json' ;
31
+ import appTranslationsBG from '@/locales/bg/app.json' ;
32
+ import appTranslationsTR from '@/locales/tr/app.json' ;
33
+ import appTranslationsZH from '@/locales/zh/app.json' ;
34
+ import appTranslationsFA from '@/locales/fa/app.json' ;
35
+ import appTranslationsES from '@/locales/es/app.json' ;
36
+ import appTranslationsSL from '@/locales/sl/app.json' ;
37
+ import appTranslationsHE from '@/locales/he/app.json' ;
38
+ import appTranslationsIT from '@/locales/it/app.json' ;
20
39
import { languageFromConfig } from './config' ;
21
40
import { localeMainLanguage } from './utils' ;
22
41
import { setConfig } from '@/utils/config' ;
23
- import appTranslationsEN from '@/locales/en/app.json' ;
24
42
25
43
const locizeProjectID = 'fe4e5a24-e4a2-4903-96fc-3d62c11fc502' ;
26
44
27
- let isChangingLanguage = false ;
28
- const defaultFallbackLang = 'en' ;
29
-
30
- const languageResources = {
31
- ar : ( ) => import ( '@/locales/ar/app.json' ) ,
32
- cs : ( ) => import ( '@/locales/cs/app.json' ) ,
33
- de : ( ) => import ( '@/locales/de/app.json' ) ,
34
- en : ( ) => Promise . resolve ( { default : appTranslationsEN } ) ,
35
- fr : ( ) => import ( '@/locales/fr/app.json' ) ,
36
- ja : ( ) => import ( '@/locales/ja/app.json' ) ,
37
- ru : ( ) => import ( '@/locales/ru/app.json' ) ,
38
- ms : ( ) => import ( '@/locales/ms/app.json' ) ,
39
- nl : ( ) => import ( '@/locales/nl/app.json' ) ,
40
- pt : ( ) => import ( '@/locales/pt/app.json' ) ,
41
- hi : ( ) => import ( '@/locales/hi/app.json' ) ,
42
- bg : ( ) => import ( '@/locales/bg/app.json' ) ,
43
- tr : ( ) => import ( '@/locales/tr/app.json' ) ,
44
- zh : ( ) => import ( '@/locales/zh/app.json' ) ,
45
- fa : ( ) => import ( '@/locales/fa/app.json' ) ,
46
- es : ( ) => import ( '@/locales/es/app.json' ) ,
47
- sl : ( ) => import ( '@/locales/sl/app.json' ) ,
48
- he : ( ) => import ( '@/locales/he/app.json' ) ,
49
- it : ( ) => import ( '@/locales/it/app.json' )
50
- } ;
51
-
52
- type LanguageKey = keyof typeof languageResources ;
53
-
54
- export const loadLanguage = async ( language : string ) => {
55
- try {
56
- const resources = await languageResources [ language as LanguageKey ] ( ) ;
57
- if ( ! i18n . hasResourceBundle ( language , 'app' ) ) {
58
- i18n . addResourceBundle ( language , 'app' , resources . default || resources ) ;
59
- }
60
- } catch ( error ) {
61
- console . error ( `Failed to load language resources for ${ language } :` , error ) ;
62
- }
63
- } ;
64
-
65
- export const changei18nLanguage = async ( language : string ) => {
66
- await loadLanguage ( language ) ;
67
- await i18n . changeLanguage ( language ) ;
68
- } ;
69
-
70
- let i18Init = i18n . use ( languageFromConfig ) ;
45
+ let i18Init = i18n
46
+ . use ( languageFromConfig ) ;
71
47
72
48
i18Init . init ( {
73
- fallbackLng : defaultFallbackLang ,
49
+ fallbackLng : 'en' ,
74
50
75
51
// have a common namespace used around the full app
76
52
ns : [ 'app' , 'wallet' ] ,
@@ -83,56 +59,55 @@ i18Init.init({
83
59
} ,
84
60
85
61
react : {
86
- useSuspense : true // Not using Suspense you will need to handle the not ready state yourself
62
+ useSuspense : true , // Not using Suspense you will need to handle the not ready state yourself
87
63
} ,
88
64
89
65
backend : {
90
66
projectId : locizeProjectID ,
91
- referenceLng : defaultFallbackLang
92
- }
67
+ referenceLng : 'en'
68
+ } ,
93
69
} ) ;
94
70
95
- // always include 'en' so we have a fallback for keys that are not translated
96
- i18n . addResourceBundle ( defaultFallbackLang , 'app' , appTranslationsEN ) ;
97
-
98
- i18n . on ( 'languageChanged' , async ( lng ) => {
99
- // changei18nLanguage triggers languageChanged, thus this check to prevent loop
100
- if ( isChangingLanguage ) {
101
- return ;
102
- }
103
-
104
- try {
105
- isChangingLanguage = true ;
106
- // Set userLanguage in config back to empty if system locale matches
107
- // the newly selected language lng to make the app use native-locale again.
108
- // This also covers partial matches. For example, if native locale is pt_BR
109
- // and the app has only pt translation, assume they match.
110
- //
111
- // Since userLanguage is stored in the backend config as a string,
112
- // setting it to null here in JS turns it into an empty string "" in Go backend.
113
- // This is ok since we're just checking for a truthy value in the language detector.
114
- const nativeLocale = await getNativeLocale ( ) ;
71
+ i18n . addResourceBundle ( 'ar' , 'app' , appTranslationsAR ) ;
72
+ i18n . addResourceBundle ( 'cs' , 'app' , appTranslationsCS ) ;
73
+ i18n . addResourceBundle ( 'de' , 'app' , appTranslationsDE ) ;
74
+ i18n . addResourceBundle ( 'en' , 'app' , appTranslationsEN ) ;
75
+ i18n . addResourceBundle ( 'fr' , 'app' , appTranslationsFR ) ;
76
+ i18n . addResourceBundle ( 'ja' , 'app' , appTranslationsJA ) ;
77
+ i18n . addResourceBundle ( 'ms' , 'app' , appTranslationsMS ) ;
78
+ i18n . addResourceBundle ( 'nl' , 'app' , appTranslationsNL ) ;
79
+ i18n . addResourceBundle ( 'ru' , 'app' , appTranslationsRU ) ;
80
+ i18n . addResourceBundle ( 'pt' , 'app' , appTranslationsPT ) ;
81
+ i18n . addResourceBundle ( 'hi' , 'app' , appTranslationsHI ) ;
82
+ i18n . addResourceBundle ( 'bg' , 'app' , appTranslationsBG ) ;
83
+ i18n . addResourceBundle ( 'tr' , 'app' , appTranslationsTR ) ;
84
+ i18n . addResourceBundle ( 'zh' , 'app' , appTranslationsZH ) ;
85
+ i18n . addResourceBundle ( 'fa' , 'app' , appTranslationsFA ) ;
86
+ i18n . addResourceBundle ( 'es' , 'app' , appTranslationsES ) ;
87
+ i18n . addResourceBundle ( 'sl' , 'app' , appTranslationsSL ) ;
88
+ i18n . addResourceBundle ( 'he' , 'app' , appTranslationsHE ) ;
89
+ i18n . addResourceBundle ( 'it' , 'app' , appTranslationsIT ) ;
90
+
91
+ i18n . on ( 'languageChanged' , ( lng ) => {
92
+ // Set userLanguage in config back to empty if system locale matches
93
+ // the newly selected language lng to make the app use native-locale again.
94
+ // This also covers partial matches. For example, if native locale is pt_BR
95
+ // and the app has only pt translation, assume they match.
96
+ //
97
+ // Since userLanguage is stored in the backend config as a string,
98
+ // setting it to null here in JS turns it into an empty string "" in Go backend.
99
+ // This is ok since we're just checking for a truthy value in the language detector.
100
+ return getNativeLocale ( ) . then ( ( nativeLocale ) => {
115
101
let match = lng === nativeLocale ;
116
-
117
102
if ( ! match ) {
118
- const localeLang = localeMainLanguage ( nativeLocale ) ;
103
+ // There are too many combinations. So, we compare only the main
104
+ // language tag.
119
105
const lngLang = localeMainLanguage ( lng ) ;
120
- await changei18nLanguage ( localeMainLanguage ( lng ) ) ;
106
+ const localeLang = localeMainLanguage ( nativeLocale ) ;
121
107
match = lngLang === localeLang ;
122
108
}
123
-
124
109
const uiLang = match ? null : lng ;
125
110
return setConfig ( { backend : { userLanguage : uiLang } } ) ;
126
- } finally {
127
- isChangingLanguage = false ;
128
- }
129
- } ) ;
130
-
131
- i18n . on ( 'initialized' , ( ) => {
132
- languageFromConfig . detect ( ( lang ) => {
133
- if ( lang && typeof lang === 'string' ) {
134
- changei18nLanguage ( localeMainLanguage ( lang ) ) ;
135
- }
136
111
} ) ;
137
112
} ) ;
138
113
0 commit comments