feat(i18n): the server's closed English vocabularies named in the reader's language (E30 S2)

- lib/vocab + the `vocab` locale area: TMDB crew jobs and departments, auto-tags (D1 A: the data
  stays English, the catalog is named at its one source, useTags), built-in download profiles,
  YouTube topics; held key-for-key to the backend's own lists by backendIds.contract.
- Countries by ISO code: TMDB parse stores {code, name}; rows stored before are coded from TMDB's
  own country list in one request (run_enrich); Intl names them, historic codes (YU, SU, XI…)
  from the catalog.
- Player tracks named from the language tag, forced/SDH flags and codec tail, not Plex's
  server-language displayTitle; no invented "Audio N" on the server.
- Download lifecycle notices (shared / expiring / expired / evicted) named from data; the share
  notice carries the title.
- Scheduler: the job result rides along as data and is named per key (dashboard + run notice).
- Invite status named; a render crash's notice no longer shows the engine's English message.
- Fix: YouTube links …/wiki/Humour, the tagger looked for "Humor" — Comedy never came from a topic.
This commit is contained in:
2026-09-29 21:21:19 +02:00
parent f6298d29af
commit f3a85916ec
50 changed files with 1475 additions and 94 deletions
+83
View File
@@ -0,0 +1,83 @@
import type { TFunction } from "i18next";
import type { PlexTrackNaming } from "./api";
// E30: the closed vocabularies the server stores in ENGLISH — a TMDB crew job or department, an
// auto-tag, a built-in download profile, a YouTube topic — named in the reader's language. The data
// stays language-neutral (it is filtered and matched on); only the NAME is translated, here, from the
// `vocab` locale area. A word the area does not hold shows as stored, so a new server value is never
// hidden — and the backend's own lists are held key-for-key to the area by backendIds.contract.
type Group = "crewJob" | "department" | "autoTag" | "profile" | "topic";
/** A vocabulary word's key: its letters and digits ("Health & Fitness" → "HealthFitness"). */
export function vocabKey(word: string): string {
return word.replace(/[^A-Za-z0-9]/g, "");
}
/** One stored English word of `group`, in the reader's language (`fallback`, by default the word
* itself, when the area does not hold it). */
export function vocab(t: TFunction, group: Group, word: string, fallback = word): string {
const key = vocabKey(word);
return key ? t(`vocab.${group}.${key}`, { defaultValue: fallback }) : fallback;
}
/** A download profile's name: a built-in one is seeded under an English name and named here; a user's
* own is theirs, as typed. */
export function profileName(t: TFunction, p: { name: string; is_builtin: boolean }): string {
return p.is_builtin ? vocab(t, "profile", p.name) : p.name;
}
function intlName(code: string, lang: string, type: "region" | "language"): string | undefined {
try {
return new Intl.DisplayNames([lang], { type, fallback: "none" }).of(code);
} catch {
return undefined; // not a well-formed code
}
}
/** A country from its ISO 3166 code. A code no longer in use (Yugoslavia, the Soviet Union) is
* named from `vocab.region` — Intl renames those to today's state (YU → Serbia) or cannot name them
* (XI, TMDB's Northern Ireland); `fallback` (the stored English name) when neither can. */
export function regionName(
t: TFunction,
code: string | null | undefined,
lang: string,
fallback = "",
): string {
if (!code) return fallback;
const up = code.toUpperCase();
return (
t(`vocab.region.${up}`, { defaultValue: "" }) || intlName(up, lang, "region") || fallback || up
);
}
/** A language from its BCP 47 tag ("en-US" → "American English"), capitalized to stand alone as a
* label ("magyar" → "Magyar"); `fallback` for a tag Intl cannot name ("und", a typo). */
export function languageName(tag: string | null | undefined, lang: string, fallback = ""): string {
const name = tag ? intlName(tag.replace(/_/g, "-"), lang, "language") : undefined;
if (!name || name === tag) return fallback;
return name.charAt(0).toLocaleUpperCase(lang) + name.slice(1);
}
/** An audio or subtitle track's name in the reader's language: "Angol (AC3 5.1)", "Magyar,
* kényszerített". Built from the language tag; a track without one keeps Plex's own title, and a
* track with neither is numbered. */
export function trackLabel(
t: TFunction,
track: PlexTrackNaming,
kind: "audio" | "subtitle",
lang: string,
): string {
// The language alone: Plex tags one track "en-US" and the next "en", and both are just English here.
const name = languageName(track.lang_tag?.split(/[-_]/)[0], lang);
if (!name) return track.label || t(`vocab.track.${kind}N`, { n: track.ord + 1 });
const flags = [
track.forced ? t("vocab.track.forced") : null,
track.hearing_impaired ? t("vocab.track.sdh") : null,
].filter(Boolean);
// The codec tail is technical and language-neutral but for its channel word ("AC3 Stereo").
const detail = track.detail
?.replace(/\bStereo\b/i, t("vocab.track.stereo"))
.replace(/\bMono\b/i, t("vocab.track.mono"));
return [name, ...flags].join(", ") + (detail ? ` (${detail})` : "");
}