Skip to content

Commit bf7da04

Browse files
committed
feat: language indicator
Signed-off-by: Innei <i@innei.in>
1 parent d81c2b5 commit bf7da04

File tree

17 files changed

+136
-83
lines changed

17 files changed

+136
-83
lines changed

configs/i18n-completeness.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import fs from "node:fs"
2+
import path from "node:path"
3+
4+
type LanguageCompletion = Record<string, number>
5+
6+
function getLanguageFiles(dir: string): string[] {
7+
return fs.readdirSync(dir).filter((file) => file.endsWith(".json"))
8+
}
9+
10+
function getNamespaces(localesDir: string): string[] {
11+
return fs
12+
.readdirSync(localesDir)
13+
.filter((file) => fs.statSync(path.join(localesDir, file)).isDirectory())
14+
}
15+
16+
function countKeys(obj: any): number {
17+
let count = 0
18+
for (const key in obj) {
19+
if (typeof obj[key] === "object") {
20+
count += countKeys(obj[key])
21+
} else {
22+
count++
23+
}
24+
}
25+
return count
26+
}
27+
28+
function calculateCompleteness(localesDir: string): LanguageCompletion {
29+
const namespaces = getNamespaces(localesDir)
30+
const languages = new Set<string>()
31+
const keyCount: Record<string, number> = {}
32+
33+
namespaces.forEach((namespace) => {
34+
const namespaceDir = path.join(localesDir, namespace)
35+
const files = getLanguageFiles(namespaceDir)
36+
37+
files.forEach((file) => {
38+
const lang = path.basename(file, ".json")
39+
languages.add(lang)
40+
41+
const content = JSON.parse(fs.readFileSync(path.join(namespaceDir, file), "utf-8"))
42+
keyCount[lang] = (keyCount[lang] || 0) + countKeys(content)
43+
})
44+
})
45+
46+
const enCount = keyCount["en"] || 0
47+
const completeness: LanguageCompletion = {}
48+
49+
languages.forEach((lang) => {
50+
if (lang !== "en") {
51+
const percent = Math.round((keyCount[lang] / enCount) * 100)
52+
completeness[lang] = percent
53+
}
54+
})
55+
56+
return completeness
57+
}
58+
59+
const i18n = calculateCompleteness(path.resolve(__dirname, "../locales"))
60+
export default i18n

configs/vite.render.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { prerelease } from "semver"
1111
import type { Plugin, UserConfig } from "vite"
1212

1313
import { getGitHash } from "../scripts/lib"
14+
import i18nCompleteness from "./i18n-completeness"
1415

1516
const pkg = JSON.parse(readFileSync("package.json", "utf8"))
1617
const isCI = process.env.CI === "true" || process.env.CI === "1"
@@ -134,6 +135,8 @@ export const viteRenderBaseConfig = {
134135
RELEASE_CHANNEL: JSON.stringify((prerelease(pkg.version)?.[0] as string) || "stable"),
135136

136137
DEBUG: process.env.DEBUG === "true",
138+
139+
I18N_COMPLETENESS_MAP: JSON.stringify({ ...i18nCompleteness, en: 100 }),
137140
},
138141
} satisfies UserConfig
139142

locales/app/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"ai_daily.title": "Top News - {{title}}",
33
"ai_daily.tooltip.content": "Here is news selected by AI from your timeline (<From /> - <To />) that may be important to you.",
44
"ai_daily.tooltip.update_schedule": "Update daily at 8 AM and 8 PM.",
5+
"app.app_name": "APP_NAME",
6+
"app.copy_logo_svg": "Copy Logo SVG",
7+
"app.toggle_sidebar": "Toggle Sidebar",
58
"discover.any_url_or_keyword": "Any URL or Keyword",
69
"discover.default_option": " (default)",
710
"discover.feed_description": "The description of this feed is as follows, and you can fill out the parameter form with the relevant information.",

locales/app/zh-CN.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"ai_daily.title": "头条 - {{title}}",
33
"ai_daily.tooltip.content": "这里是通过 AI 从您的时间线中选择的头条新闻(<From /> - <To />),可能对您很重要。",
44
"ai_daily.tooltip.update_schedule": "每天早上 8 点、晚上 8 点更新。",
5+
"app.app_name": "APP_NAME",
6+
"app.copy_logo_svg": "复制 Logo SVG",
7+
"app.toggle_sidebar": "切换侧边栏",
58
"discover.any_url_or_keyword": "任何 URL 或关键词",
69
"discover.default_option": " (默认)",
710
"discover.feed_description": "此 Feed 的描述如下,您可以填写相关信息。",

locales/common/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"app.copied_to_clipboard": "Copied to clipboard",
23
"cancel": "Cancel",
34
"confirm": "Confirm",
45
"ok": "OK",

locales/common/ja.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"app.copied_to_clipboard": "クリップボードにコピーしました",
3+
"cancel": "キャンセル",
4+
"confirm": "確認",
5+
"ok": "OK",
6+
"quantifier.piece": "",
7+
"time.last_night": "昨夜",
8+
"time.the_night_before_last": "一昨夜",
9+
"time.today": "今日",
10+
"time.yesterday": "昨日",
11+
"tips.load-lng-error": "言語パックの読み込みに失敗しました",
12+
"words.back": "戻る",
13+
"words.copy": "コピー",
14+
"words.edit": "編集",
15+
"words.entry": "エントリー",
16+
"words.id": "ID",
17+
"words.items_one": "アイテム",
18+
"words.items_other": "アイテム",
19+
"words.local": "ローカル",
20+
"words.record": "記録",
21+
"words.record_one": "記録",
22+
"words.record_other": "記録",
23+
"words.result": "結果",
24+
"words.result_one": "結果",
25+
"words.result_other": "結果",
26+
"words.space": "",
27+
"words.which.all": "すべて"
28+
}

locales/common/zh-CN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"app.copied_to_clipboard": "已复制到剪贴板",
23
"cancel": "取消",
34
"confirm": "确认",
45
"ok": "",

locales/lang/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"langs.en": "English",
3-
"langs.zh-CN": "简体中文(部分完成)",
3+
"langs.ja": "日本語",
4+
"langs.zh-CN": "简体中文",
45
"name": "English"
56
}

locales/lang/ja.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"langs.en": "English",
3+
"langs.ja": "日本語",
4+
"langs.zh-CN": "简体中文",
5+
"name": "日本語"
6+
}

locales/lang/zh-CN.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"langs.en": "English",
3-
"langs.zh-CN": "简体中文(部分完成)",
4-
"name": "English"
3+
"langs.ja": "日本語",
4+
"langs.zh-CN": "简体中文",
5+
"name": "简体中文"
56
}

0 commit comments

Comments
 (0)