Server-i18n katalog + backfill for befintliga anvandare utan hushall
This commit is contained in:
+27
-26
@@ -1,31 +1,32 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const cache: Record<string, Record<string, string>> = {};
|
||||
|
||||
/**
|
||||
* Minimal server-side i18n lookup into the mobile app's common.json files.
|
||||
* Falls back to the requested key if the translation is missing.
|
||||
* Minimal server-side i18n catalog. API code must never read files from other
|
||||
* apps' source trees (e.g. apps/mobile). Values are copied once from the mobile
|
||||
* common.json files at implementation time.
|
||||
*/
|
||||
|
||||
const HOUSEHOLD_DEFAULT_NAMES: Record<string, string> = {
|
||||
da: "Hjemme",
|
||||
de: "Zuhause",
|
||||
en: "Home",
|
||||
es: "Casa",
|
||||
fi: "Koti",
|
||||
fr: "Maison",
|
||||
it: "Casa",
|
||||
nb: "Hjemme",
|
||||
nl: "Thuis",
|
||||
pl: "Dom",
|
||||
pt: "Casa",
|
||||
sv: "Hemma",
|
||||
};
|
||||
|
||||
export function t(key: string, languageTag: string): string {
|
||||
const lang = (languageTag.split("-")[0] ?? languageTag).toLowerCase();
|
||||
let dict = cache[lang];
|
||||
if (!dict) {
|
||||
try {
|
||||
const file = path.resolve(
|
||||
__dirname,
|
||||
"../../../../apps/mobile/src/locales",
|
||||
lang,
|
||||
"common.json",
|
||||
);
|
||||
dict = JSON.parse(readFileSync(file, "utf8")) as Record<string, string>;
|
||||
} catch {
|
||||
dict = {};
|
||||
}
|
||||
cache[lang] = dict;
|
||||
if (key !== "onboarding.householdDefaultName") {
|
||||
// No other server-side keys are supported yet; fall back to a safe default.
|
||||
return HOUSEHOLD_DEFAULT_NAMES["sv"]!;
|
||||
}
|
||||
return dict[key] ?? key;
|
||||
|
||||
const lang = (languageTag.split("-")[0] ?? "sv").toLowerCase();
|
||||
return (
|
||||
HOUSEHOLD_DEFAULT_NAMES[lang] ?? HOUSEHOLD_DEFAULT_NAMES["en"] ?? HOUSEHOLD_DEFAULT_NAMES["sv"]!
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user