langue
deploy / deploy (push) Successful in 31s

This commit is contained in:
Romain committed 2026-10-04 16:37:27 +02:00
1 parent 4e270e616a
commit c82b7894a5
51 files changed
+7374 -104

No files matched your search

+110 -13
View File
@@ -14,9 +14,10 @@ type RawKey = keyof Dict
type PluralBase = RawKey extends infer K ? (K extends `${infer B}_other` ? B : never) : never
type SuffixedPlural = RawKey extends infer K ? (K extends `${string}_${'zero' | 'one' | 'two' | 'few' | 'many' | 'other'}` ? K : never) : never
export type TKey = Exclude<RawKey, SuffixedPlural> | PluralBase
export type Translations = { [K in RawKey]: string }
// Les langues aux pluriels plus riches (arabe, russe, polonais…) ajoutent des clés « _two », « _few », « _many »
export type Translations = { [K in RawKey]: string } & Record<string, string>
export type Lang = 'fr' | 'en' | 'es' | 'it' | 'de' | 'pt' | 'zh' | 'ja' | 'ko'
export type Lang = 'fr' | 'en' | 'es' | 'it' | 'de' | 'pt' | 'nl' | 'pl' | 'ru' | 'tr' | 'ar' | 'hi' | 'id' | 'th' | 'vi' | 'zh' | 'ja' | 'ko'
export interface LangInfo {
code: Lang
@@ -26,6 +27,8 @@ export interface LangInfo {
locale: string
/** Langue demandée à TMDB (titres, résumés) */
tmdb: string
/** Écriture de droite à gauche */
rtl?: boolean
}
export const LANGS: LangInfo[] = [
@@ -35,6 +38,15 @@ export const LANGS: LangInfo[] = [
{ code: 'it', label: 'Italiano', locale: 'it-IT', tmdb: 'it-IT' },
{ code: 'de', label: 'Deutsch', locale: 'de-DE', tmdb: 'de-DE' },
{ code: 'pt', label: 'Português', locale: 'pt-BR', tmdb: 'pt-BR' },
{ code: 'nl', label: 'Nederlands', locale: 'nl-NL', tmdb: 'nl-NL' },
{ code: 'pl', label: 'Polski', locale: 'pl-PL', tmdb: 'pl-PL' },
{ code: 'ru', label: 'Русский', locale: 'ru-RU', tmdb: 'ru-RU' },
{ code: 'tr', label: 'Türkçe', locale: 'tr-TR', tmdb: 'tr-TR' },
{ code: 'ar', label: 'العربية', locale: 'ar-SA', tmdb: 'ar-SA', rtl: true },
{ code: 'hi', label: 'हिन्दी', locale: 'hi-IN', tmdb: 'hi-IN' },
{ code: 'id', label: 'Bahasa Indonesia', locale: 'id-ID', tmdb: 'id-ID' },
{ code: 'th', label: 'ไทย', locale: 'th-TH', tmdb: 'th-TH' },
{ code: 'vi', label: 'Tiếng Việt', locale: 'vi-VN', tmdb: 'vi-VN' },
{ code: 'zh', label: '中文', locale: 'zh-CN', tmdb: 'zh-CN' },
{ code: 'ja', label: '日本語', locale: 'ja-JP', tmdb: 'ja-JP' },
{ code: 'ko', label: '한국어', locale: 'ko-KR', tmdb: 'ko-KR' },
@@ -47,6 +59,15 @@ const loaders: Record<Exclude<Lang, 'fr'>, () => Promise<{ default: Translations
it: () => import('./locales/it'),
de: () => import('./locales/de'),
pt: () => import('./locales/pt'),
nl: () => import('./locales/nl'),
pl: () => import('./locales/pl'),
ru: () => import('./locales/ru'),
tr: () => import('./locales/tr'),
ar: () => import('./locales/ar'),
hi: () => import('./locales/hi'),
id: () => import('./locales/id'),
th: () => import('./locales/th'),
vi: () => import('./locales/vi'),
zh: () => import('./locales/zh'),
ja: () => import('./locales/ja'),
ko: () => import('./locales/ko'),
@@ -56,7 +77,85 @@ const STORAGE_KEY = 'azuucine:lang'
export const isLang = (v: unknown): v is Lang => typeof v === 'string' && v in BY_CODE
/** Langue enregistrée, sinon celle du téléphone, sinon le français. */
/**
* Pays où se trouve l'appareil, d'après son fuseau horaire (aucune requête réseau, aucune géolocalisation).
* Pour un pays à plusieurs langues, on prend celle du téléphone si elle en fait partie.
*/
const ZONES: [RegExp, string, Lang[]][] = [
[/^Europe\/(Paris|Monaco)$|^Indian\/Reunion$|^America\/(Martinique|Guadeloupe|Cayenne)$|^Pacific\/(Tahiti|Noumea)$/, 'FR', ['fr']],
[/^Europe\/Brussels$/, 'BE', ['fr', 'nl', 'de']],
[/^Europe\/(Zurich|Busingen)$/, 'CH', ['de', 'fr', 'it']],
[/^Europe\/Luxembourg$/, 'LU', ['fr', 'de']],
[/^America\/(Montreal|Toronto|Vancouver|Edmonton|Winnipeg|Halifax|St_Johns|Regina)$/, 'CA', ['en', 'fr']],
[/^Africa\/(Abidjan|Dakar|Bamako|Ouagadougou|Niamey|Conakry|Lome|Porto-Novo|Libreville|Douala|Kinshasa|Brazzaville|Bangui|Ndjamena)$|^Indian\/Antananarivo$/, '', ['fr']],
[/^Europe\/(London|Dublin)$/, 'GB', ['en']],
[/^America\/(New_York|Chicago|Denver|Los_Angeles|Phoenix|Anchorage|Detroit)$|^Pacific\/Honolulu$/, 'US', ['en', 'es']],
[/^Australia\/|^Pacific\/Auckland$/, 'AU', ['en']],
[/^Europe\/Madrid$|^Atlantic\/Canary$/, 'ES', ['es']],
[/^America\/(Mexico_City|Bogota|Lima|Santiago|Caracas|Guayaquil|La_Paz|Asuncion|Montevideo|Panama|Costa_Rica|Guatemala|El_Salvador|Tegucigalpa|Managua|Havana|Santo_Domingo|Puerto_Rico|Tijuana|Monterrey|Cancun)$|^America\/Argentina\//, '', ['es']],
[/^Europe\/(Rome|Vatican|San_Marino)$/, 'IT', ['it']],
[/^Europe\/(Berlin|Vienna)$/, 'DE', ['de']],
[/^Europe\/Lisbon$|^Atlantic\/(Azores|Madeira)$/, 'PT', ['pt']],
[/^America\/(Sao_Paulo|Bahia|Fortaleza|Recife|Manaus|Belem|Cuiaba|Porto_Velho|Rio_Branco|Maceio|Araguaina|Campo_Grande)$/, 'BR', ['pt']],
[/^Africa\/(Luanda|Maputo)$/, '', ['pt']],
[/^Europe\/Amsterdam$|^America\/Paramaribo$/, 'NL', ['nl']],
[/^Europe\/Warsaw$/, 'PL', ['pl']],
[/^Europe\/(Moscow|Kaliningrad|Samara|Volgograd|Kirov|Astrakhan|Saratov|Ulyanovsk|Minsk)$|^Asia\/(Yekaterinburg|Omsk|Novosibirsk|Barnaul|Tomsk|Novokuznetsk|Krasnoyarsk|Irkutsk|Chita|Yakutsk|Khandyga|Vladivostok|Ust-Nera|Magadan|Sakhalin|Srednekolymsk|Kamchatka|Anadyr|Almaty|Bishkek)$/, 'RU', ['ru']],
[/^Europe\/Istanbul$|^Asia\/Istanbul$/, 'TR', ['tr']],
[/^Asia\/(Riyadh|Dubai|Kuwait|Qatar|Bahrain|Muscat|Aden|Baghdad|Amman|Beirut|Damascus|Gaza|Hebron)$|^Africa\/(Cairo|Tripoli|Tunis|Algiers|Casablanca|El_Aaiun|Khartoum|Nouakchott)$/, '', ['ar', 'fr']],
[/^Asia\/(Kolkata|Calcutta)$/, 'IN', ['hi', 'en']],
[/^Asia\/(Jakarta|Pontianak|Makassar|Jayapura)$/, 'ID', ['id']],
[/^Asia\/Bangkok$/, 'TH', ['th']],
[/^Asia\/(Ho_Chi_Minh|Saigon)$/, 'VN', ['vi']],
[/^Asia\/(Shanghai|Chongqing|Harbin|Urumqi|Hong_Kong|Macau|Taipei)$/, '', ['zh']],
[/^Asia\/Singapore$/, 'SG', ['en', 'zh']],
[/^Asia\/Tokyo$/, 'JP', ['ja']],
[/^Asia\/Seoul$/, 'KR', ['ko']],
]
function browserLangs(): Lang[] {
const out: Lang[] = []
for (const l of navigator.languages ?? [navigator.language]) {
const code = l?.slice(0, 2).toLowerCase()
if (isLang(code) && !out.includes(code)) out.push(code)
}
return out
}
/** Langue du pays où l'on se trouve (fuseau horaire), sinon celle du téléphone, sinon l'anglais. */
export function detectLang(): Lang {
const browser = browserLangs()
let zone = ''
try {
zone = Intl.DateTimeFormat().resolvedOptions().timeZone ?? ''
} catch {
/* ignore */
}
const hit = ZONES.find(([re]) => re.test(zone))
if (hit) return hit[2].find((l) => browser.includes(l)) ?? hit[2][0]
return browser[0] ?? 'en'
}
/** Pays détecté (pour les plateformes de streaming et les sorties cinéma). */
function detectRegion(): string | undefined {
try {
const zone = Intl.DateTimeFormat().resolvedOptions().timeZone ?? ''
return ZONES.find(([re]) => re.test(zone))?.[1] || undefined
} catch {
return undefined
}
}
/** La langue a-t-elle été choisie à la main ? (sinon : automatique) */
export function isLangChosen(): boolean {
try {
return isLang(localStorage.getItem(STORAGE_KEY))
} catch {
return false
}
}
/** Langue choisie, sinon automatique. */
export function initialLang(): Lang {
try {
const saved = localStorage.getItem(STORAGE_KEY)
@@ -64,11 +163,7 @@ export function initialLang(): Lang {
} catch {
/* ignore */
}
for (const l of navigator.languages ?? [navigator.language]) {
const code = l?.slice(0, 2).toLowerCase()
if (isLang(code)) return code
}
return 'fr'
return detectLang()
}
let current: Lang = 'fr'
@@ -80,15 +175,17 @@ export const getLang = () => current
export const locale = () => BY_CODE[current].locale
export const tmdbLanguage = () => BY_CODE[current].tmdb
/** Charge la langue et l'active (à attendre avant d'afficher l'interface). */
export async function loadLang(code: Lang): Promise<void> {
/** Charge la langue et l'active. `remember` : choix fait à la main (null = revenir à l'automatique). */
export async function loadLang(code: Lang, remember?: boolean | null): Promise<void> {
const next = code === 'fr' ? fr : (await loaders[code]()).default
current = code
dict = next
plural = new Intl.PluralRules(BY_CODE[code].locale)
document.documentElement.lang = code
document.documentElement.dir = BY_CODE[code].rtl ? 'rtl' : 'ltr'
try {
localStorage.setItem(STORAGE_KEY, code)
if (remember) localStorage.setItem(STORAGE_KEY, code)
else if (remember === null) localStorage.removeItem(STORAGE_KEY)
} catch {
/* ignore */
}
@@ -116,5 +213,5 @@ const formatVar = (name: string, v: string | number) => (typeof v === 'number' &
/** Nombre formaté dans la langue active (espaces, virgules…). */
export const fmtNumber = (n: number, opts?: Intl.NumberFormatOptions) => n.toLocaleString(locale(), opts)
/** Pays associé à la langue (plateformes de streaming, sorties cinéma) : FR, US, ES… */
export const region = () => BY_CODE[current].locale.split('-')[1]
/** Pays de l'utilisateur (plateformes de streaming, sorties cinéma) : celui du fuseau horaire, sinon celui de la langue. */
export const region = () => detectRegion() ?? BY_CODE[current].locale.split('-')[1]