Files
MyIP/common/locale-registry.js
jason5ng32andClaude Opus 5.5 c29e48687b Refactor(layout): split backend-only code out of common/, group frontend utils
common/ had become the backend's library: 30 of its 41 modules were
imported by nothing in the frontend, so the name no longer said what
belonged there. It now holds only what the frontend or the build imports
too; backend-only code moved to server/, flat except for datasets/ (the
dataset engine and its readers). The *-db/ dataset directories stay in
common/: self-hosting docs and Docker volumes mount those paths.

frontend/utils/ keeps its app-wide modules at the root and gains ip/
(IP / ASN shaping and validation), report/ (the report pipeline) and
features/ (the pure layer of a single feature). Paths only — no logic
changes. The Dockerfile copies server/, and the AGENTS.md files say where
a new module goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:10:02 +08:00

59 lines
3.0 KiB
JavaScript

// Central registry of the languages the UI ships. Adding one = a locale pack
// plus a line here. Front end imports it via `@/utils/locale-registry.js`.
//
// The back end takes no part in that: which languages upstream *data* comes in
// is a separate set owned by the source itself (SUPPORTED_LANGS in
// server/datasets/maxmind-service.js), and every `?lang` consumer resolves an unfamiliar
// tag onto its own family instead of rejecting it. A new UI locale is a
// front-end-only change.
// code=UI code + locale file name · apiTag=tag sent upstream ·
// htmlLang=<html lang> · status=full|beta
export const LOCALES = [
{ code: 'en', nativeName: 'English', flag: 'us', apiTag: 'en', htmlLang: 'en', status: 'full' },
{ code: 'zh', nativeName: '简体中文', flag: 'cn', apiTag: 'zh-CN', htmlLang: 'zh-CN', status: 'full' },
{ code: 'zh-TW', nativeName: '繁體中文', flag: 'tw', apiTag: 'zh-CN', htmlLang: 'zh-TW', status: 'full' }, // Upstream APIs do not support zh-TW, so apiTag aliases to zh-CN
{ code: 'fr', nativeName: 'Français', flag: 'fr', apiTag: 'fr', htmlLang: 'fr', status: 'full' },
{ code: 'ru', nativeName: 'Русский', flag: 'ru', apiTag: 'ru', htmlLang: 'ru', status: 'full' },
{ code: 'pt-BR', nativeName: 'Português (Brasil)', flag: 'br', apiTag: 'pt-BR', htmlLang: 'pt-BR', status: 'full' },
];
// Registry order — also the order the language picker renders.
export const LOCALE_CODES = LOCALES.map((locale) => locale.code);
// Locales held to full coverage; beta ones may ship a partial pack.
export const FULL_LOCALE_CODES = LOCALES.filter((l) => l.status === 'full').map((l) => l.code);
export const FALLBACK_LOCALE = 'en';
export const getLocale = (code) => LOCALES.find((locale) => locale.code === code);
// Both mappings pass an unregistered code through rather than blanking it.
export const toApiTag = (code) => getLocale(code)?.apiTag ?? code;
export const toHtmlLang = (code) => getLocale(code)?.htmlLang ?? code;
// The chain a missing translation walks: variant → base → en. A base only
// joins the chain when it is registered. This is the single definition of the
// order — i18n, the privacy / checklist datasets and the changelog all use it.
export const fallbackChain = (code) => {
const chain = [code];
const base = String(code).split('-')[0];
if (base !== code && LOCALE_CODES.includes(base)) chain.push(base);
if (!chain.includes(FALLBACK_LOCALE)) chain.push(FALLBACK_LOCALE);
return chain;
};
// Resolve a BCP-47 tag against `codes` in three steps: exact, then the base
// language, then any locale of that family (pt-PT → pt-BR), registry order
// deciding between siblings.
export const matchLocale = (tag, codes = LOCALE_CODES) => {
if (!tag) return null;
const wanted = String(tag).toLowerCase();
const base = wanted.split('-')[0];
return codes.find((code) => code.toLowerCase() === wanted)
?? codes.find((code) => code.toLowerCase() === base)
?? codes.find((code) => code.toLowerCase().split('-')[0] === base)
?? null;
};