From a6b309c089da88a13d83615ce53d3739bc63ef71 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 18 Oct 2021 20:05:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BA=E6=8F=92=E4=BB=B6=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E7=BF=BB=E8=AF=91=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared/i18n/index.ts | 12 ++++++++++++ shared/index.tsx | 2 +- web/plugins/com.msgbyte.webview/tsconfig.json | 1 - web/src/plugin/common/index.ts | 6 +++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/shared/i18n/index.ts b/shared/i18n/index.ts index 6ed489ca..b25f7675 100644 --- a/shared/i18n/index.ts +++ b/shared/i18n/index.ts @@ -58,6 +58,18 @@ export const t: TFunction = ( } }; +/** + * 本地翻译 + * @example + * localTrans({zh-CN: '你好', en-US: 'Hello'}); + * + * @param trans 翻译对象 + */ +export function localTrans(trans: Record) { + const lang = i18next.language as AllowedLanguage; + return trans[lang] ?? trans['zh-CN'] ?? trans['en-US']; +} + /** * 设置i18next的语言 */ diff --git a/shared/index.tsx b/shared/index.tsx index 826460ca..f560671d 100644 --- a/shared/index.tsx +++ b/shared/index.tsx @@ -43,7 +43,7 @@ export { useColorScheme } from './contexts/ColorSchemeContext'; export { getDMConverseName } from './helper/converse-helper'; // i18n -export { t, setLanguage, useTranslation } from './i18n'; +export { t, localTrans, setLanguage, useTranslation } from './i18n'; export { Trans } from './i18n/Trans'; export { useLanguage } from './i18n/language'; diff --git a/web/plugins/com.msgbyte.webview/tsconfig.json b/web/plugins/com.msgbyte.webview/tsconfig.json index beb3c60f..465a28b5 100644 --- a/web/plugins/com.msgbyte.webview/tsconfig.json +++ b/web/plugins/com.msgbyte.webview/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "rootDir": "./src", "baseUrl": "./src", "esModuleInterop": true, "jsx": "react", diff --git a/web/src/plugin/common/index.ts b/web/src/plugin/common/index.ts index 0d377b49..3b0eceb4 100644 --- a/web/src/plugin/common/index.ts +++ b/web/src/plugin/common/index.ts @@ -10,4 +10,8 @@ export { } from '@/routes/Main/Content/Group/utils'; export { openModal, ModalWrapper, useModalContext } from '@/components/Modal'; export { getGlobalState } from '@/utils/global-state-helper'; -export { getCachedUserInfo, getCachedConverseInfo } from 'tailchat-shared'; +export { + getCachedUserInfo, + getCachedConverseInfo, + localTrans, +} from 'tailchat-shared';