feat: Update localization paths, add new files, settings, descriptions, generate TOC, modify imports/exports, define types (#11)

This commit introduces new localization paths, adds new localization files, adds new settings and descriptions, generates a table of contents, modifies file imports and exports, and defines types for localization resources.

Changes:
- Updated localization paths
- Added new localization files
- Added new settings and descriptions
- Generated a table of contents
- Modified file imports and exports
- Defined types for localization resources
This commit is contained in:
CanisMinor
2023-07-18 09:14:13 +08:00
committed by GitHub
parent 455a1f75f5
commit 579a0bf06f
22 changed files with 390 additions and 190 deletions

View File

@@ -3,9 +3,9 @@ const { defineConfig } = require('@lobehub/i18n-cli');
module.exports = defineConfig({
reference: description,
entry: 'public/locales/zh_CN',
entry: 'locales/zh_CN',
entryLocale: 'zh_CN',
output: 'public/locales',
output: 'locales',
outputLocales: ['en_US'],
splitToken: 2500,
temperature: 0,

40
locales/en_US/common.json Normal file
View File

@@ -0,0 +1,40 @@
{
"advanceSettings": "Advanced Settings",
"agentAvatar": "Avatar",
"agentDescription": "Description",
"agentDescriptionPlaceholder": "Please enter a description",
"agentModel": "Model",
"agentName": "Name",
"agentNamePlaceholder": "Please enter a name",
"agentProfile": "Agent Profile",
"agentPrompt": "AI Prompt",
"agentPromptPlaceholder": "Please enter AI prompt",
"agentTag": "Tag",
"agentTagPlaceholder": "Please enter a tag",
"archive": "Archive",
"autoGenerate": "Auto Generate",
"cancel": "Cancel",
"close": "Close",
"confirmRemoveSessionItemAlert": "You are about to remove this agent. Once removed, it cannot be recovered. Please confirm your action.",
"defaultAgent": "Default Agent",
"edit": "Edit",
"editAgentProfile": "Edit Agent Profile",
"export": "Export",
"gpt-3.5-turbo": "GPT 3.5 Turbo",
"gpt-3.5-turbo-16k": "GPT 3.5 Turbo (16K)",
"gpt-4": "GPT 4",
"gpt-4-32k": "GPT 4 (32K)",
"modelConfig": "Model Configuration",
"modelTemperature": "Temperature",
"newAgent": "New Agent",
"noDescription": "No description",
"ok": "OK",
"profile": "Profile",
"reset": "Reset",
"searchAgentPlaceholder": "Search agents and conversations...",
"sessionSetting": "Session Setting",
"setting": "Setting",
"share": "Share",
"updateAgent": "Update Agent",
"updatePrompt": "Update Prompt"
}

View File

@@ -0,0 +1,97 @@
{
"danger": {
"reset": {
"title": "Reset All Settings",
"desc": "Reset all settings to default values",
"action": "Reset Now",
"confirm": "Confirm reset all settings?",
"currentVersion": "Current Version"
},
"clear": {
"title": "Clear All Data",
"desc": "Clear all chat and settings data",
"action": "Clear Now",
"confirm": "Confirm clear all chat and settings data?"
}
},
"header": "Settings",
"settingChat": {
"title": "Chat Settings",
"inputTemplate": {
"title": "User Input Template",
"desc": "The latest user message will be filled into this template"
},
"compressThreshold": {
"title": "History Message Length Compression Threshold",
"desc": "When the uncompressed history message exceeds this value, it will be compressed"
},
"historyCount": {
"title": "Number of History Messages",
"desc": "Number of history messages carried in each request"
},
"maxTokens": {
"title": "Max Tokens per Response",
"desc": "The maximum number of tokens used for each interaction"
},
"sendKey": {
"title": "Send Key"
}
},
"settingModel": {
"title": "Model Settings",
"model": {
"title": "Model"
},
"temperature": {
"title": "Randomness (temperature)",
"desc": "The higher the value, the more random the response"
},
"topP": {
"title": "Nucleus Sampling (top_p)",
"desc": "Similar to randomness, but do not change it together with randomness"
},
"presencePenalty": {
"title": "Topic Freshness (presence_penalty)",
"desc": "The higher the value, the more likely it is to expand to new topics"
},
"frequencyPenalty": {
"title": "Frequency Penalty (frequency_penalty)",
"desc": "The higher the value, the more likely it is to reduce repeated words"
}
},
"settingOpenAI": {
"title": "OpenAI Settings",
"token": {
"title": "API Key",
"desc": "Use your own key to bypass password access restrictions",
"placeholder": "OpenAI API Key"
},
"endpoint": {
"title": "API Endpoint",
"desc": "In addition to the default address, it must include http(s)://"
}
},
"settingSystem": {
"title": "System Settings",
"accessCode": {
"title": "Access Code",
"desc": "Encryption access has been enabled by the administrator",
"placeholder": "Please enter the access code"
}
},
"settingTheme": {
"title": "Theme Settings",
"avatar": {
"title": "Avatar",
"desc": "Supports URL / Base64 / Emoji"
},
"fontSize": {
"title": "Font Size",
"desc": "Font size of chat content"
},
"lang": {
"name": "Language Settings",
"all": "All Languages"
}
}
}

40
locales/zh_CN/common.json Normal file
View File

@@ -0,0 +1,40 @@
{
"advanceSettings": "高级设置",
"agentAvatar": "头像",
"agentDescription": "描述",
"agentDescriptionPlaceholder": "请输入描述",
"agentModel": "模型",
"agentName": "名称",
"agentNamePlaceholder": "请输入名称",
"agentProfile": "助手信息",
"agentPrompt": "提示词",
"agentPromptPlaceholder": "请输入 AI 提示词",
"agentTag": "标签",
"agentTagPlaceholder": "请输入标签",
"archive": "归档",
"autoGenerate": "自动补全",
"cancel": "取消",
"close": "关闭",
"confirmRemoveSessionItemAlert": "即将删除该助手,删除后该将无法找回,请确认你的操作",
"defaultAgent": "默认助手",
"edit": "编辑",
"editAgentProfile": "编辑助手信息",
"export": "导出",
"gpt-3.5-turbo": "GPT 3.5",
"gpt-3.5-turbo-16k": "GPT 3.5 (16K)",
"gpt-4": "GPT 4",
"gpt-4-32k": "GPT 4 (32K)",
"modelConfig": "模型配置",
"modelTemperature": "发散度",
"newAgent": "新建助手",
"noDescription": "暂无描述",
"ok": "确定",
"profile": "身份卡",
"reset": "重置",
"searchAgentPlaceholder": "搜索助手和对话...",
"sessionSetting": "会话设置",
"setting": "设置",
"share": "分享",
"updateAgent": "更新助理信息",
"updatePrompt": "更新提示词"
}

View File

@@ -0,0 +1,98 @@
{
"danger": {
"reset": {
"title": "重置所有设置",
"desc": "重置所有设置项回默认值",
"action": "立即重置",
"confirm": "确认重置所有设置?",
"currentVersion": "当前版本"
},
"clear": {
"title": "清除所有数据",
"desc": "清除所有聊天、设置数据",
"action": "立即清除",
"confirm": "确认清除所有聊天、设置数据?"
}
},
"header": "设置",
"settingChat": {
"title": "聊天设置",
"inputTemplate": {
"title": "用户输入预处理",
"desc": "用户最新的一条消息会填充到此模板"
},
"compressThreshold": {
"title": "历史消息长度压缩阈值",
"desc": "当未压缩的历史消息超过该值时,将进行压缩"
},
"historyCount": {
"title": "附带历史消息数",
"desc": "每次请求携带的历史消息数"
},
"maxTokens": {
"title": "单次回复限制 (max_tokens)",
"desc": "单次交互所用的最大 Token 数"
},
"sendKey": {
"title": "发送键"
}
},
"settingModel": {
"title": "模型设置",
"model": {
"title": "模型"
},
"temperature": {
"title": "随机性 (temperature)",
"desc": "值越大,回复越随机"
},
"topP": {
"title": "核采样 (top_p)",
"desc": "与随机性类似,但不要和随机性一起更改"
},
"presencePenalty": {
"title": "话题新鲜度 (presence_penalty)",
"desc": "值越大,越有可能扩展到新话题"
},
"frequencyPenalty": {
"title": "频率惩罚度 (frequency_penalty)",
"desc": "值越大,越有可能降低重复字词"
}
},
"settingOpenAI": {
"title": "OpenAI 设置",
"token": {
"title": "API Key",
"desc": "使用自己的 Key 可绕过密码访问限制",
"placeholder": "OpenAI API Key"
},
"endpoint": {
"title": "接口地址",
"desc": "除默认地址外,必须包含 http(s)://"
}
},
"settingSystem": {
"title": "系统设置",
"accessCode": {
"title": "访问密码",
"desc": "管理员已开启加密访问",
"placeholder": "请输入访问密码"
}
},
"settingTheme": {
"title": "主题设置",
"avatar": {
"title": "头像",
"desc": "支持 URL / Base64 / Emoji 表情符号"
},
"fontSize": {
"title": "字体大小",
"desc": "聊天内容的字体大小"
},
"lang": {
"name": "语言设置",
"all": "所有语言"
}
}
}

View File

@@ -35,7 +35,7 @@
"test": "vitest --passWithNoTests",
"test:coverage": "vitest run --coverage --passWithNoTests",
"test:update": "vitest -u",
"toc": "i18next-resources-for-ts toc -i ./public/locales/zh_CN -o ./src/types/resources.ts",
"toc": "node scripts/toc.mjs",
"type-check": "tsc --noEmit"
},
"lint-staged": {

View File

@@ -0,0 +1,12 @@
import fs from 'node:fs';
import { resolve } from 'node:path';
import i18nConfig from '../.i18nrc.js';
import resources from '../src/locales/resources/zh_CN.ts';
for (const [ns, value] of Object.entries(resources)) {
fs.writeFileSync(
resolve(i18nConfig.output, i18nConfig.entryLocale, `${ns}.json`),
JSON.stringify(value, null, 2),
);
}

40
scripts/toc.mjs Normal file
View File

@@ -0,0 +1,40 @@
import { tocForResources } from 'i18next-resources-for-ts';
import fs from 'node:fs';
import { resolve } from 'node:path';
import i18nConfig from '../.i18nrc.js';
const locales = [i18nConfig.entryLocale, ...i18nConfig.outputLocales];
const RES_OUTPUT = 'src/locales/resources';
let index = '';
let indexObj = '';
locales.forEach((locale) => {
const files = fs
.readdirSync(resolve(i18nConfig.output, locale))
.filter((name) => name.includes('.json'));
index += `import ${locale} from "./${locale}";\n`;
indexObj += ` "${locale.replace('_', '-')}": ${locale},\n`;
const ns = [];
files.forEach((file) => {
ns.push({
name: file.replace('.json', ''),
path: resolve(i18nConfig.output, locale, file),
});
});
let toc = tocForResources(ns, resolve(RES_OUTPUT)).replaceAll('\\', '/');
if (locale === i18nConfig.entryLocale) {
toc = toc.replaceAll('.json', '').replaceAll('../../../locales/zh_CN', '../default');
}
fs.writeFileSync(resolve(RES_OUTPUT, `${locale}.ts`), toc);
});
fs.writeFileSync(
resolve(RES_OUTPUT, `index.ts`),
`${index}
export default {
${indexObj}};
`,
);

View File

@@ -1,32 +1,29 @@
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { isArray } from 'lodash-es';
import { initReactI18next } from 'react-i18next';
import { commonLocaleSet } from './namespaces';
import type { Namespaces, Resources } from '@/types/locale';
interface LocalSet {
['en-US']: Record<string, any>;
['zh-CN']: Record<string, any>;
}
import resources from './resources';
interface I18NOptions {
localSet: LocalSet;
namespace: string;
}
export const createI18nNext = (options: I18NOptions) => {
// 将语言包合并
const resources = {
'en-US': {
common: commonLocaleSet['en-US'],
[options.namespace]: options.localSet['en-US'],
},
'zh-CN': {
common: commonLocaleSet['zh-CN'],
[options.namespace]: options.localSet['zh-CN'],
},
};
const getRes = (res: Resources, namespace: Namespaces[]) => {
const newRes: any = {};
for (const [locale, value] of Object.entries(res)) {
newRes[locale] = {};
for (const ns of namespace) {
newRes[locale][ns] = value[ns];
}
}
return newRes;
};
export const createI18nNext = (namespace?: Namespaces[] | Namespaces) => {
const ns: Namespaces[] = namespace
? isArray(namespace)
? ['common', ...namespace]
: ['common', namespace]
: ['common'];
return (
i18n
// detect user language
@@ -37,14 +34,15 @@ export const createI18nNext = (options: I18NOptions) => {
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init({
// @ts-ignore
debug: process.env.NODE_ENV === 'development',
defaultNS: [options.namespace, 'common'],
defaultNS: ns,
fallbackLng: 'zh-CN',
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
},
ns: [options.namespace, 'common'],
resources,
ns,
resources: getRes(resources, ns),
})
);
};

View File

@@ -1,41 +0,0 @@
export default {
'advanceSettings': 'Advanced Settings',
'agentAvatar': 'Avatar',
'agentDescription': 'Description',
'agentDescriptionPlaceholder': 'Please enter a description',
'agentModel': 'Model',
'agentName': 'Name',
'agentNamePlaceholder': 'Please enter a name',
'agentProfile': 'Agent Profile',
'agentPrompt': 'AI Prompt',
'agentPromptPlaceholder': 'Please enter AI prompt',
'agentTag': 'Tag',
'agentTagPlaceholder': 'Please enter a tag',
'archive': 'Archive',
'autoGenerate': 'Auto Generate',
'cancel': 'Cancel',
'close': 'Close',
'confirmRemoveSessionItemAlert':
'You are about to remove this agent. Once removed, it cannot be recovered. Please confirm your action.',
'defaultAgent': 'Default Agent',
'edit': 'Edit',
'editAgentProfile': 'Edit Agent Profile',
'export': 'Export',
'gpt-3.5-turbo': 'GPT 3.5 Turbo',
'gpt-3.5-turbo-16k': 'GPT 3.5 Turbo (16K)',
'gpt-4': 'GPT 4',
'gpt-4-32k': 'GPT 4 (32K)',
'modelConfig': 'Model Configuration',
'modelTemperature': 'Temperature',
'newAgent': 'New Agent',
'noDescription': 'No description',
'ok': 'OK',
'profile': 'Profile',
'reset': 'Reset',
'searchAgentPlaceholder': 'Search agents and conversations...',
'sessionSetting': 'Session Setting',
'setting': 'Setting',
'share': 'Share',
'updateAgent': 'Update Agent',
'updatePrompt': 'Update Prompt',
};

View File

@@ -1,97 +0,0 @@
export default {
danger: {
clear: {
action: 'Clear Now',
confirm: 'Confirm clear all chat and settings data?',
desc: 'Clear all chat and settings data',
title: 'Clear All Data',
},
reset: {
action: 'Reset Now',
confirm: 'Confirm reset all settings?',
currentVersion: 'Current Version',
desc: 'Reset all settings to default values',
title: 'Reset All Settings',
},
},
header: 'Settings',
settingChat: {
compressThreshold: {
desc: 'When the uncompressed history message exceeds this value, it will be compressed',
title: 'History Message Length Compression Threshold',
},
historyCount: {
desc: 'Number of history messages carried in each request',
title: 'Number of History Messages',
},
inputTemplate: {
desc: 'The latest user message will be filled into this template',
title: 'User Input Template',
},
maxTokens: {
desc: 'The maximum number of tokens used for each interaction',
title: 'Max Tokens per Response',
},
sendKey: {
title: 'Send Key',
},
title: 'Chat Settings',
},
settingModel: {
frequencyPenalty: {
desc: 'The higher the value, the more likely it is to reduce repeated words',
title: 'Frequency Penalty (frequency_penalty)',
},
model: {
title: 'Model',
},
presencePenalty: {
desc: 'The higher the value, the more likely it is to expand to new topics',
title: 'Topic Freshness (presence_penalty)',
},
temperature: {
desc: 'The higher the value, the more random the response',
title: 'Randomness (temperature)',
},
title: 'Model Settings',
topP: {
desc: 'Similar to randomness, but do not change it together with randomness',
title: 'Nucleus Sampling (top_p)',
},
},
settingOpenAI: {
endpoint: {
desc: 'In addition to the default address, it must include http(s)://',
title: 'API Endpoint',
},
title: 'OpenAI Settings',
token: {
desc: 'Use your own key to bypass password access restrictions',
placeholder: 'OpenAI API Key',
title: 'API Key',
},
},
settingSystem: {
accessCode: {
desc: 'Encryption access has been enabled by the administrator',
placeholder: 'Please enter the access code',
title: 'Access Code',
},
title: 'System Settings',
},
settingTheme: {
avatar: {
desc: 'Supports URL / Base64 / Emoji',
title: 'Avatar',
},
fontSize: {
desc: 'Font size of chat content',
title: 'Font Size',
},
lang: {
all: 'All Languages',
name: 'Language Settings',
},
title: 'Theme Settings',
},
};

View File

@@ -1,6 +1,5 @@
import { createI18nNext } from './create';
import { commonLocaleSet } from './namespaces';
const initI18n = createI18nNext({ localSet: commonLocaleSet, namespace: 'common' });
const initI18n = createI18nNext();
export default initI18n;

View File

@@ -1,16 +0,0 @@
import enCommon from './en_US/common';
import settingEN from './en_US/setting';
import zhCommon from './zh_CN/common';
import settingZH from './zh_CN/setting';
export const commonLocaleSet = {
'en-US': enCommon,
'zh-CN': zhCommon,
};
export const settingsLocaleSet = {
'en-US': settingEN,
'zh-CN': settingZH,
};
export type Language = 'zh-CN' | 'en-US';

View File

@@ -0,0 +1,9 @@
import common from '../../../locales/en_US/common.json';
import setting from '../../../locales/en_US/setting.json';
const resources = {
common,
setting,
} as const;
export default resources;

View File

@@ -0,0 +1,7 @@
import en_US from './en_US';
import zh_CN from './zh_CN';
export default {
'en-US': en_US,
'zh-CN': zh_CN,
};

View File

@@ -0,0 +1,9 @@
import common from '../default/common';
import setting from '../default/setting';
const resources = {
common,
setting,
} as const;
export default resources;

View File

@@ -4,14 +4,13 @@ import { Flexbox } from 'react-layout-kit';
import { shallow } from 'zustand/shallow';
import { createI18nNext } from '@/locales/create';
import { commonLocaleSet } from '@/locales/namespaces';
import { useSessionStore } from '@/store/session';
import { useSettings } from '@/store/settings';
import Sidebar from '../Sidebar';
import { Sessions } from './SessionList';
const initI18n = createI18nNext({ localSet: commonLocaleSet, namespace: 'common' });
const initI18n = createI18nNext();
const ChatLayout = memo<PropsWithChildren>(({ children }) => {
useEffect(() => {

View File

@@ -4,14 +4,13 @@ import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';
import { createI18nNext } from '@/locales/create';
import { settingsLocaleSet } from '@/locales/namespaces';
import { Sessions } from '@/pages/chat/SessionList';
import Sidebar from '../Sidebar';
import Header from './Header';
import SettingForm from './SettingForm';
const initI18n = createI18nNext({ localSet: settingsLocaleSet, namespace: 'setting' });
const initI18n = createI18nNext('setting');
const SettingLayout = memo(() => {
const { t } = useTranslation('setting');

View File

@@ -1,8 +1,8 @@
import resources from '@/types/resources';
import { DefaultResources } from '@/types/locale';
declare module 'i18next' {
interface CustomTypeOptions {
defaultNS: 'common';
resources: typeof resources;
resources: DefaultResources;
}
}

7
src/types/locale.ts Normal file
View File

@@ -0,0 +1,7 @@
import resources from '@/locales/resources';
import defaultResources from '@/locales/resources/zh_CN';
export type Resources = typeof resources;
export type DefaultResources = typeof defaultResources;
export type Namespaces = keyof DefaultResources;
export type Locales = keyof Resources;