π¬ A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots π
TypeScript browser-extensionchrome-extensiondocumentationfirefox-extensionguide-generatorlocal-firstmanifest-v3privacy-firstproductivityreactscreenshotstep-by-step-guidetutorialtypescriptworkflowwxt
refactor(ai): resolve every per-language table through one lookup
davbioa43dc70e9a8c
diff --git a/src/core/capture/ai/__tests__/locale.test.ts b/src/core/capture/ai/__tests__/locale.test.tsnew file mode 100644index 0000000..3bfbe95--- /dev/null+++ b/src/core/capture/ai/__tests__/locale.test.ts@@ -0,0 +1,39 @@+import { describe, expect, it } from 'vitest';+import { resolveByLocale } from '../locale';++const TABLE = { es: 'spanish', fr: 'french', 'pt-BR': 'portuguese', 'zh-CN': 'chinese' };++describe('resolveByLocale', () => {+ it('matches an exact code', () => {+ expect(resolveByLocale(TABLE, 'fr')).toBe('french');+ });++ it('matches a regional code to its base language', () => {+ expect(resolveByLocale(TABLE, 'fr-CA')).toBe('french');+ expect(resolveByLocale(TABLE, 'pt')).toBe('portuguese');+ expect(resolveByLocale(TABLE, 'zh')).toBe('chinese');+ });++ it('ignores case', () => {+ expect(resolveByLocale(TABLE, 'ZH-CN')).toBe('chinese');+ expect(resolveByLocale(TABLE, 'Fr')).toBe('french');+ });++ it('ignores surrounding whitespace', () => {+ expect(resolveByLocale(TABLE, ' fr ')).toBe('french');+ });++ it('returns undefined for an unknown language', () => {+ expect(resolveByLocale(TABLE, 'sv')).toBeUndefined();+ });++ it('returns undefined for an empty locale', () => {+ expect(resolveByLocale(TABLE, ' ')).toBeUndefined();+ });++ it('never reaches an inherited object property', () => {+ expect(resolveByLocale(TABLE, 'constructor')).toBeUndefined();+ expect(resolveByLocale(TABLE, 'toString')).toBeUndefined();+ expect(resolveByLocale(TABLE, '__proto__')).toBeUndefined();+ });+});diff --git a/src/core/capture/ai/locale.ts b/src/core/capture/ai/locale.tsnew file mode 100644index 0000000..61c55fd--- /dev/null+++ b/src/core/capture/ai/locale.ts@@ -0,0 +1,12 @@+export function resolveByLocale<T>(table: Readonly<Record<string, T>>, locale: string): T | undefined {+ const normalized = locale.trim().toLowerCase();+ if (!normalized) return undefined;++ const keys = Object.keys(table);+ const exact = keys.find((key) => key.toLowerCase() === normalized);+ if (exact) return table[exact];++ const base = normalized.split('-')[0];+ const sameLanguage = keys.find((key) => key.toLowerCase().split('-')[0] === base);+ return sameLanguage ? table[sameLanguage] : undefined;+}diff --git a/src/core/capture/ai/prompts.ts b/src/core/capture/ai/prompts.tsindex ec779c1..a204904 100644--- a/src/core/capture/ai/prompts.ts+++ b/src/core/capture/ai/prompts.ts@@ -69,6 +69,8 @@ export const AI_LANGUAGES = [export type AILanguageCode = (typeof AI_LANGUAGES)[number]['code'];+import { resolveByLocale } from './locale';+const LANGUAGE_NAMES: Record<string, string> = {es: 'Spanish',fr: 'French',@@ -88,18 +90,12 @@ const LANGUAGE_INSTRUCTIONS: Record<Exclude<AILanguageCode, 'en'>, string> = {};export function getLanguageSuffix(locale: string): string {- if (locale.startsWith('en')) return '';-- const instructions: Record<string, string | undefined> = LANGUAGE_INSTRUCTIONS;-- const exact = instructions[locale];- if (exact) return exact;+ const normalized = locale.trim().toLowerCase();+ if (normalized.startsWith('en')) return '';- const base = locale.split('-')[0];- for (const [code, instruction] of Object.entries(LANGUAGE_INSTRUCTIONS)) {- if (code.split('-')[0] === base) return instruction;- }+ const instruction = resolveByLocale(LANGUAGE_INSTRUCTIONS, normalized);+ if (instruction) return instruction;- const lang = LANGUAGE_NAMES[base] || locale;+ const lang = resolveByLocale(LANGUAGE_NAMES, normalized) ?? locale;return `\nIMPORTANT: Write the output in ${lang}. Never translate button, field, or page names.`;}