Skip to content

Commit 49e35c3

Browse files
committed
feat(neuron-ui): cache language configuration
1 parent 13a85ca commit 49e35c3

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

packages/neuron-ui/src/utils/i18n.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import i18n from 'i18next'
22
import { initReactI18next } from 'react-i18next'
3+
import { language } from 'utils/localCache'
34
import zh from 'locales/zh.json'
45
import en from 'locales/en.json'
56

@@ -8,7 +9,7 @@ i18n.use(initReactI18next).init({
89
en,
910
zh,
1011
},
11-
fallbackLng: 'en',
12+
fallbackLng: language.load(),
1213
interpolation: {
1314
escapeValue: false,
1415
},

packages/neuron-ui/src/utils/initializeApp.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
currentNetworkID as currentNetworkIDCache,
99
currentWallet as currentWalletCache,
1010
systemScript as systemScriptCache,
11+
language as languageCache,
1112
} from 'utils/localCache'
1213
import { Routes, ConnectionStatus } from 'utils/const'
1314
import { WalletWizardPath } from 'components/WalletWizard'
@@ -25,7 +26,7 @@ const intializeApp = ({
2526
dispatch: StateDispatch
2627
}) => {
2728
const {
28-
locale = 'zh-CN',
29+
locale,
2930
networks = [],
3031
currentNetworkID: networkID = '',
3132
wallets = [],
@@ -36,8 +37,9 @@ const intializeApp = ({
3637
connectionStatus = false,
3738
codeHash = '',
3839
} = initializedState
39-
if (locale !== i18n.language) {
40+
if (locale && locale !== i18n.language) {
4041
i18n.changeLanguage(locale)
42+
languageCache.save(locale)
4143
}
4244
if (wallet && wallet.id) {
4345
history.push(Routes.Overview)

packages/neuron-ui/src/utils/localCache.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export enum LocalCacheKey {
66
CurrentWallet = 'currentWallet',
77
CurrentNetworkID = 'currentNetworkID',
88
SystemScript = 'systemScript',
9+
Language = 'lng',
910
}
1011
enum AddressBookVisibility {
1112
Invisible = '0',
@@ -141,6 +142,16 @@ export const systemScript = {
141142
},
142143
}
143144

145+
export const language = {
146+
save: (lng: string) => {
147+
window.localStorage.setItem(LocalCacheKey.Language, lng)
148+
return true
149+
},
150+
load: () => {
151+
return window.localStorage.getItem(LocalCacheKey.Language) || 'en'
152+
},
153+
}
154+
144155
export default {
145156
LocalCacheKey,
146157
addressBook,
@@ -150,4 +161,5 @@ export default {
150161
currentWallet,
151162
currentNetworkID,
152163
systemScript,
164+
language,
153165
}

0 commit comments

Comments
 (0)