File tree Expand file tree Collapse file tree 3 files changed +13
-8
lines changed Expand file tree Collapse file tree 3 files changed +13
-8
lines changed Original file line number Diff line number Diff line change @@ -7,18 +7,23 @@ export const LanguageProvider = ({ children }) => {
77 const [ translations , setTranslations ] = useState ( { } ) ;
88
99 useEffect ( ( ) => {
10- // 加载翻译文件
10+ // 使用动态导入加载翻译文件
1111 const loadTranslations = async ( ) => {
1212 try {
13- const response = await fetch ( `/locales/${ language } .json` ) ;
14- if ( response . ok ) {
15- const translationData = await response . json ( ) ;
16- setTranslations ( translationData ) ;
17- } else {
18- console . warn ( `Failed to load translations for ${ language } ` ) ;
19- }
13+ const translationModule = await import ( `../locales/${ language } .json` ) ;
14+ setTranslations ( translationModule . default ) ;
2015 } catch ( error ) {
2116 console . error ( 'Error loading translations:' , error ) ;
17+ // 尝试加载备用语言或默认的空对象
18+ try {
19+ const fallbackLang = language === 'zh' ? 'en' : 'zh' ;
20+ const fallbackModule = await import ( `../locales/${ fallbackLang } .json` ) ;
21+ setTranslations ( fallbackModule . default ) ;
22+ console . warn ( `Loaded fallback translations for ${ fallbackLang } ` ) ;
23+ } catch ( fallbackError ) {
24+ console . error ( 'Failed to load fallback translations:' , fallbackError ) ;
25+ setTranslations ( { } ) ;
26+ }
2227 }
2328 } ;
2429
File renamed without changes.
File renamed without changes.
You can’t perform that action at this time.
0 commit comments