Files
ComfyUI_frontend/scripts/update-constants-locale.ts
Chenlei Hu 1856479de9 Translate topbar command menu (#1784)
* Rename script

* Translate menu labels

* Update translations

* Display translated menu items
2024-12-03 22:51:15 -05:00

58 lines
1.5 KiB
TypeScript

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()