dreame-shopify-app/app/lib/locale.ts

27 lines
667 B
TypeScript
Raw Permalink Normal View History

2026-06-19 00:53:13 +08:00
export type ButtonTextMap = Record<string, string>;
export function selectButtonText(locale: string | undefined, texts: ButtonTextMap): string {
const normalizedLocale = locale?.trim();
if (normalizedLocale && texts[normalizedLocale]) {
return texts[normalizedLocale];
}
const language = normalizedLocale?.split("-")[0];
if (language) {
if (texts[language]) {
return texts[language];
}
const languageMatch = Object.entries(texts).find(([key]) => key.split("-")[0] === language);
if (languageMatch) {
return languageMatch[1];
}
}
if (texts.en) {
return texts.en;
}
return Object.values(texts)[0] ?? "";
}