-
Notifications
You must be signed in to change notification settings - Fork 495
Update zh-cn.ts to 049b5bf #2419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,149 @@ | ||
| /** | ||
| * Language Pack Diff Checker Script | ||
| * | ||
| * Purpose: | ||
| * Compare src/lang/locale/en.ts and a target language file (e.g. zh-cn.ts), output missing and extra keys. | ||
| * Output is color-coded: | ||
| * - Green: Key exists in en.ts but missing in the target language file | ||
| * - Red: Key exists in the target language file but not in en.ts | ||
| * - Yellow: The same key exists in both files but the line numbers are different | ||
| * | ||
| * Dependencies: | ||
| * Node.js environment, no extra dependencies required. | ||
| * | ||
| * Usage: | ||
| * 1. Open a terminal in this file's directory (or project root). | ||
| * 2. Run: | ||
| * node src/lang/langDiffWithEn.js zh-cn | ||
| * # or node src/lang/langDiffWithEn.js ja | ||
| * # The argument is the language file name (without extension), default is zh-cn | ||
| * | ||
| * Output: | ||
| * + (green): Key exists in en.ts but missing in the target language file | ||
| * - (red): Key exists in the target language file but not in en.ts | ||
| * ! (yellow): The same key exists in both files but the line numbers are different | ||
| * If there is no output, the two files have identical keys. | ||
| */ | ||
| /** | ||
| * 语言包差异检测脚本 | ||
| * | ||
| * 用途: | ||
| * 比较 src/lang/locale/en.ts 与指定语言文件(如 zh-cn.ts)中的 key,输出缺失和多余的 key。 | ||
| * 输出内容会用不同颜色区分: | ||
| * - 绿色:en.ts 有但目标语言缺失的 key | ||
| * - 红色:目标语言有但 en.ts 没有的 key | ||
| * - 黄色:相同的 key 所在的行号不相同 | ||
| * | ||
| * 依赖: | ||
| * Node.js 环境,无需额外依赖。 | ||
| * | ||
| * 用法: | ||
| * 1. 在命令行进入本文件所在目录(或项目根目录)。 | ||
| * 2. 运行: | ||
| * node src/lang/langDiffWithEn.js zh-cn | ||
| * # 或 node src/lang/langDiffWithEn.js ja | ||
| * # 参数为语言文件名(不带扩展名),默认为 zh-cn | ||
| * | ||
| * 输出说明: | ||
| * + 开头(绿色):en.ts 有但目标语言缺失的 key | ||
| * - 开头(红色):目标语言有但 en.ts 没有的 key | ||
| * ! 开头(黄色):相同的 key 所在的行号不相同 | ||
| * 若无输出则表示两个文件 key 完全一致。 | ||
| */ | ||
|
|
||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| /** | ||
| * 读取 locale 文件夹下指定语言和 en.ts 文件,逐行解析键值和行号 | ||
| * @param lang 语言文件名(不含扩展名),如 zh-cn | ||
| * @returns { en: Record<string, {value: string, line: number}>, lang: Record<string, {value: string, line: number}> } | ||
| */ | ||
| function readLocaleFiles(lang = "zh-cn") { | ||
| // 构造 locale 目录路径 | ||
| const localeDir = path.join(__dirname, "./locale"); | ||
| // 获取 en.ts 文件路径 | ||
| const enFile = path.join(localeDir, "en.ts"); | ||
| // 获取指定语言文件路径 | ||
| const langFile = path.join(localeDir, `${lang}.ts`); | ||
| /** | ||
| * 解析指定的语言文件,提取每个 key 的值和所在行号 | ||
| * @param file 文件路径 | ||
| * @returns {Record<string, {value: string, line: number}>} | ||
| */ | ||
| const parse = (file) => { | ||
| // 读取文件内容并按行分割 | ||
| const lines = fs.readFileSync(file, "utf8").split(/\r?\n/); | ||
| const result = {}; | ||
| let inExport = false; | ||
| for (let i = 0; i < lines.length; i++) { | ||
| const line = lines[i]; | ||
| // 检查是否进入 export default 块 | ||
| if (!inExport && line.includes("export default")) { | ||
| inExport = true; | ||
| continue; | ||
| } | ||
| if (!inExport) continue; | ||
| // 匹配 key: "value", 格式的行 | ||
| const m = line.match(/^\s*([A-Z0-9_]+)\s*:\s*.*?,?$/); | ||
| if (m) { | ||
| let key = m[1]; | ||
| // 保存 key 及其所在行号 | ||
| result[key] = i + 1; | ||
| } | ||
| } | ||
| return result; | ||
| }; | ||
| // 返回 en 和指定语言的解析结果 | ||
| return { | ||
| en: parse(enFile), | ||
| lang: parse(langFile), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * 比较 en 和 lang 的 key/value,输出差异 | ||
| */ | ||
|
|
||
| function diffLang(lang = "zh-cn") { | ||
| const { en, lang: l } = readLocaleFiles(lang); | ||
| const allKeys = new Set([...Object.keys(en), ...Object.keys(l)]); | ||
| const diffs = []; | ||
| for (const key of allKeys) { | ||
| if (!(key in en)) { | ||
| diffs.push(`- ${key}: ${l[key]}`); | ||
| } else if (!(key in l)) { | ||
| diffs.push(`+ ${key}: ${en[key]}`); | ||
| } else { | ||
| if (en[key] !== l[key]) { | ||
| diffs.push(`! ${key}: en: ${en[key]} <--> lang: ${l[key]}`); | ||
| } | ||
| } | ||
| } | ||
| return diffs; | ||
| } | ||
|
|
||
| // CLI | ||
| if (require.main === module) { | ||
| const lang = process.argv[2] || "zh-cn"; | ||
| const diffs = diffLang(lang); | ||
| if (diffs.length === 0) { | ||
| console.log(`语言文件 ${lang} 与 en.ts 无差异`); | ||
| } else { | ||
| diffs.forEach((line) => { | ||
| if (line.startsWith("+")) { | ||
| // 绿色 | ||
| console.log("\x1b[32m%s\x1b[0m", line); | ||
| } else if (line.startsWith("-")) { | ||
| // 红色 | ||
| console.log("\x1b[31m%s\x1b[0m", line); | ||
| } else if (line.startsWith("!")) { | ||
| // 黄色 | ||
| console.log("\x1b[33m%s\x1b[0m", line); | ||
| } else { | ||
| // 默认 | ||
| console.log(line); | ||
| } | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 这类脚本更适合作为个人本地使用的小工具。
2 若确实想尝试提交,你可能最好把下面代码中的中文注释也改为英文。
3 此外,应当取消在
function readLocaleFiles()和function diffLang()设置默认变量值为zh-cn.ts,因在第 128 行已有默认自定义语言的入口。你可以在第 128 行这里添加一行注释,说明可以将此处改为其他语言的文件名。1 This type of script is more suitable as a small tool for personal local use.
2 If you really want to try submitting it, you'd better change the Chinese comments in the following code to English as well.
3 In addition, you should remove the default variable values set to
zh-cn.tsinfunction readLocaleFiles()andfunction diffLang(), because there is already an entry for the default custom language on line 128. You can add a comment on line 128 to indicate that this can be changed to the file name of other languages.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我还是不太推荐提交。别的不说,像正则表达式这种实际使用中经常需要调试修改的,如果每次调整都要等待作者审核,反而可能影响到你的工作效率。代码文件毕竟比不得翻译文本,确实存在一定的风险隐患,可能会添不少不必要的麻烦。
I still don't quite recommend submitting it. Not to mention, things like regular expressions often need to be debugged and modified in actual use. If you have to wait for the review every time you make an adjustment, it may actually affect your work efficiency. After all, code files are different from translated texts. There are indeed certain potential risks, which may cause quite a lot of unnecessary troubles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
写这个脚本我只是为了方便自己在行号无法对应的时候快速找到差异。提交上来是觉得有人可能会用得上,另外的原因是我保存在本地可能会丢失。我知道写的并不严谨,但是能用而且够用,并且在当前可能也只有我自己去使用这个脚本。我想不出有什么情况需要经常去调整里面的正则表达式,而且如果有需要,我肯定是在本地修改使用之后才提交修改,不会影响到我的工作效率
I wrote this script simply for my own convenience to quickly identify differences when line numbers don't match. I'm sharing it because I thought it might be useful to someone else, and also because keeping it locally could risk losing it. I know it's not written with strict rigor, but it's functional and sufficient, especially since, for now, I'm likely the only one using this script. I can't think of any situations where the regular expressions in it would need frequent adjustments, and if necessary, I would definitely modify and test it locally before submitting any changes, so it won't affect my work efficiency.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一旦提交,这就不只是你一个人的事了,作者需要审核你这个脚本。而且既然想要提交,说为了自己使用方便,难免不太合适。如果你确实希望其他人也能用上,就不能还用中文注释,更不能写不严谨的内容。
至于说保存在本地可能会丢失,我看到你有不少历史仓库,你应该是知道怎么用 GitHub 仓库进行保存的呀。
Once submitted, this won't just be your own matter. The author needs to review your script. Moreover, since you want to submit it, it's inappropriate to use it simply for your own convenience. If you really hope that others can also use it, you can't keep using Chinese comments, and you definitely can't write content that is not rigorous.
As for the concern that it might be lost if saved locally, I noticed that you have quite a few historical repositories. You should know how to use a GitHub repository for storage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当然,每个人都有自己的选择。我没问题了。
Of course, everyone has their own choices. I have no more questions.