From e5a2017f7de41b2457e90e5e784e0dbc28604d13 Mon Sep 17 00:00:00 2001 From: ShareVB Date: Mon, 27 Jul 2026 09:27:30 +0200 Subject: [PATCH] fix(i18n): tool search in current language and handle switching Fix #486 --- locales/en.yml | 18 +++ src/layouts/base.layout.vue | 22 ++- .../command-palette/command-palette.store.ts | 152 +++++++++--------- .../command-palette/command-palette.vue | 45 ++++-- src/tools/tools.store.ts | 38 +++-- 5 files changed, 168 insertions(+), 107 deletions(-) diff --git a/locales/en.yml b/locales/en.yml index e3901830..677f5586 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -9574,6 +9574,24 @@ tools: cidr-expands-to-hostcount-ips-exceeding-limit-of-max_cidr_ips: CIDR expands to {0} IPs, exceeding limit of {1} tag-reachable: Reachable tag-unreachable: Unreachable + command-palette: + store: + texts: + pages: Pages + learn-more-about-it-tools: Learn more about IT-Tools. + actions-0: Actions + report-a-bug-or-an-issue-to-help-improve-it-tools: Report a bug or an issue to help improve it-tools. + report-a-bug-or-an-issue: Report a bug or an issue + view-the-source-code-of-it-tools-on-github: View the source code of it-tools on Github. + external: External + github-repository: Github repository + actions: Actions + toggle-dark-mode-on-or-off: Toggle dark mode on or off. + toggle-dark-mode: Toggle dark mode + tools-0: Tools + get-a-random-tool-from-the-list: Get a random tool from the list. + random-tool: Random tool + tools: Tools collapsibleToolMenu: text: expanding: Expanding... diff --git a/src/layouts/base.layout.vue b/src/layouts/base.layout.vue index 8f7a79b2..12b52cf8 100644 --- a/src/layouts/base.layout.vue +++ b/src/layouts/base.layout.vue @@ -27,13 +27,15 @@ watchEffect(() => { document.documentElement.style.setProperty('--app-topbar-height', `${Math.round(navbarHeight.value)}px`); }); -const { t } = useI18n(); +const { t, locale } = useI18n(); const toolStore = useToolStore(); const { favoriteTools, toolsByCategory } = storeToRefs(toolStore); const tools = computed(() => [ - ...(favoriteTools.value.length > 0 ? [{ name: t('tools.categories.favorite-tools'), components: favoriteTools.value }] : []), + ...(favoriteTools.value.length > 0 + ? [{ name: t('tools.categories.favorite-tools'), components: favoriteTools.value }] + : []), ...toolsByCategory.value, ]); @@ -44,9 +46,7 @@ const tools = computed(() => [
-
- IT - TOOLS -
+
IT - TOOLS
{{ $t('home.subtitle') }} @@ -109,12 +109,20 @@ const tools = computed(() => [ - + - + + +
diff --git a/src/modules/command-palette/command-palette.store.ts b/src/modules/command-palette/command-palette.store.ts index a24d50d5..4cad5905 100644 --- a/src/modules/command-palette/command-palette.store.ts +++ b/src/modules/command-palette/command-palette.store.ts @@ -11,85 +11,89 @@ import DiceIcon from '~icons/mdi/dice-5'; import InfoIcon from '~icons/mdi/information-outline'; import { useFlexSearch } from '@/composable/flexSearch'; +import { translate as t } from '@/plugins/i18n.plugin'; + const maxSearchResultsPerCategory = import.meta.env.VITE_MAX_SEARCH_RESULT || 25; -export const useCommandPaletteStore = defineStore('command-palette', () => { - const toolStore = useToolStore(); - const styleStore = useStyleStore(); - const router = useRouter(); - const searchPrompt = ref(''); +export const useCommandPaletteStore = (locale: string) => + defineStore(`command-palette-${locale}`, () => { + const toolStore = useToolStore(); + const styleStore = useStyleStore(); + const router = useRouter(); + const searchPrompt = ref(''); - const toolsOptions = toolStore.tools.map(tool => ({ - ...tool, - to: tool.path, - toolCategory: tool.category, - category: 'Tools', - })); + const toolsOptions = toolStore.tools.map((tool) => ({ + ...tool, + to: tool.path, + toolCategory: tool.category, + category: t('tools.command-palette.store.texts.tools'), + })); - const searchOptions: PaletteOption[] = [ - ...toolsOptions, - { - name: 'Random tool', - description: 'Get a random tool from the list.', - action: () => { - const { path } = _.sample(toolStore.tools)!; - router.push(path); + const searchOptions: PaletteOption[] = [ + ...toolsOptions, + { + name: t('tools.command-palette.store.texts.random-tool'), + description: t('tools.command-palette.store.texts.get-a-random-tool-from-the-list'), + action: () => { + const { path } = _.sample(toolStore.tools)!; + router.push(path); + }, + icon: DiceIcon, + category: t('tools.command-palette.store.texts.tools-0'), + keywords: ['random', 'tool', 'pick', 'choose', 'select'], + closeOnSelect: true, }, - icon: DiceIcon, - category: 'Tools', - keywords: ['random', 'tool', 'pick', 'choose', 'select'], - closeOnSelect: true, - }, - { - name: 'Toggle dark mode', - description: 'Toggle dark mode on or off.', - action: () => styleStore.toggleDark(), - icon: SunIcon, - category: 'Actions', - keywords: ['dark', 'theme', 'toggle', 'mode', 'light', 'system'], - }, - { - name: 'Github repository', - href: 'https://github.com/sharevb/it-tools', - category: 'External', - description: 'View the source code of it-tools on Github.', - keywords: ['github', 'repo', 'repository', 'source', 'code'], - icon: GithubIcon, - }, - { - name: 'Report a bug or an issue', - description: 'Report a bug or an issue to help improve it-tools.', - href: 'https://github.com/sharevb/it-tools/issues/new/choose', - category: 'Actions', - keywords: ['report', 'issue', 'bug', 'problem', 'error'], - icon: BugIcon, - }, - { - name: 'About', - description: 'Learn more about IT-Tools.', - to: '/about', - category: 'Pages', - keywords: ['about', 'learn', 'more', 'info', 'information'], - icon: InfoIcon, - }, - ]; + { + name: t('tools.command-palette.store.texts.toggle-dark-mode'), + description: t('tools.command-palette.store.texts.toggle-dark-mode-on-or-off'), + action: () => styleStore.toggleDark(), + icon: SunIcon, + category: t('tools.command-palette.store.texts.actions'), + keywords: ['dark', 'theme', 'toggle', 'mode', 'light', 'system'], + }, + { + name: t('tools.command-palette.store.texts.github-repository'), + href: 'https://github.com/sharevb/it-tools', + category: t('tools.command-palette.store.texts.external'), + description: t('tools.command-palette.store.texts.view-the-source-code-of-it-tools-on-github'), + keywords: ['github', 'repo', 'repository', 'source', 'code'], + icon: GithubIcon, + }, + { + name: t('tools.command-palette.store.texts.report-a-bug-or-an-issue'), + description: t('tools.command-palette.store.texts.report-a-bug-or-an-issue-to-help-improve-it-tools'), + href: 'https://github.com/sharevb/it-tools/issues/new/choose', + category: t('tools.command-palette.store.texts.actions-0'), + keywords: ['report', 'issue', 'bug', 'problem', 'error'], + icon: BugIcon, + }, + { + name: t('tools.pomodoro-timer.PomodoroApp.text.about'), + description: t('tools.command-palette.store.texts.learn-more-about-it-tools'), + to: '/about', + category: t('tools.command-palette.store.texts.pages'), + keywords: ['about', 'learn', 'more', 'info', 'information'], + icon: InfoIcon, + }, + ]; - const { searchResult } = useFlexSearch({ - search: searchPrompt, - data: searchOptions, - options: { - keys: [{ name: 'name', weight: 2 }, 'description', 'keywords', 'category'], - }, - }); + const { searchResult } = useFlexSearch({ + search: searchPrompt, + data: searchOptions, + options: { + keys: [{ name: 'name', weight: 2 }, 'description', 'keywords', 'category'], + }, + }); - const filteredSearchResult = computed(() => - _.mapValues( - _.groupBy(searchResult.value, option => option.category), - categoryOptions => categoryOptions.slice(0, maxSearchResultsPerCategory), - )); + const filteredSearchResult = computed(() => + _.mapValues( + _.groupBy(searchResult.value, (option) => option.category), + (categoryOptions) => categoryOptions.slice(0, maxSearchResultsPerCategory), + ), + ); - return { - filteredSearchResult, - searchPrompt, - }; -}); + return { + filteredSearchResult, + searchPrompt, + }; + })(); diff --git a/src/modules/command-palette/command-palette.vue b/src/modules/command-palette/command-palette.vue index 8455a2ed..5b758cf4 100644 --- a/src/modules/command-palette/command-palette.vue +++ b/src/modules/command-palette/command-palette.vue @@ -3,13 +3,17 @@ import { storeToRefs } from 'pinia'; import * as _ from 'es-toolkit/compat'; import { useCommandPaletteStore } from './command-palette.store'; import type { PaletteOption } from './command-palette.types'; +import { loadLocaleMessages } from '@/plugins/i18n.plugin'; const isModalOpen = ref(false); const inputRef = ref(); const router = useRouter(); const isMac = computed(() => window.navigator.userAgent.toLowerCase().includes('mac')); -const commandPaletteStore = useCommandPaletteStore(); +const { locale } = useI18n(); +await loadLocaleMessages(locale.value); + +const commandPaletteStore = useCommandPaletteStore(locale.value); const { searchPrompt, filteredSearchResult } = storeToRefs(commandPaletteStore); const keys = useMagicKeys({ @@ -32,7 +36,7 @@ whenever(keys.meta_k, open); whenever(keys.escape, close); function open() { - return isModalOpen.value = true; + return (isModalOpen.value = true); } function close() { @@ -67,7 +71,9 @@ function handleKeydown(event: KeyboardEvent) { } function getOptionIndex(option: PaletteOption) { - return Object.values(filteredSearchResult.value).flat().findIndex(o => o === option); + return Object.values(filteredSearchResult.value) + .flat() + .findIndex((o) => o === option); } function activateOption(option: PaletteOption) { @@ -108,7 +114,6 @@ function activateOption(option: PaletteOption) {
- {{ $t('search.label') }} @@ -118,14 +123,36 @@ function activateOption(option: PaletteOption) { - - + +
{{ category }}
- +
@@ -136,8 +163,8 @@ function activateOption(option: PaletteOption) { font-size: 18px; ::v-deep(.input-wrapper) { - padding: 4px; - padding-left: 18px; + padding: 4px; + padding-left: 18px; } } diff --git a/src/tools/tools.store.ts b/src/tools/tools.store.ts index 6659b693..cf99d049 100644 --- a/src/tools/tools.store.ts +++ b/src/tools/tools.store.ts @@ -6,36 +6,37 @@ import type { Tool, ToolCategory, ToolWithCategory } from './tools.types'; import { tools as allTools } from './index'; import { useITStorage } from '@/composable/queryParams'; +import { translate as t } from '@/plugins/i18n.plugin'; + export const useToolStore = defineStore('tools', () => { const favoriteToolsName = useITStorage('favoriteToolsName', []) as Ref; - const { t } = useI18n(); - const tools = computed(() => allTools - .map((tool) => { + const tools = computed(() => + allTools.map((tool) => { const toolI18nKey = tool.path.replace(/\//g, ''); const category = tool.category || 'Development'; - return ({ + return { ...tool, path: tool.path, name: t(`tools.${toolI18nKey}.title`, tool.name), description: t(`tools.${toolI18nKey}.description`, tool.description), category: t(`tools.categories.${category.toLowerCase()}`, category), - }); - })); + }; + }), + ); const toolsByCategory = computed(() => { const orderedTools = _.orderBy(tools.value, ['category', 'name'], ['asc', 'asc']); - return Object.entries(_.groupBy(orderedTools, tool => tool.category)) - .map(([name, components]) => ({ - name, - components, - })); + return Object.entries(_.groupBy(orderedTools, (tool) => tool.category)).map(([name, components]) => ({ + name, + components, + })); }); const favoriteTools = computed(() => { return favoriteToolsName.value - .map(favoriteName => tools.value.find(({ name, path }) => name === favoriteName || path === favoriteName)) + .map((favoriteName) => tools.value.find(({ name, path }) => name === favoriteName || path === favoriteName)) .filter(Boolean) as ToolWithCategory[]; // cast because .filter(Boolean) does not remove undefined from type }); @@ -54,17 +55,20 @@ export const useToolStore = defineStore('tools', () => { }, removeToolFromFavorites({ tool }: { tool: MaybeRef }) { - favoriteToolsName.value = favoriteToolsName.value.filter(name => get(tool).name !== name && get(tool).path !== name); - favoriteToolsName.value = favoriteToolsName.value.filter(name => get(tool).name !== name && get(tool).path !== name); + favoriteToolsName.value = favoriteToolsName.value.filter( + (name) => get(tool).name !== name && get(tool).path !== name, + ); + favoriteToolsName.value = favoriteToolsName.value.filter( + (name) => get(tool).name !== name && get(tool).path !== name, + ); }, isToolFavorite({ tool }: { tool: MaybeRef }) { - return favoriteToolsName.value.includes(get(tool).name) - || favoriteToolsName.value.includes(get(tool).path); + return favoriteToolsName.value.includes(get(tool).name) || favoriteToolsName.value.includes(get(tool).path); }, updateFavoriteTools(newOrder: ToolWithCategory[]) { - favoriteToolsName.value = newOrder.map(tool => tool.path); + favoriteToolsName.value = newOrder.map((tool) => tool.path); }, }; });