localization for ntfy and mail
This commit is contained in:
45
frontend/src/utils/categories.ts
Normal file
45
frontend/src/utils/categories.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import defaultCategories from "@shared/defaultCategories.json";
|
||||
|
||||
type DefaultCategory = {
|
||||
key: string;
|
||||
de: string;
|
||||
en: string;
|
||||
};
|
||||
|
||||
const DEFAULT_CATEGORY_DATA = defaultCategories as DefaultCategory[];
|
||||
|
||||
function normalize(input: string): string {
|
||||
return input.trim().toLowerCase();
|
||||
}
|
||||
|
||||
const translationsByNormalizedName = new Map<string, DefaultCategory>();
|
||||
for (const category of DEFAULT_CATEGORY_DATA) {
|
||||
translationsByNormalizedName.set(normalize(category.de), category);
|
||||
translationsByNormalizedName.set(normalize(category.en), category);
|
||||
}
|
||||
|
||||
export function getCanonicalDefaultCategoryName(input: string): string | null {
|
||||
const normalized = normalize(input);
|
||||
const match = translationsByNormalizedName.get(normalized);
|
||||
return match ? match.de : null;
|
||||
}
|
||||
|
||||
export function translateCategoryName(name: string | null | undefined, language: string): string {
|
||||
if (!name) {
|
||||
return "";
|
||||
}
|
||||
const normalized = normalize(name);
|
||||
const match = translationsByNormalizedName.get(normalized);
|
||||
if (!match) {
|
||||
return name;
|
||||
}
|
||||
|
||||
if (language.startsWith("de")) {
|
||||
return match.de;
|
||||
}
|
||||
if (language.startsWith("en")) {
|
||||
return match.en;
|
||||
}
|
||||
// For other languages we fall back to English to keep labels readable.
|
||||
return match.en;
|
||||
}
|
||||
Reference in New Issue
Block a user