refactor: unify i18n function calls and simplify scripts

- Replace dynamic t(lang.labelKey) with static t(lang.label) using helper functions
- Add getLanguageLabel/getLangLabel/getLocaleLabel helper functions for switch-based label lookup
- Simplify translation check scripts to only detect literal string calls
- Fix namespace lookup for dotted namespaces like 'memorize.review'
This commit is contained in:
2026-03-18 08:13:58 +08:00
parent 286add7fff
commit 1ef337801d
6 changed files with 239 additions and 342 deletions

View File

@@ -20,19 +20,37 @@ import { PageLayout } from "@/components/ui/PageLayout";
import { getTTSUrl, TTS_SUPPORTED_LANGUAGES } from "@/lib/bigmodel/tts";
const TTS_LANGUAGES = [
{ value: "Auto", labelKey: "auto" },
{ value: "Chinese", labelKey: "chinese" },
{ value: "English", labelKey: "english" },
{ value: "Japanese", labelKey: "japanese" },
{ value: "Korean", labelKey: "korean" },
{ value: "French", labelKey: "french" },
{ value: "German", labelKey: "german" },
{ value: "Italian", labelKey: "italian" },
{ value: "Spanish", labelKey: "spanish" },
{ value: "Portuguese", labelKey: "portuguese" },
{ value: "Russian", labelKey: "russian" },
{ value: "Auto", label: "auto" },
{ value: "Chinese", label: "chinese" },
{ value: "English", label: "english" },
{ value: "Japanese", label: "japanese" },
{ value: "Korean", label: "korean" },
{ value: "French", label: "french" },
{ value: "German", label: "german" },
{ value: "Italian", label: "italian" },
{ value: "Spanish", label: "spanish" },
{ value: "Portuguese", label: "portuguese" },
{ value: "Russian", label: "russian" },
] as const;
type TTSLabel = typeof TTS_LANGUAGES[number]["label"];
function getLanguageLabel(t: (key: string) => string, label: TTSLabel): string {
switch (label) {
case "auto": return t("languages.auto");
case "chinese": return t("languages.chinese");
case "english": return t("languages.english");
case "japanese": return t("languages.japanese");
case "korean": return t("languages.korean");
case "french": return t("languages.french");
case "german": return t("languages.german");
case "italian": return t("languages.italian");
case "spanish": return t("languages.spanish");
case "portuguese": return t("languages.portuguese");
case "russian": return t("languages.russian");
}
}
export default function TextSpeakerPage() {
const t = useTranslations("text_speaker");
const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -359,7 +377,7 @@ export default function TextSpeakerPage() {
}}
size="sm"
>
{t(`languages.${lang.labelKey}`)}
{getLanguageLabel(t, lang.label)}
</LightButton>
))}
<Input

View File

