Skip to content

Commit 295e386

Browse files
fix: use string placeholders for i18n to survive JSON serialization
1 parent 02e60b7 commit 295e386

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

src/i18n/translations.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ export const locales: { code: Locale; native: string }[] = [
99

1010
export const defaultLocale: Locale = "en";
1111

12-
export const translations: Record<Locale, Record<string, string | ((...args: string[]) => string)>> = {
12+
export const translations: Record<Locale, Record<string, string>> = {
1313
en: {
1414
toggleTheme: "Toggle theme",
1515
language: "Language",
1616
copyColorCode: "Copy color code",
17-
copiedColor: (color) => `Copied color ${color}`,
17+
copiedColor: "Copied color {0}",
1818
clipboardDenied: "Clipboard permission denied. Please copy manually.",
19-
sizeSetTo: (size) => `Export size set to ${size}px`,
20-
pngExported: (size) => `PNG exported at ${size}x${size}`,
21-
svgExported: (size) => `SVG exported at ${size}x${size}`,
19+
sizeSetTo: "Export size set to {0}px",
20+
pngExported: "PNG exported at {0}x{0}",
21+
svgExported: "SVG exported at {0}x{0}",
2222
canvasError: "Unable to initialize canvas for PNG export.",
2323
renderError: "Failed to render image for PNG export.",
2424
svgExportFailed: "SVG export failed.",
@@ -41,11 +41,11 @@ export const translations: Record<Locale, Record<string, string | ((...args: str
4141
toggleTheme: "切换主题",
4242
language: "语言",
4343
copyColorCode: "复制颜色代码",
44-
copiedColor: (color) => `已复制颜色 ${color}`,
44+
copiedColor: "已复制颜色 {0}",
4545
clipboardDenied: "剪贴板权限被拒绝,请手动复制。",
46-
sizeSetTo: (size) => `导出尺寸已设为 ${size}px`,
47-
pngExported: (size) => `PNG 已导出,尺寸 ${size}x${size}`,
48-
svgExported: (size) => `SVG 已导出,尺寸 ${size}x${size}`,
46+
sizeSetTo: "导出尺寸已设为 {0}px",
47+
pngExported: "PNG 已导出,尺寸 {0}x{0}",
48+
svgExported: "SVG 已导出,尺寸 {0}x{0}",
4949
canvasError: "无法初始化画布以导出 PNG。",
5050
renderError: "渲染图像以导出 PNG 失败。",
5151
svgExportFailed: "SVG 导出失败。",
@@ -68,11 +68,11 @@ export const translations: Record<Locale, Record<string, string | ((...args: str
6868
toggleTheme: "テーマ切り替え",
6969
language: "言語",
7070
copyColorCode: "カラーコードをコピー",
71-
copiedColor: (color) => `カラー ${color} をコピーしました`,
71+
copiedColor: "カラー {0} をコピーしました",
7272
clipboardDenied: "クリップボードの権限が拒否されました。手動でコピーしてください。",
73-
sizeSetTo: (size) => `エクスポートサイズを ${size}px に設定しました`,
74-
pngExported: (size) => `PNG を ${size}x${size} でエクスポートしました`,
75-
svgExported: (size) => `SVG を ${size}x${size} でエクスポートしました`,
73+
sizeSetTo: "エクスポートサイズを {0}px に設定しました",
74+
pngExported: "PNG を {0}x{0} でエクスポートしました",
75+
svgExported: "SVG を {0}x{0} でエクスポートしました",
7676
canvasError: "PNG エクスポート用のキャンバスを初期化できません。",
7777
renderError: "PNG エクスポート用の画像のレンダリングに失敗しました。",
7878
svgExportFailed: "SVG のエクスポートに失敗しました。",
@@ -89,17 +89,17 @@ export const translations: Record<Locale, Record<string, string | ((...args: str
8989
sizeRange: "64 ~ 4096、64 単位",
9090
exportPng: "PNG エクスポート",
9191
exportSvg: "SVG エクスポート",
92-
footer: '&copy; Matsuzaka Yuki &middot; <a href="https://github.com/matsuzaka-yuki" target="_blank" rel="noopener noreferrer">GitHub</a> &middot; <a href="https://github.com/LyraVoid/APatch-Image-WebUI" target="_blank" rel="noopener noreferrer">APatch-Image-WebUI</a> に基づく',
92+
footer: '&copy; Matsuzaka Yuki &middot; <a href="https://github.com/matsuzaka-yuki" target="_blank" rel="noopener noreferrer">GitHub</a> &middot; <a href="https://github.com/LyraVoid/APatch-Image-WebUI" target="__blank" rel="noopener noreferrer">APatch-Image-WebUI</a> に基づく',
9393
},
9494
"zh-TW": {
9595
toggleTheme: "切換主題",
9696
language: "語言",
9797
copyColorCode: "複製顏色代碼",
98-
copiedColor: (color) => `已複製顏色 ${color}`,
98+
copiedColor: "已複製顏色 {0}",
9999
clipboardDenied: "剪貼簿權限被拒絕,請手動複製。",
100-
sizeSetTo: (size) => `匯出尺寸已設為 ${size}px`,
101-
pngExported: (size) => `PNG 已匯出,尺寸 ${size}x${size}`,
102-
svgExported: (size) => `SVG 已匯出,尺寸 ${size}x${size}`,
100+
sizeSetTo: "匯出尺寸已設為 {0}px",
101+
pngExported: "PNG 已匯出,尺寸 {0}x{0}",
102+
svgExported: "SVG 已匯出,尺寸 {0}x{0}",
103103
canvasError: "無法初始化畫布以匯出 PNG。",
104104
renderError: "渲染圖像以匯出 PNG 失敗。",
105105
svgExportFailed: "SVG 匯出失敗。",

src/pages/index.astro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,11 @@ import { locales, defaultLocale, translations as i18nStrings } from "../i18n/tra
362362
if (!supportedLocales.includes(currentLocale)) currentLocale = "en";
363363

364364
function tr(key, ...args) {
365-
const fn = translations[currentLocale]?.[key];
366-
if (typeof fn === "function") return fn(...args);
367-
return fn || key;
365+
let text = translations[currentLocale]?.[key] || translations["en"]?.[key] || key;
366+
args.forEach((arg, i) => {
367+
text = text.replace(new RegExp(`\\{${i}\\}`, "g"), arg);
368+
});
369+
return text;
368370
}
369371

370372
function applyTranslations() {

0 commit comments

Comments
 (0)