mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
feat(i18n): register Brazilian Portuguese as a locale
Add pt-BR to the locale list and everything that hangs off it: the client loader, Intl and date-fns mapping, the reply-language footers of the Coach and insights prompts (English body, pt-BR footer, the same path Korean and Dutch take), the citation-coverage normative words, the no-key fallback copy, the AUDIT-C standard-drink line, country labels, period search, CSV yes/no cells and the Telegram bare-word aliases. The locale enums in the extract, managed-profile and record-settings schemas now derive from the locale list instead of repeating it, and the five notification and login-alert routes share one isLocale guard instead of hand-rolled chains, so a later locale is one edit there. A language tag is matched exactly and case-insensitively before its primary subtag, so pt-BR and pt-PT browsers land on the Portuguese bundle rather than English, and AUDIT-C no longer cuts a region tag to two letters. PHQ-9, GAD-7 and WHO-5 are not marked validated for pt-BR: the wording has not been read against the published Brazilian forms, so readers get the English items plus the existing note.
This commit is contained in:
1 parent
1619b2406a
commit
764794496c
72 files changed
+425
-159
No files matched your search
@@ -18,7 +18,15 @@ import { locales, localeLanguageNames, coerceLocale } from "../config";
|
||||
import { getPhaseMessage } from "@/lib/jobs/reminder-phases";
|
||||
import { detectRefusal } from "@/lib/ai/coach/refusal";
|
||||
|
||||
const NON_GERMAN_TRANSLATED = ["es", "fr", "it", "pl", "ko", "nl"] as const;
|
||||
const NON_GERMAN_TRANSLATED = [
|
||||
"es",
|
||||
"fr",
|
||||
"it",
|
||||
"pl",
|
||||
"ko",
|
||||
"nl",
|
||||
"pt-BR",
|
||||
] as const;
|
||||
|
||||
describe("plural tiers", () => {
|
||||
it("gives Polish its third tier for 2-4 and keeps many for 5+", () => {
|
||||
|
||||
@@ -7,6 +7,7 @@ export const locales = [
|
||||
"pl",
|
||||
"ko",
|
||||
"nl",
|
||||
"pt-BR",
|
||||
] as const;
|
||||
export type Locale = (typeof locales)[number];
|
||||
|
||||
@@ -22,6 +23,29 @@ export const defaultLocale: Locale = "en";
|
||||
* bundles were complete. Route every such coercion through here so adding a
|
||||
* locale to `locales` is the only edit a new language needs.
|
||||
*/
|
||||
export function isLocale(value: unknown): value is Locale {
|
||||
return (
|
||||
typeof value === "string" && (locales as readonly string[]).includes(value)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a BCP-47 language tag (`navigator.language`, one Accept-Language entry)
|
||||
* to a shipped locale: an exact match first, case-insensitive so `pt-br`
|
||||
* finds `pt-BR`, then the primary subtag, so `de-AT` reads German and `pt-PT`
|
||||
* reads the one Portuguese bundle rather than English.
|
||||
*/
|
||||
export function localeFromLanguageTag(
|
||||
tag: string | null | undefined,
|
||||
): Locale | null {
|
||||
const lower = tag?.trim().toLowerCase();
|
||||
if (!lower) return null;
|
||||
const exact = locales.find((l) => l.toLowerCase() === lower);
|
||||
if (exact) return exact;
|
||||
const primary = lower.split("-")[0];
|
||||
return locales.find((l) => l.toLowerCase().split("-")[0] === primary) ?? null;
|
||||
}
|
||||
|
||||
export function coerceLocale(locale: string | null | undefined): Locale {
|
||||
return locales.includes(locale as Locale)
|
||||
? (locale as Locale)
|
||||
@@ -43,6 +67,7 @@ export const localeLanguageNames: Record<Locale, string> = {
|
||||
pl: "Polish",
|
||||
ko: "Korean",
|
||||
nl: "Dutch",
|
||||
"pt-BR": "Brazilian Portuguese",
|
||||
};
|
||||
|
||||
export const localeLabels: Record<Locale, string> = {
|
||||
@@ -54,6 +79,7 @@ export const localeLabels: Record<Locale, string> = {
|
||||
pl: "Polski",
|
||||
ko: "한국어",
|
||||
nl: "Nederlands",
|
||||
"pt-BR": "Português (Brasil)",
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,12 @@ import {
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { apiFetchRaw } from "@/lib/api/api-fetch";
|
||||
import { locales, defaultLocale, type Locale } from "./config";
|
||||
import {
|
||||
locales,
|
||||
defaultLocale,
|
||||
localeFromLanguageTag,
|
||||
type Locale,
|
||||
} from "./config";
|
||||
import {
|
||||
makeFormatters,
|
||||
type Formatters,
|
||||
@@ -66,10 +71,7 @@ const I18nContext = createContext<I18nContextValue | null>(null);
|
||||
|
||||
function detectSystemLocale(): Locale {
|
||||
if (typeof window === "undefined") return defaultLocale;
|
||||
const browserLang = navigator.language.split("-")[0];
|
||||
return (locales as readonly string[]).includes(browserLang)
|
||||
? (browserLang as Locale)
|
||||
: defaultLocale;
|
||||
return localeFromLanguageTag(navigator.language) ?? defaultLocale;
|
||||
}
|
||||
|
||||
function readLocaleCookie(): Locale | null {
|
||||
|
||||
@@ -55,6 +55,7 @@ const loaders: Record<Locale, () => Promise<{ default: MessageBundle }>> = {
|
||||
pl: () => import("../../../messages/pl.json"),
|
||||
ko: () => import("../../../messages/ko.json"),
|
||||
nl: () => import("../../../messages/nl.json"),
|
||||
"pt-BR": () => import("../../../messages/pt-BR.json"),
|
||||
};
|
||||
|
||||
function initialCache(): Partial<Record<Locale, MessageBundle>> {
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { cookies } from "next/headers";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { defaultLocale, locales, type Locale } from "./config";
|
||||
import {
|
||||
defaultLocale,
|
||||
locales,
|
||||
localeFromLanguageTag,
|
||||
type Locale,
|
||||
} from "./config";
|
||||
|
||||
const LOCALE_COOKIE_NAMES = ["healthlog-locale", "userLocale"] as const;
|
||||
|
||||
@@ -24,8 +29,8 @@ function pickLocaleFromAcceptLanguage(header: string | null): Locale | null {
|
||||
.sort((a, b) => b.q - a.q);
|
||||
|
||||
for (const { tag } of candidates) {
|
||||
const primary = tag.split("-")[0];
|
||||
if (isLocale(primary)) return primary;
|
||||
const locale = localeFromLanguageTag(tag);
|
||||
if (locale) return locale;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user