diff --git a/package.json b/package.json index 1a6b34284..94151b055 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "preview": "vite preview", "lint": "eslint src", "lint:fix": "eslint src --fix", - "locale": "lobe-i18n locale" + "locale": "lobe-i18n locale", + "collect-i18n": "playwright test --config=playwright.i18n.config.ts" }, "devDependencies": { "@babel/core": "^7.24.7", diff --git a/playwright.i18n.config.ts b/playwright.i18n.config.ts new file mode 100644 index 000000000..f6a49ef8f --- /dev/null +++ b/playwright.i18n.config.ts @@ -0,0 +1,14 @@ +import { PlaywrightTestConfig } from '@playwright/test' + +const config: PlaywrightTestConfig = { + testDir: './scripts', + use: { + baseURL: 'http://localhost:5173', + headless: true + }, + reporter: 'list', + timeout: 10000, + testMatch: /collect-i18n\.ts/ +} + +export default config diff --git a/scripts/collect-i18n.ts b/scripts/collect-i18n.ts new file mode 100644 index 000000000..09802f20a --- /dev/null +++ b/scripts/collect-i18n.ts @@ -0,0 +1,50 @@ +import * as fs from 'fs' +import { comfyPageFixture as test } from '../browser_tests/fixtures/ComfyPage' +import type { ComfyCommandImpl } from '../src/stores/commandStore' +import { CORE_MENU_COMMANDS } from '../src/constants/coreMenuCommands' +import { normalizeI18nKey } from '../src/utils/formatUtil' + +const localePath = './src/locales/en.json' +const extractMenuCommandLocaleStrings = (): Set => { + const labels = new Set() + for (const [category, _] of CORE_MENU_COMMANDS) { + category.forEach((category) => labels.add(category)) + } + return labels +} + +test('collect-i18n', async ({ comfyPage }) => { + const commands = await comfyPage.page.evaluate(() => { + const workspace = window['app'].extensionManager + const commands = workspace.command.commands as ComfyCommandImpl[] + return commands.map((command) => ({ + id: command.id, + label: command.label, + menubarLabel: command.menubarLabel + })) + }) + + const locale = JSON.parse(fs.readFileSync(localePath, 'utf-8')) + const menuLabels = extractMenuCommandLocaleStrings() + const commandMenuLabels = new Set( + commands.map((command) => command.menubarLabel ?? command.label ?? '') + ) + const allLabels = new Set([...menuLabels, ...commandMenuLabels]) + allLabels.delete('') + + const allLabelsLocale = Object.fromEntries( + Array.from(allLabels).map((label) => [normalizeI18nKey(label), label]) + ) + + fs.writeFileSync( + localePath, + JSON.stringify( + { + ...locale, + menuLabels: allLabelsLocale + }, + null, + 2 + ) + ) +}) diff --git a/scripts/update-constants-locale.ts b/scripts/update-constants-locale.ts index b380dc245..77405af80 100644 --- a/scripts/update-constants-locale.ts +++ b/scripts/update-constants-locale.ts @@ -1,6 +1,5 @@ import fs from 'fs' import { CORE_SETTINGS } from '../src/constants/coreSettings' -import { CORE_MENU_COMMANDS } from '../src/constants/coreMenuCommands' interface SettingLocale { name: string @@ -20,17 +19,8 @@ const extractSettingLocaleStrings = (): Record => { ) } -const extractMenuCommandLocaleStrings = (): Record => { - const labels = new Set() - 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')) @@ -42,10 +32,6 @@ const main = () => { settingsDialog: { ...(globalLocale.settingsDialog ?? {}), ...settingLocaleStrings - }, - menuLabels: { - ...(globalLocale.menuLabels ?? {}), - ...menuCommandLocaleStrings } }, null, diff --git a/src/components/topbar/CommandMenubar.vue b/src/components/topbar/CommandMenubar.vue index 328f64570..49bfb17c4 100644 --- a/src/components/topbar/CommandMenubar.vue +++ b/src/components/topbar/CommandMenubar.vue @@ -31,6 +31,7 @@