1- import i18n from "i18next" ;
1+ import { SystemConfig } from "@App/pkg/config/config" ;
2+ import i18n , { Callback } from "i18next" ;
23import { initReactI18next } from "react-i18next" ;
34import dayjs from "dayjs" ;
45import relativeTime from "dayjs/plugin/relativeTime" ;
@@ -16,43 +17,46 @@ import "dayjs/locale/zh-cn";
1617import "dayjs/locale/zh-tw" ;
1718import "dayjs/locale/ja" ;
1819import "dayjs/locale/de" ;
19- import { systemConfig } from "@App/pages/store/global" ;
2020
21- const uiLanguage = chrome . i18n . getUILanguage ( ) ;
22-
23- i18n . use ( initReactI18next ) . init ( {
24- fallbackLng : "en-US" ,
25- lng : globalThis . localStorage ? localStorage [ "language" ] || uiLanguage : uiLanguage , // 优先使用localStorage中的语言设置
26- interpolation : {
27- escapeValue : false , // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
28- } ,
29- resources : {
30- "en-US" : { title : "English" , translation : enUS } ,
31- "vi-VN" : { title : "Tiếng Việt" , translation : viVN } ,
32- "zh-CN" : { title : "简体中文" , translation : zhCN } ,
33- "zh-TW" : { title : "繁体中文" , translation : zhTW } ,
34- "ach-UG" : { title : "伪语言" , translation : achUG } ,
35- "ja-JP" : { title : "日本語" , translation : jaJP } ,
36- "de-DE" : { title : "Deutsch" , translation : deDE } ,
37- } ,
38- } ) ;
21+ dayjs . extend ( relativeTime ) ;
3922
4023export let localePath = "" ;
4124
42- async function initLanguage ( ) {
43- const lng = await systemConfig . getLanguage ( ) ;
44- i18n . changeLanguage ( lng ) ;
25+ export function changeLanguage ( lng : string , callback ?: Callback ) : void {
26+ i18n . changeLanguage ( lng , callback ) ;
4527 dayjs . locale ( lng . toLocaleLowerCase ( ) ) ;
46- if ( lng !== "zh-CN" ) {
47- localePath = "en" ;
48- }
4928}
5029
51- setTimeout ( ( ) => {
52- initLanguage ( ) ;
53- } , 0 ) ;
30+ // let cachedSystemConfig: SystemConfig;
5431
55- dayjs . extend ( relativeTime ) ;
32+ export function initLocales ( systemConfig : SystemConfig ) {
33+ // cachedSystemConfig = systemConfig;
34+ const uiLanguage = chrome . i18n . getUILanguage ( ) ;
35+ const defaultLanguage = globalThis . localStorage ? localStorage [ "language" ] || uiLanguage : uiLanguage ;
36+ i18n . use ( initReactI18next ) . init ( {
37+ fallbackLng : "en-US" ,
38+ lng : defaultLanguage , // 优先使用localStorage中的语言设置
39+ interpolation : {
40+ escapeValue : false , // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
41+ } ,
42+ resources : {
43+ "en-US" : { title : "English" , translation : enUS } ,
44+ "vi-VN" : { title : "Tiếng Việt" , translation : viVN } ,
45+ "zh-CN" : { title : "简体中文" , translation : zhCN } ,
46+ "zh-TW" : { title : "繁体中文" , translation : zhTW } ,
47+ "ach-UG" : { title : "伪语言" , translation : achUG } ,
48+ "ja-JP" : { title : "日本語" , translation : jaJP } ,
49+ "de-DE" : { title : "Deutsch" , translation : deDE } ,
50+ } ,
51+ } ) ;
52+
53+ systemConfig . getLanguage ( ) . then ( lng => {
54+ changeLanguage ( lng ) ;
55+ if ( lng !== "zh-CN" ) {
56+ localePath = "en" ;
57+ }
58+ } ) ;
59+ }
5660
5761export function i18nName ( script : { name : string ; metadata : Metadata } ) {
5862 return script . metadata [ `name:${ i18n . language . toLowerCase ( ) } ` ]
0 commit comments