Skip to content

Commit

Permalink
perf: i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed Nov 25, 2024
1 parent e66e3a7 commit 784689a
Show file tree
Hide file tree
Showing 24 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"i18n-ally.keystyle": "flat",
"i18n-ally.sortKeys": true,
"i18n-ally.keepFulfilled": false,
"i18n-ally.sourceLanguage": "zh", // 根据此语言文件翻译其他语言文件的变量和内容
"i18n-ally.displayLanguage": "zh", // 显示语言
"i18n-ally.sourceLanguage": "zh-CN", // 根据此语言文件翻译其他语言文件的变量和内容
"i18n-ally.displayLanguage": "zh-CN", // 显示语言
"i18n-ally.namespace": true,
"i18n-ally.pathMatcher": "{locale}/{namespaces}.json",
"i18n-ally.extract.targetPickingStrategy": "most-similar-by-key",
Expand Down
13 changes: 8 additions & 5 deletions packages/web/hooks/useI18n.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Cookies, { CookieAttributes } from 'js-cookie';
import { useTranslation } from 'next-i18next';
import { LangEnum } from '../../../projects/app/src/web/common/utils/i18n';

const setCookie = (key: string, value: string, options?: CookieAttributes) => {
Cookies.set(key, value, options);
Expand All @@ -13,11 +14,13 @@ const LANG_KEY = 'NEXT_LOCALE';
export const useI18nLng = () => {
const { i18n } = useTranslation();
const languageMap: Record<string, string> = {
zh: 'zh',
'zh-CN': 'zh',
'zh-Hans': 'zh',
en: 'en',
'en-US': 'en'
zh: LangEnum.zh_CN,
'zh-CN': LangEnum.zh_CN,
'zh-Hans': LangEnum.zh_CN,
'zh-HK': LangEnum.zh_CN,
'zh-TW': LangEnum.zh_TW,
en: LangEnum.en,
'en-US': LangEnum.en
};

const onChangeLng = (lng: string) => {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 9 additions & 9 deletions packages/web/types/i18next.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'i18next';
import common from '../i18n/zh/common.json';
import dataset from '../i18n/zh/dataset.json';
import app from '../i18n/zh/app.json';
import file from '../i18n/zh/file.json';
import publish from '../i18n/zh/publish.json';
import workflow from '../i18n/zh/workflow.json';
import user from '../i18n/zh/user.json';
import chat from '../i18n/zh/chat.json';
import login from '../i18n/zh/login.json';
import common from '../i18n/zh-CN/common.json';
import dataset from '../i18n/zh-CN/dataset.json';
import app from '../i18n/zh-CN/app.json';
import file from '../i18n/zh-CN/file.json';
import publish from '../i18n/zh-CN/publish.json';
import workflow from '../i18n/zh-CN/workflow.json';
import user from '../i18n/zh-CN/user.json';
import chat from '../i18n/zh-CN/chat.json';
import login from '../i18n/zh-CN/login.json';

export interface I18nNamespaces {
common: typeof common;
Expand Down
4 changes: 2 additions & 2 deletions projects/app/next-i18next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

module.exports = {
i18n: {
defaultLocale: 'zh',
locales: ['en', 'zh', 'zh_TW'],
defaultLocale: 'zh-CN',
locales: ['en', 'zh-CN', 'zh-TW'],
localeDetection: false
},
localePath:
Expand Down
4 changes: 2 additions & 2 deletions projects/app/src/components/Select/I18nLngSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { langMap } from '@/web/common/utils/i18n';
import { Avatar, Box, Flex } from '@chakra-ui/react';
import { Box, Flex } from '@chakra-ui/react';
import MySelect from '@fastgpt/web/components/common/MySelect';
import { useI18nLng } from '@fastgpt/web/hooks/useI18n';
import { useTranslation } from 'next-i18next';
Expand All @@ -14,7 +14,7 @@ const I18nLngSelector = () => {
return Object.entries(langMap).map(([key, lang]) => ({
label: (
<Flex alignItems={'center'}>
<MyIcon borderRadius={'0'} mr={2} name={lang.avatar as any} w={'14px'} h={'9px'} />
<MyIcon borderRadius={'0'} mr={2} name={lang.avatar as any} w={'1rem'} />
<Box>{lang.label}</Box>
</Flex>
),
Expand Down
11 changes: 7 additions & 4 deletions projects/app/src/web/common/utils/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { I18nNsType } from '@fastgpt/web/types/i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';

export enum LangEnum {
'zh' = 'zh',
'zh_CN' = 'zh-CN',
'zh_TW' = 'zh-TW',
'en' = 'en'
}
export const langMap = {
[LangEnum.en]: {
label: 'English(US)',
icon: 'common/language/en',
avatar: 'common/language/America'
},
[LangEnum.zh]: {
[LangEnum.zh_CN]: {
label: '简体中文',
icon: 'common/language/zh',
avatar: 'common/language/China'
},
[LangEnum.zh_TW]: {
label: '中文(台湾)',
avatar: 'common/language/China'
}
};
Expand Down

0 comments on commit 784689a

Please sign in to comment.