Skip to content

Commit ea486a0

Browse files
fix(i18n): use resolved UI language for Visual Explain AI analysis in auto mode (#1026)
When the app language is set to Auto, map i18n.resolvedLanguage (including regional codes such as pt-BR and zh-CN) to the existing SUPPORTED_LANGUAGES label, falling back to English when it is missing or unsupported. Explicit language selection is unchanged. Fixes #894 Co-authored-by: princecherry9905 <230962627+princecherry9905@users.noreply.github.com>
1 parent 423749f commit ea486a0

3 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎src/components/modals/visual-explain/ExplainAiAnalysis.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ interface ExplainAiAnalysisProps {
1111
}
1212

1313
export function ExplainAiAnalysis({ plan }: ExplainAiAnalysisProps) {
14-
const { t } = useTranslation();
14+
const { t, i18n } = useTranslation();
1515
const { settings } = useSettings();
16+
const resolvedLanguage = i18n.resolvedLanguage ?? i18n.language;
1617
const [analysis, setAnalysis] = useState("");
1718
const [isLoading, setIsLoading] = useState(false);
1819
const [error, setError] = useState<string | null>(null);
@@ -41,7 +42,7 @@ export function ExplainAiAnalysis({ plan }: ExplainAiAnalysisProps) {
4142
provider: settings.aiProvider,
4243
model: settings.aiModel || "",
4344
query: queryWithPlan,
44-
language: getAiExplanationLanguage(settings.language),
45+
language: getAiExplanationLanguage(settings.language, resolvedLanguage),
4546
},
4647
});
4748
setAnalysis(result);
@@ -50,7 +51,7 @@ export function ExplainAiAnalysis({ plan }: ExplainAiAnalysisProps) {
5051
} finally {
5152
setIsLoading(false);
5253
}
53-
}, [plan, settings.aiProvider, settings.aiModel, settings.language, t]);
54+
}, [plan, settings.aiProvider, settings.aiModel, settings.language, resolvedLanguage, t]);
5455

5556
useEffect(() => {
5657
if (plan.raw_output) {

‎src/i18n/language.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import { SUPPORTED_LANGUAGES, type AppLanguage } from "./config";
22

3-
export function getAiExplanationLanguage(language: AppLanguage): string {
3+
function findSupportedLanguageLabel(locale?: string): string | undefined {
4+
if (!locale) return undefined;
5+
const wanted = locale.replace("_", "-").toLowerCase();
6+
const exact = SUPPORTED_LANGUAGES.find(
7+
({ id }) => id.toLowerCase() === wanted,
8+
);
9+
if (exact) return exact.label;
10+
const base = wanted.split("-")[0];
11+
return SUPPORTED_LANGUAGES.find(({ id }) => {
12+
const supported = id.toLowerCase();
13+
return supported === base || supported.startsWith(`${base}-`);
14+
})?.label;
15+
}
16+
17+
export function getAiExplanationLanguage(
18+
language: AppLanguage,
19+
resolvedLanguage?: string,
20+
): string {
421
const fallBackLanguage = "English";
522

623
if (language === "auto") {
7-
return fallBackLanguage;
24+
return findSupportedLanguageLabel(resolvedLanguage) ?? fallBackLanguage;
825
}
926

1027
const supportedLanguage = SUPPORTED_LANGUAGES.find(

‎tests/i18n/language.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, it, expect } from "vitest";
2+
import { getAiExplanationLanguage } from "../../src/i18n/language";
3+
import { SUPPORTED_LANGUAGES } from "../../src/i18n/config";
4+
5+
describe("language", () => {
6+
describe("getAiExplanationLanguage", () => {
7+
it("uses the resolved language in auto mode", () => {
8+
expect(getAiExplanationLanguage("auto", "it")).toBe("Italiano");
9+
});
10+
11+
it("maps supported regional locales in auto mode", () => {
12+
expect(getAiExplanationLanguage("auto", "pt-BR")).toBe("Português (Brasil)");
13+
expect(getAiExplanationLanguage("auto", "zh-CN")).toBe("中文");
14+
expect(getAiExplanationLanguage("auto", "it-CH")).toBe("Italiano");
15+
});
16+
17+
it("is case- and separator-insensitive", () => {
18+
expect(getAiExplanationLanguage("auto", "PT_br")).toBe("Português (Brasil)");
19+
});
20+
21+
it("falls back to English for unsupported resolved languages", () => {
22+
expect(getAiExplanationLanguage("auto", "nl")).toBe("English");
23+
expect(getAiExplanationLanguage("auto", "sv-SE")).toBe("English");
24+
});
25+
26+
it("falls back to English when the resolved language is missing", () => {
27+
expect(getAiExplanationLanguage("auto")).toBe("English");
28+
expect(getAiExplanationLanguage("auto", undefined)).toBe("English");
29+
expect(getAiExplanationLanguage("auto", "")).toBe("English");
30+
});
31+
32+
it("keeps explicitly selected languages unchanged", () => {
33+
for (const { id, label } of SUPPORTED_LANGUAGES) {
34+
expect(getAiExplanationLanguage(id)).toBe(label);
35+
}
36+
expect(getAiExplanationLanguage("it", "de")).toBe("Italiano");
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)