Translate topbar command menu (#1784)

* Rename script

* Translate menu labels

* Update translations

* Display translated menu items
This commit is contained in:
Chenlei Hu
2024-12-03 19:51:15 -08:00
committed by GitHub
parent fc69129a2f
commit 1856479de9
8 changed files with 101 additions and 43 deletions

View File

@@ -0,0 +1,57 @@
import fs from 'fs'
import { CORE_SETTINGS } from '../src/constants/coreSettings'
import { CORE_MENU_COMMANDS } from '../src/constants/coreMenuCommands'
interface SettingLocale {
name: string
tooltip?: string
}
const extractSettingLocaleStrings = (): Record<string, SettingLocale> => {
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 extractMenuCommandLocaleStrings = (): Record<string, string> => {
const labels = new Set<string>()
for (const [category, _] of CORE_MENU_COMMANDS) {
category.forEach((category) => labels.add(category))
}
return Object.fromEntries(Array.from(labels).map((label) => [label, label]))
}
const main = () => {
const settingLocaleStrings = extractSettingLocaleStrings()
const menuCommandLocaleStrings = extractMenuCommandLocaleStrings()
const localePath = './src/locales/en.json'
const globalLocale = JSON.parse(fs.readFileSync(localePath, 'utf-8'))
fs.writeFileSync(
localePath,
JSON.stringify(
{
...globalLocale,
settingsDialog: {
...(globalLocale.settingsDialog ?? {}),
...settingLocaleStrings
},
menuLabels: {
...(globalLocale.menuLabels ?? {}),
...menuCommandLocaleStrings
}
},
null,
2
)
)
}
main()

View File

@@ -1,39 +0,0 @@
import fs from 'fs'
import { CORE_SETTINGS } from '../src/constants/coreSettings'
interface SettingLocale {
name: string
tooltip?: string
}
const extractLocaleStrings = (): Record<string, SettingLocale> => {
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: { ...globalLocale.settingsDialog, ...localeStrings }
},
null,
2
)
)
}
main()