-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: Add i18n auto flow
pnpm run i18n
- Loading branch information
1 parent
04fd60d
commit e18fc57
Showing
12 changed files
with
150 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,97 @@ | ||
{ | ||
"danger": { | ||
"clear": { | ||
"action": "立即清除", | ||
"confirm": "确认清除所有聊天、设置数据?", | ||
"desc": "清除所有聊天、设置数据", | ||
"title": "清除所有数据" | ||
}, | ||
"reset": { | ||
"title": "重置所有设置", | ||
"desc": "重置所有设置项回默认值", | ||
"action": "立即重置", | ||
"confirm": "确认重置所有设置?", | ||
"currentVersion": "当前版本" | ||
}, | ||
"clear": { | ||
"title": "清除所有数据", | ||
"desc": "清除所有聊天、设置数据", | ||
"action": "立即清除", | ||
"confirm": "确认清除所有聊天、设置数据?" | ||
"currentVersion": "当前版本", | ||
"desc": "重置所有设置项回默认值", | ||
"title": "重置所有设置" | ||
} | ||
}, | ||
|
||
"header": "设置", | ||
"settingChat": { | ||
"title": "聊天设置", | ||
"inputTemplate": { | ||
"title": "用户输入预处理", | ||
"desc": "用户最新的一条消息会填充到此模板" | ||
}, | ||
"compressThreshold": { | ||
"title": "历史消息长度压缩阈值", | ||
"desc": "当未压缩的历史消息超过该值时,将进行压缩" | ||
"desc": "当未压缩的历史消息超过该值时,将进行压缩", | ||
"title": "历史消息长度压缩阈值" | ||
}, | ||
"historyCount": { | ||
"title": "附带历史消息数", | ||
"desc": "每次请求携带的历史消息数" | ||
"desc": "每次请求携带的历史消息数", | ||
"title": "附带历史消息数" | ||
}, | ||
"inputTemplate": { | ||
"desc": "用户最新的一条消息会填充到此模板", | ||
"title": "用户输入预处理" | ||
}, | ||
"maxTokens": { | ||
"title": "单次回复限制 (max_tokens)", | ||
"desc": "单次交互所用的最大 Token 数" | ||
"desc": "单次交互所用的最大 Token 数", | ||
"title": "单次回复限制 (max_tokens)" | ||
}, | ||
"sendKey": { | ||
"title": "发送键" | ||
} | ||
}, | ||
"title": "聊天设置" | ||
}, | ||
"settingModel": { | ||
"title": "模型设置", | ||
"frequencyPenalty": { | ||
"desc": "值越大,越有可能降低重复字词", | ||
"title": "频率惩罚度 (frequency_penalty)" | ||
}, | ||
"model": { | ||
"title": "模型" | ||
}, | ||
"presencePenalty": { | ||
"desc": "值越大,越有可能扩展到新话题", | ||
"title": "话题新鲜度 (presence_penalty)" | ||
}, | ||
"temperature": { | ||
"title": "随机性 (temperature)", | ||
"desc": "值越大,回复越随机" | ||
"desc": "值越大,回复越随机", | ||
"title": "随机性 (temperature)" | ||
}, | ||
"title": "模型设置", | ||
"topP": { | ||
"title": "核采样 (top_p)", | ||
"desc": "与随机性类似,但不要和随机性一起更改" | ||
}, | ||
"presencePenalty": { | ||
"title": "话题新鲜度 (presence_penalty)", | ||
"desc": "值越大,越有可能扩展到新话题" | ||
}, | ||
"frequencyPenalty": { | ||
"title": "频率惩罚度 (frequency_penalty)", | ||
"desc": "值越大,越有可能降低重复字词" | ||
"desc": "与随机性类似,但不要和随机性一起更改", | ||
"title": "核采样 (top_p)" | ||
} | ||
}, | ||
"settingOpenAI": { | ||
"endpoint": { | ||
"desc": "除默认地址外,必须包含 http(s)://", | ||
"title": "接口地址" | ||
}, | ||
"title": "OpenAI 设置", | ||
"token": { | ||
"title": "API Key", | ||
"desc": "使用自己的 Key 可绕过密码访问限制", | ||
"placeholder": "OpenAI API Key" | ||
}, | ||
"endpoint": { | ||
"title": "接口地址", | ||
"desc": "除默认地址外,必须包含 http(s)://" | ||
"placeholder": "OpenAI API Key", | ||
"title": "API Key" | ||
} | ||
}, | ||
"settingSystem": { | ||
"title": "系统设置", | ||
"accessCode": { | ||
"title": "访问密码", | ||
"desc": "管理员已开启加密访问", | ||
"placeholder": "请输入访问密码" | ||
} | ||
"placeholder": "请输入访问密码", | ||
"title": "访问密码" | ||
}, | ||
"title": "系统设置" | ||
}, | ||
"settingTheme": { | ||
"title": "主题设置", | ||
"avatar": { | ||
"title": "头像", | ||
"desc": "支持 URL / Base64 / Emoji 表情符号" | ||
"desc": "支持 URL / Base64 / Emoji 表情符号", | ||
"title": "头像" | ||
}, | ||
"fontSize": { | ||
"title": "字体大小", | ||
"desc": "聊天内容的字体大小" | ||
"desc": "聊天内容的字体大小", | ||
"title": "字体大小" | ||
}, | ||
"lang": { | ||
"name": "语言设置", | ||
"all": "所有语言" | ||
} | ||
"all": "所有语言", | ||
"name": "语言设置" | ||
}, | ||
"title": "主题设置" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { consola } from 'consola'; | ||
import { colors } from 'consola/utils'; | ||
import fs from 'node:fs'; | ||
import { resolve } from 'node:path'; | ||
|
||
import i18nConfig from '../.i18nrc'; | ||
|
||
export const genDefaultLocale = (input: string) => { | ||
consola.info(`Default locale is ${i18nConfig.entryLocale}...`); | ||
const resources = require(`../${input}/${i18nConfig.entryLocale}`); | ||
const data = Object.entries(resources.default); | ||
consola.start(`Generate default locale json, found ${data.length} namespaces...`); | ||
for (const [ns, value] of data) { | ||
const filepath = resolve(i18nConfig.output, i18nConfig.entryLocale, `${ns}.json`); | ||
fs.writeFileSync(filepath, JSON.stringify(value, null, 2)); | ||
consola.success(colors.bgWhiteBright(colors.black(` ${ns} `)), colors.gray(filepath)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { consola } from 'consola'; | ||
import { colors } from 'consola/utils'; | ||
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]; | ||
export const genResources = (output: string) => { | ||
let index = ''; | ||
let indexObj = ''; | ||
consola.start(`Generate locale resources and types, found ${locales.length} locales...`); | ||
for (const locale of locales) { | ||
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 = []; | ||
for (const file of files) { | ||
ns.push({ | ||
name: file.replace('.json', ''), | ||
path: resolve(i18nConfig.output, locale, file), | ||
}); | ||
} | ||
let toc = tocForResources(ns, resolve(output)).replaceAll('\\', '/'); | ||
if (locale === i18nConfig.entryLocale) { | ||
toc = toc.replaceAll('.json', '').replaceAll('../../../locales/zh_CN', '../default'); | ||
} | ||
const filepath = resolve(output, `${locale}.ts`); | ||
fs.writeFileSync(filepath, toc); | ||
consola.success(colors.bgBlue(colors.black(` ${locale} `)), colors.gray(filepath)); | ||
} | ||
const indexFilepath = resolve(output, `index.ts`); | ||
fs.writeFileSync( | ||
indexFilepath, | ||
`${index} | ||
const resources = { | ||
${indexObj}} as const; | ||
export default resources; | ||
export const defaultResources = ${i18nConfig.entryLocale}; | ||
export type Resources = typeof resources; | ||
export type DefaultResources = typeof defaultResources; | ||
export type Namespaces = keyof DefaultResources; | ||
export type Locales = keyof Resources; | ||
`, | ||
); | ||
|
||
consola.success(colors.bgGreen(colors.black(` INDEX `)), colors.gray(indexFilepath)); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { genDefaultLocale } from './genDefaultLocale'; | ||
import { genResources } from './genResources'; | ||
|
||
const RES_OUTPUT = 'src/locales/resources'; | ||
|
||
genDefaultLocale(RES_OUTPUT); | ||
genResources(RES_OUTPUT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import en_US from './en_US'; | ||
import zh_CN from './zh_CN'; | ||
|
||
export default { | ||
const resources = { | ||
'en-US': en_US, | ||
'zh-CN': zh_CN, | ||
}; | ||
} as const; | ||
export default resources; | ||
export const defaultResources = zh_CN; | ||
export type Resources = typeof resources; | ||
export type DefaultResources = typeof defaultResources; | ||
export type Namespaces = keyof DefaultResources; | ||
export type Locales = keyof Resources; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
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; | ||
export type * from '@/locales/resources'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters