docs/en/guides/i18n.md
[!WARNING] This document is machine translated from Chinese. While we strive for accuracy, there may be some imperfections in the translation.
i18n Ally is a powerful VSCode extension that provides real-time feedback during development, helping developers detect missing or incorrect translations earlier.
The plugin has already been configured in the project — simply install it to get started.
Never use flat structures like "add.button.tip": "Add". Instead, adopt a clear nested structure:
// Wrong - Flat structure
{
"add.button.tip": "Add",
"delete.button.tip": "Delete"
}
// Correct - Nested structure
{
"add": {
"button": {
"tip": "Add"
}
},
"delete": {
"button": {
"tip": "Delete"
}
}
}
t()We strongly advise against using template strings for dynamic interpolation. While convenient in general JavaScript development, they cause several issues in i18n scenarios.
Tools like i18n Ally cannot parse dynamic content within template strings, resulting in:
// Not recommended - Plugin cannot resolve
const message = t(`fruits.${fruit}`);
Template strings appear as raw code instead of the final translated text in IDEs, degrading the development experience.
Since the plugin cannot track such usages, developers must manually verify the existence of corresponding keys in language files.
To avoid missing keys, all dynamically translated texts should first maintain a FooKeyMap, then retrieve the translation text through a function.
For example:
// src/renderer/src/i18n/label.ts
const themeModeKeyMap = {
dark: "settings.theme.dark",
light: "settings.theme.light",
system: "settings.theme.system",
} as const;
export const getThemeModeLabel = (key: string): string => {
return themeModeKeyMap[key] ? t(themeModeKeyMap[key]) : key;
};
By avoiding template strings, you gain better developer experience, more reliable translation checks, and a more maintainable codebase.
The project includes several scripts to automate i18n-related tasks:
i18n:check - Validate i18n StructureThis script checks:
pnpm i18n:check
i18n:sync - Synchronize JSON Structure and Sort OrderThis script uses zh-cn.json as the source of truth to sync structure across all language files, including:
[to be translated]pnpm i18n:sync
i18n:translate - Automatically Translate Pending TextsThis script fills in texts marked as [to be translated] using machine translation.
Typically, after adding new texts in zh-cn.json, run i18n:sync, then i18n:translate to complete translations.
Before using this script, set the required environment variables:
API_KEY="sk-xxx"
BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1/"
MODEL="qwen-plus-latest"
Alternatively, add these variables directly to your .env file.
pnpm i18n:translate
zh-cn.jsonpnpm i18n:sync to propagate the keys to other language filespnpm i18n:translate to perform machine translationpnpm i18n:check to catch i18n issues early.user.profile.avatar.upload.error