From a56462fc7c247d82372c6f55e23a986f6303bcc7 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Mon, 2 Dec 2024 14:34:21 -0800 Subject: [PATCH] Translate core setting name & tooltip (#1765) * lazy eval default value * Add setting translation * Adjust hooks * Add all translations * nit * Normalized setting id * Update locales * Fallback * Locale => Language * Locale => Language * Update translations --- .i18nrc.cjs | 2 +- lint-staged.config.js | 8 +- scripts/update-setting-locale.ts | 32 +++ .../dialog/content/setting/SettingItem.vue | 15 +- src/constants/coreSettings.ts | 6 +- src/locales/en.json | 239 +++++++++++++++++- src/locales/ja.json | 237 +++++++++++++++++ src/locales/ru.json | 237 +++++++++++++++++ src/locales/zh.json | 237 +++++++++++++++++ src/scripts/ui/settings.ts | 4 +- src/types/settingTypes.ts | 2 +- 11 files changed, 1008 insertions(+), 11 deletions(-) create mode 100644 scripts/update-setting-locale.ts diff --git a/.i18nrc.cjs b/.i18nrc.cjs index a53543933..df3590444 100644 --- a/.i18nrc.cjs +++ b/.i18nrc.cjs @@ -1,4 +1,4 @@ -// This file is intentionally kept in CommonJS format (.cjs) +// This file is intentionally kept in CommonJS format (.cjs) // to resolve compatibility issues with dependencies that require CommonJS. // Do not convert this file to ESModule format unless all dependencies support it. const { defineConfig } = require('@lobehub/i18n-cli'); diff --git a/lint-staged.config.js b/lint-staged.config.js index 216d0674e..f5682f495 100644 --- a/lint-staged.config.js +++ b/lint-staged.config.js @@ -8,9 +8,11 @@ export default { 'tsc-strict' ], - './src/locales/*.json': (stagedFiles) => [ - 'lobe-i18n locale', - ...formatFiles(stagedFiles) + './src/locales/en.json': () => ['lobe-i18n locale'], + + './src/constants/coreSettings.ts': () => [ + 'tsx scripts/update-setting-locale.ts', + 'lobe-i18n locale' ] } diff --git a/scripts/update-setting-locale.ts b/scripts/update-setting-locale.ts new file mode 100644 index 000000000..601535495 --- /dev/null +++ b/scripts/update-setting-locale.ts @@ -0,0 +1,32 @@ +import fs from 'fs' +import { CORE_SETTINGS } from '../src/constants/coreSettings' + +interface SettingLocale { + name: string + tooltip?: string +} + +const extractLocaleStrings = (): Record => { + return Object.fromEntries( + CORE_SETTINGS.sort((a, b) => a.id.localeCompare(b.id)).map((setting) => [ + // '.' is not allowed in JSON keys, so we replace it with '_' + setting.id.replace(/\./g, '_'), + { + name: setting.name, + tooltip: setting.tooltip + } + ]) + ) +} + +const main = () => { + const localeStrings = extractLocaleStrings() + const localePath = './src/locales/en.json' + const globalLocale = JSON.parse(fs.readFileSync(localePath, 'utf-8')) + fs.writeFileSync( + localePath, + JSON.stringify({ ...globalLocale, settingsDialog: localeStrings }, null, 2) + ) +} + +main() diff --git a/src/components/dialog/content/setting/SettingItem.vue b/src/components/dialog/content/setting/SettingItem.vue index 83b4fcc0b..03df2f427 100644 --- a/src/components/dialog/content/setting/SettingItem.vue +++ b/src/components/dialog/content/setting/SettingItem.vue @@ -1,6 +1,6 @@