From c99603e756753f5b929e7fdea0164458bf38f05d Mon Sep 17 00:00:00 2001 From: Yizhuo <31850902+LCJove@users.noreply.github.com> Date: Mon, 18 Mar 2024 19:34:26 +0800 Subject: [PATCH] :globe_with_meridians: chore: add new locale (#131) Co-authored-by: gaoyizhuo.gyz --- src/ProChat/__test__/index.test.tsx | 7 +++++++ src/ProChat/components/ChatList/index.tsx | 19 +++++++++++-------- src/locale/{en-US/index.ts => en-US.ts} | 8 ++++++++ src/locale/index.ts | 3 ++- src/locale/{zh-CN/index.ts => zh-CN.ts} | 8 ++++++++ src/types/locale.ts | 16 ++++++++++++++++ 6 files changed, 52 insertions(+), 9 deletions(-) rename src/locale/{en-US/index.ts => en-US.ts} (69%) rename src/locale/{zh-CN/index.ts => zh-CN.ts} (63%) create mode 100644 src/types/locale.ts diff --git a/src/ProChat/__test__/index.test.tsx b/src/ProChat/__test__/index.test.tsx index 73804f5c..d27af85a 100644 --- a/src/ProChat/__test__/index.test.tsx +++ b/src/ProChat/__test__/index.test.tsx @@ -1,3 +1,4 @@ +import { gLocaleObject } from '@/locale'; import { render } from '@testing-library/react'; import { ProChat } from '..'; @@ -12,4 +13,10 @@ describe('ProChat', () => { ); expect(wrapper.getByText('RenderInputArea')).toBeInTheDocument(); }); + + it('i18n worked', () => { + const app = render(); + const text = gLocaleObject('en-US'); + expect(app.queryByPlaceholderText(text.placeholder)).toBeInTheDocument(); + }); }); diff --git a/src/ProChat/components/ChatList/index.tsx b/src/ProChat/components/ChatList/index.tsx index 71e460fa..77111ca8 100644 --- a/src/ProChat/components/ChatList/index.tsx +++ b/src/ProChat/components/ChatList/index.tsx @@ -7,6 +7,7 @@ import { chatSelectors } from '@/ProChat/store/selectors'; import { ChatListItemProps } from '@/ChatList/ChatListItem'; import { useRefFunction } from '@/ProChat/hooks/useRefFunction'; +import { gLocaleObject } from '@/locale'; import { renderActions } from './Actions'; import { renderMessagesExtra } from './Extras'; import { renderMessages } from './Messages'; @@ -20,6 +21,7 @@ interface ListProps extends Partial { const List = memo( ({ showTitle, itemShouldUpdate, chatItemRenderConfig, markdownProps }) => { const data = useStore(chatSelectors.currentChatsWithGuideMessage, isEqual); + const locale = useStore((s) => s.locale); const [ init, @@ -68,15 +70,16 @@ const List = memo( ); const textObj = useMemo(() => { + const localeObj = gLocaleObject(locale); return { - cancel: '取消', - confirm: '确认', - copy: '复制', - copySuccess: '复制成功', - delete: '删除', - edit: '编辑', - history: '历史范围', - regenerate: '重新生成', + cancel: localeObj.cancel, + confirm: localeObj.confirm, + copy: localeObj.copy, + copySuccess: localeObj.copySuccess, + delete: localeObj.delete, + edit: localeObj.edit, + history: localeObj.history, + regenerate: localeObj.regenerate, }; }, []); if (!init) return ; diff --git a/src/locale/en-US/index.ts b/src/locale/en-US.ts similarity index 69% rename from src/locale/en-US/index.ts rename to src/locale/en-US.ts index d18a5b17..960d9d62 100644 --- a/src/locale/en-US/index.ts +++ b/src/locale/en-US.ts @@ -6,4 +6,12 @@ export default { clearModalTitle: 'You are about to clear the session, and you will not be able to retrieve it after clearing. Do you want to clear the current session?', defaultHelloMessage: 'Let us start chatting', + cancel: 'Cancel', + confirm: 'Confirm', + copy: 'Copy', + copySuccess: 'Copy Success', + delete: 'Delete', + edit: 'Edit', + history: 'History', + regenerate: 'Regenerate', }; diff --git a/src/locale/index.ts b/src/locale/index.ts index ae5eb69c..a8b9526f 100644 --- a/src/locale/index.ts +++ b/src/locale/index.ts @@ -1,3 +1,4 @@ +import { LocaleProps } from '@/types/locale'; import enUSLocal from './en-US'; import zhCNLocal from './zh-CN'; export type Locale = 'zh-CN' | 'en-US'; @@ -7,6 +8,6 @@ const locales = { 'zh-CN': zhCNLocal, }; -export const gLocaleObject = (glocale: Locale): Record => { +export const gLocaleObject = (glocale: Locale): LocaleProps => { return locales[glocale as 'zh-CN'] || locales['zh-CN']; }; diff --git a/src/locale/zh-CN/index.ts b/src/locale/zh-CN.ts similarity index 63% rename from src/locale/zh-CN/index.ts rename to src/locale/zh-CN.ts index 0f2814af..10580e9e 100644 --- a/src/locale/zh-CN/index.ts +++ b/src/locale/zh-CN.ts @@ -5,4 +5,12 @@ export default { clearDialogue: '清空对话', clearModalTitle: '你即将要清空会话,清空后将无法找回。是否清空当前会话?', defaultHelloMessage: '让我们开始对话吧', + cancel: '取消', + confirm: '确认', + copy: '复制', + copySuccess: '复制成功', + delete: '删除', + edit: '编辑', + history: '历史范围', + regenerate: '重新生成', }; diff --git a/src/types/locale.ts b/src/types/locale.ts new file mode 100644 index 00000000..e3dbf4c9 --- /dev/null +++ b/src/types/locale.ts @@ -0,0 +1,16 @@ +export interface LocaleProps { + placeholder: string; + backToBottom: string; + clearCurrentDialogue: string; + clearDialogue: string; + clearModalTitle: string; + defaultHelloMessage: string; + cancel: string; + confirm: string; + copy: string; + copySuccess: string; + delete: string; + edit: string; + history: string; + regenerate: string; +}