add method to localize string with hashes

This commit is contained in:
Mads Rasmussen
2024-04-08 19:04:04 +02:00
parent 7aaeeadb7c
commit d742dbbf52

View File

@@ -159,4 +159,18 @@ export class UmbLocalizationController<LocalizationSetType extends UmbLocalizati
relativeTime(value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions): string {
return new Intl.RelativeTimeFormat(this.lang(), options).format(value, unit);
}
string(text: string): string {
// find all words starting with #
const regex = /#\w+/g;
const localizedText = text.replace(regex, (match: string) => {
const key = match.slice(1);
const localized = this.term(key);
// we didn't find a localized string, so we return the original string with the #
return localized === key ? match : localized;
});
return localizedText;
}
}