27 lines
667 B
TypeScript
27 lines
667 B
TypeScript
|
|
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] ?? "";
|
||
|
|
}
|