File tree 4 files changed +27
-11
lines changed
4 files changed +27
-11
lines changed Original file line number Diff line number Diff line change 24
24
"events" : " ^3.3.0" ,
25
25
"i18next" : " ^23.11.5" ,
26
26
"i18next-browser-languagedetector" : " ^8.0.0" ,
27
+ "i18next-resources-to-backend" : " ^1.2.1" ,
27
28
"idb" : " ^7.1.1" ,
28
29
"idb-open-plus" : " ^1.0.0" ,
29
30
"jsonc-parser" : " ^3.2.0" ,
Original file line number Diff line number Diff line change 1
1
import { Button , ButtonProps } from "antd" ;
2
+ import { useState } from "react" ;
2
3
import { useTranslation } from "react-i18next" ;
3
4
4
5
export const LanguageButton = ( props : ButtonProps ) => {
5
6
const { i18n } = useTranslation ( ) ;
7
+ const [ isLoading , setLoading ] = useState ( false ) ;
8
+ ( window as any ) . i18n = i18n ;
6
9
return (
7
10
< Button
11
+ loading = { isLoading }
8
12
shape = "circle"
9
13
{ ...props }
10
14
onClick = { ( ) => {
11
- i18n . changeLanguage ( i18n . resolvedLanguage === "en" ? "zh" : "en" ) ;
15
+ setLoading ( true ) ;
16
+ i18n
17
+ . changeLanguage ( i18n . resolvedLanguage === "en" ? "zh-cn" : "en" )
18
+ // .then(()=>console.log('changeLanguage ok'), (err) => console.error(err))
19
+ . finally ( ( ) => setLoading ( false ) ) ;
12
20
} }
13
21
>
14
- { i18n . resolvedLanguage === "en" ? "中" : "En" }
22
+ { isLoading ? "" : i18n . resolvedLanguage === "en" ? "中" : "En" }
15
23
</ Button >
16
24
) ;
17
25
} ;
Original file line number Diff line number Diff line change 1
1
import i18n from "i18next" ;
2
2
import { initReactI18next } from "react-i18next" ;
3
3
import LanguageDetector from "i18next-browser-languagedetector" ;
4
+ import resourcesToBackend from "i18next-resources-to-backend" ;
4
5
import { isValidElement } from "react" ;
5
- import en from '../i18n/en.json' ;
6
- import zh from '../i18n/zh.json' ;
7
- // const en = await import("../i18n/en.json");
8
- // const zh = await import("../i18n/zh.json");
6
+
9
7
const fallbackLng = "en" ;
10
8
i18n
11
9
// 检测用户当前使用的语言
12
10
// 文档: https://github.com/i18next/i18next-browser-languageDetector
13
11
. use ( LanguageDetector )
14
12
// 注入 react-i18next 实例
15
13
. use ( initReactI18next )
14
+ // 动态加载语言包
15
+ . use (
16
+ resourcesToBackend ( ( language :string , namespace :string ) => import ( `../i18n/${ language } .json` ) )
17
+ )
16
18
// 初始化 i18next
17
19
// 配置参数的文档: https://www.i18next.com/overview/configuration-options
18
20
. init ( {
19
- debug : true ,
21
+ debug : import . meta . env . DEV ,
20
22
fallbackLng : fallbackLng ,
21
23
interpolation : {
22
24
escapeValue : false ,
23
25
} ,
24
- resources : {
25
- en : { translation : en } ,
26
- zh : { translation : zh } ,
27
- } ,
28
26
} ) ;
29
27
30
28
export function getLabel ( label : string | { [ key : string ] : string } ) {
You can’t perform that action at this time.
0 commit comments