fix: i18n fallback

This commit is contained in:
Junyan Qin
2025-08-17 11:43:38 +08:00
parent 0ea7609ff1
commit 17d997c88e
2 changed files with 27 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
export interface I18nObject {
en_US: string;
zh_Hans: string;
zh_Hant?: string;
ja_JP?: string;
}

View File

@@ -3,6 +3,7 @@
import { ReactNode } from 'react';
import '@/i18n';
import { I18nObject } from '@/app/infra/entities/common';
import i18n from 'i18next';
interface I18nProviderProps {
children: ReactNode;
@@ -11,10 +12,28 @@ interface I18nProviderProps {
export default function I18nProvider({ children }: I18nProviderProps) {
return <>{children}</>;
}
export function extractI18nObject(i18nLabel: I18nObject): string {
const language = localStorage.getItem('langbot_language');
if ((language === 'zh-Hans' && i18nLabel.zh_Hans) || !i18nLabel.en_US) {
return i18nLabel.zh_Hans;
}
return i18nLabel.en_US;
}
// export function extractI18nObject(i18nLabel: I18nObject): string {
// const language = localStorage.getItem('langbot_language');
// if ((language === 'zh-Hans' && i18nLabel.zh_Hans) || !i18nLabel.en_US) {
// return i18nLabel.zh_Hans;
// }
// return i18nLabel.en_US;
// }
export const extractI18nObject = (i18nObject: I18nObject): string => {
// 根据当前语言返回对应的值, fallback优先级en_US、zh_Hans、zh_Hant、ja_JP
const language = i18n.language.replace('-', '_');
console.log('language:', language);
console.log('i18nObject:', i18nObject);
if (language === 'en_US' && i18nObject.en_US) return i18nObject.en_US;
if (language === 'zh_Hans' && i18nObject.zh_Hans) return i18nObject.zh_Hans;
if (language === 'zh_Hant' && i18nObject.zh_Hant) return i18nObject.zh_Hant;
if (language === 'ja_JP' && i18nObject.ja_JP) return i18nObject.ja_JP;
return (
i18nObject.en_US ||
i18nObject.zh_Hans ||
i18nObject.zh_Hant ||
i18nObject.ja_JP ||
''
);
};