Files
hoerdle/lib/i18n.ts
2025-11-28 15:36:06 +01:00

42 lines
958 B
TypeScript

export type LocalizedString = {
[key: string]: string;
};
export function getLocalizedValue(
value: any,
locale: string,
fallback: string = ''
): string {
if (!value) return fallback;
// If it's already a string, return it (backward compatibility or simple values)
if (typeof value === 'string') return value;
// If it's an object, try to get the requested locale
if (typeof value === 'object') {
if (value[locale]) return value[locale];
// Fallback to 'de'
if (value['de']) return value['de'];
// Fallback to 'en'
if (value['en']) return value['en'];
// Fallback to first key
const keys = Object.keys(value);
if (keys.length > 0) return value[keys[0]];
}
return fallback;
}
export function createLocalizedObject(
de: string,
en?: string
): LocalizedString {
return {
de: de.trim(),
en: (en || de).trim()
};
}