Skip to content

Commit

Permalink
🌐 chore: add new locale (#131)
Browse files Browse the repository at this point in the history
Co-authored-by: gaoyizhuo.gyz <[email protected]>
  • Loading branch information
LCJove and LCJove authored Mar 18, 2024
1 parent 1974a3b commit c99603e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/ProChat/__test__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { gLocaleObject } from '@/locale';
import { render } from '@testing-library/react';
import { ProChat } from '..';

Expand All @@ -12,4 +13,10 @@ describe('ProChat', () => {
);
expect(wrapper.getByText('RenderInputArea')).toBeInTheDocument();
});

it('i18n worked', () => {
const app = render(<ProChat locale="en-US" />);
const text = gLocaleObject('en-US');
expect(app.queryByPlaceholderText(text.placeholder)).toBeInTheDocument();
});
});
19 changes: 11 additions & 8 deletions src/ProChat/components/ChatList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,6 +21,7 @@ interface ListProps extends Partial<ChatListProps> {
const List = memo<ListProps>(
({ showTitle, itemShouldUpdate, chatItemRenderConfig, markdownProps }) => {
const data = useStore(chatSelectors.currentChatsWithGuideMessage, isEqual);
const locale = useStore((s) => s.locale);

const [
init,
Expand Down Expand Up @@ -68,15 +70,16 @@ const List = memo<ListProps>(
);

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 <SkeletonList />;
Expand Down
8 changes: 8 additions & 0 deletions src/locale/en-US/index.ts → src/locale/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
3 changes: 2 additions & 1 deletion src/locale/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -7,6 +8,6 @@ const locales = {
'zh-CN': zhCNLocal,
};

export const gLocaleObject = (glocale: Locale): Record<string, string> => {
export const gLocaleObject = (glocale: Locale): LocaleProps => {
return locales[glocale as 'zh-CN'] || locales['zh-CN'];
};
8 changes: 8 additions & 0 deletions src/locale/zh-CN/index.ts → src/locale/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ export default {
clearDialogue: '清空对话',
clearModalTitle: '你即将要清空会话,清空后将无法找回。是否清空当前会话?',
defaultHelloMessage: '让我们开始对话吧',
cancel: '取消',
confirm: '确认',
copy: '复制',
copySuccess: '复制成功',
delete: '删除',
edit: '编辑',
history: '历史范围',
regenerate: '重新生成',
};
16 changes: 16 additions & 0 deletions src/types/locale.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit c99603e

Please sign in to comment.