@@ -20,32 +20,50 @@ import { Plus } from "lucide-react";
import { authClient } from "@/lib/auth-client";
const SOURCE_LANGUAGES = [
{ value: "Auto", labelKey: "auto" },
{ value: "Chinese", labelKey: "chinese" },
{ value: "English", labelKey: "english" },
{ value: "Japanese", labelKey: "japanese" },
{ value: "Korean", labelKey: "korean" },
{ value: "French", labelKey: "french" },
{ value: "German", labelKey: "german" },
{ value: "Italian", labelKey: "italian" },
{ value: "Spanish", labelKey: "spanish" },
{ value: "Portuguese", labelKey: "portuguese" },
{ value: "Russian", labelKey: "russian" },
{ value: "Auto", label: "auto" },
{ value: "Chinese", label: "chinese" },
{ value: "English", label: "english" },
{ value: "Japanese", label: "japanese" },
{ value: "Korean", label: "korean" },
{ value: "French", label: "french" },
{ value: "German", label: "german" },
{ value: "Italian", label: "italian" },
{ value: "Spanish", label: "spanish" },
{ value: "Portuguese", label: "portuguese" },
{ value: "Russian", label: "russian" },
] as const;
const TARGET_LANGUAGES = [
{ value: "Chinese", labelKey: "chinese" },
{ value: "English", labelKey: "english" },
{ value: "Japanese", labelKey: "japanese" },
{ value: "Korean", labelKey: "korean" },
{ value: "French", labelKey: "french" },
{ value: "German", labelKey: "german" },
{ value: "Italian", labelKey: "italian" },
{ value: "Spanish", labelKey: "spanish" },
{ value: "Portuguese", labelKey: "portuguese" },
{ value: "Russian", labelKey: "russian" },
{ value: "Chinese", label: "chinese" },
{ value: "English", label: "english" },
{ value: "Japanese", label: "japanese" },
{ value: "Korean", label: "korean" },
{ value: "French", label: "french" },
{ value: "German", label: "german" },
{ value: "Italian", label: "italian" },
{ value: "Spanish", label: "spanish" },
{ value: "Portuguese", label: "portuguese" },
{ value: "Russian", label: "russian" },
] as const;
type LangLabel = typeof SOURCE_LANGUAGES[number]["label"];
function getLangLabel(t: (key: string) => string, label: LangLabel): string {
switch (label) {
case "auto": return t("auto");
case "chinese": return t("chinese");
case "english": return t("english");
case "japanese": return t("japanese");
case "korean": return t("korean");
case "french": return t("french");
case "german": return t("german");
case "italian": return t("italian");
case "spanish": return t("spanish");
case "portuguese": return t("portuguese");
case "russian": return t("russian");
}
}
// Estimated button width in pixels (including gap)
const BUTTON_WIDTH = 80;
const LABEL_WIDTH = 100;
@@ -290,7 +308,7 @@ export default function TranslatorPage() {
}}
className="shrink-0"
>
{t(lang.labelKey)}
{getLangLabel(t, lang.label)}
</LightButton>
))}
<Input
@@ -353,7 +371,7 @@ export default function TranslatorPage() {
}}
className="shrink-0"
>
{t(lang.labelKey)}
{getLangLabel(t, lang.label)}
</LightButton>
))}
<Input

View File

@@ -13,11 +13,18 @@ import { actionCreateCard } from "@/modules/card/card-action";
import type { CardType, CardMeaning } from "@/modules/card/card-action-dto";
import { toast } from "sonner";
const QUERY_LANGUAGE_LABELS = {
english: "english",
chinese: "chinese",
japanese: "japanese",
korean: "korean",
} as const;
const QUERY_LANGUAGES = [
{ value: "en", labelKey: "english" },
{ value: "zh", labelKey: "chinese" },
{ value: "ja", labelKey: "japanese" },
{ value: "ko", labelKey: "korean" },
{ value: "en", label: "english" as const },
{ value: "zh", label: "chinese" as const },
{ value: "ja", label: "japanese" as const },
{ value: "ko", label: "korean" as const },
] as const;
interface AddCardModalProps {
@@ -169,7 +176,7 @@ export function AddCardModal({
}}
size="sm"
>
{t(lang.labelKey)}
{t(lang.label)}
</LightButton>
))}
<Input

View File

@@ -21,7 +21,25 @@ const COMMON_LANGUAGES = [
{ label: "portuguese", value: "portuguese" },
{ label: "russian", value: "russian" },
{ label: "other", value: "other" },
];
] as const;
type LocaleLabel = typeof COMMON_LANGUAGES[number]["label"];
function getLocaleLabel(t: (key: string) => string, label: LocaleLabel): string {
switch (label) {
case "chinese": return t("translator.chinese");
case "english": return t("translator.english");
case "italian": return t("translator.italian");
case "japanese": return t("translator.japanese");
case "korean": return t("translator.korean");
case "french": return t("translator.french");
case "german": return t("translator.german");
case "spanish": return t("translator.spanish");
case "portuguese": return t("translator.portuguese");
case "russian": return t("translator.russian");
case "other": return t("translator.other");
}
}
interface LocaleSelectorProps {
value: string;
@@ -62,7 +80,7 @@ export function LocaleSelector({ value, onChange }: LocaleSelectorProps) {
>
{COMMON_LANGUAGES.map((lang) => (
<option key={lang.value} value={lang.value}>
{t(`translator.${lang.label}`)}
{getLocaleLabel(t, lang.label)}
</option>
))}
</Select>