|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The options of `localization.locale` (Setup's 默认语言): the languages the |
| 5 | + * deployment supports, not a list written into the manifest. |
| 6 | + * |
| 7 | + * The manifest used to declare four (`en-US`, `zh-CN`, `ja-JP`, `es-ES`), and |
| 8 | + * that table was the enforcement boundary. The user's language menu offers |
| 9 | + * something else: what `GET /api/v1/i18n/locales` reports, which is the i18n |
| 10 | + * service's `getLocales()` (the app's declared `i18n.supportedLocales`, else |
| 11 | + * every locale a bundle or an authored translation item has loaded). So a |
| 12 | + * deployment with a French pack, or a Vietnamese translation item, offered |
| 13 | + * French or Vietnamese to each user and refused it as the organization's |
| 14 | + * default. This source makes both answers one: the options and the accepted |
| 15 | + * values are what `getLocales()` reports at the moment they are asked, so a |
| 16 | + * translation item added at runtime is offered here too. |
| 17 | + * |
| 18 | + * What it answers, for the i18n service's codes: |
| 19 | + * |
| 20 | + * - **Options.** One per code, in the service's order, each named in its own |
| 21 | + * language: the platform locale table's name for the language the code |
| 22 | + * means (`PLATFORM_LOCALES`, through its read aliases, so `zh-CN` is |
| 23 | + * 简体中文), else `Intl.DisplayNames` in the code itself (`vi-VN` is |
| 24 | + * "Tiếng Việt (Việt Nam)"), else the code. The language menu names them |
| 25 | + * the same way. |
| 26 | + * - **Accepted values.** Each code, and a second spelling of one: a stored |
| 27 | + * code that means the same table language (`en-US` where `en` is offered, |
| 28 | + * `zh-CN` where `zh-Hans` is). Those are the spellings this setting has |
| 29 | + * stored all along, and its manifest default is `en-US`; the read aliases |
| 30 | + * exist so that such values stay valid. A code the table does not name is |
| 31 | + * accepted only as spelled (`fr-FR` is not `fr`). |
| 32 | + * - **A stored value in effect.** Offered under its own spelling when it is a |
| 33 | + * second spelling of an offered code, so the page shows it selected; and, |
| 34 | + * when the deployment no longer offers it, offered as itself, named as no |
| 35 | + * longer offered, so the page shows what is in effect rather than a blank. |
| 36 | + * It is not accepted again: only a write that changes the key is judged, so |
| 37 | + * the stored value keeps reading and keeps working until someone picks |
| 38 | + * another language. |
| 39 | + * |
| 40 | + * No i18n service in the composition, or one that reports no locale: the |
| 41 | + * source answers nothing and the manifest's declared four stand. |
| 42 | + */ |
| 43 | + |
| 44 | +import { resolveLocale } from '@objectstack/core'; |
| 45 | +import { PLATFORM_LOCALES, platformLocaleTagOf } from '@objectstack/spec/system'; |
| 46 | +import type { SettingsOptionSource, SettingsServedOption } from './settings-service.types.js'; |
| 47 | + |
| 48 | +/** The i18n service surface this source reads: the locale list the language menu reads. */ |
| 49 | +export interface LocaleListService { |
| 50 | + getLocales(): readonly string[]; |
| 51 | +} |
| 52 | + |
| 53 | +const TABLE_LABEL = new Map<string, string>(PLATFORM_LOCALES.map((row) => [row.tag, row.label])); |
| 54 | + |
| 55 | +/** |
| 56 | + * A locale's name in its own language: the platform locale table's name for |
| 57 | + * the language the code means, else `Intl.DisplayNames` in the code itself, |
| 58 | + * capitalized as the table's names are, else the code. Every failure answers |
| 59 | + * the code, which is still an honest, selectable name: `Intl.DisplayNames` |
| 60 | + * may be absent, throws `RangeError` on a malformed tag, and answers the code |
| 61 | + * back for a language its ICU data does not name. |
| 62 | + */ |
| 63 | +export function localeSelfName(code: string): string { |
| 64 | + const tag = platformLocaleTagOf(code); |
| 65 | + const fromTable = tag ? TABLE_LABEL.get(tag) : undefined; |
| 66 | + if (fromTable) return fromTable; |
| 67 | + try { |
| 68 | + const name = new Intl.DisplayNames([code], { type: 'language' }).of(code); |
| 69 | + if (!name || name === code) return code; |
| 70 | + return name.charAt(0).toLocaleUpperCase(code) + name.slice(1); |
| 71 | + } catch { |
| 72 | + return code; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** "no longer offered", in the reader's language. */ |
| 77 | +export const NOT_OFFERED_COPY: Record<'en' | 'zh-CN' | 'ja-JP' | 'es-ES', string> = { |
| 78 | + en: 'no longer offered', |
| 79 | + 'zh-CN': '已不再提供', |
| 80 | + 'ja-JP': '現在は提供されていません', |
| 81 | + 'es-ES': 'ya no disponible', |
| 82 | +}; |
| 83 | + |
| 84 | +const COPY_LOCALES = Object.keys(NOT_OFFERED_COPY); |
| 85 | + |
| 86 | +/** The "no longer offered" words for a reader's locale: exact, then by language, else English. */ |
| 87 | +export function notOfferedCopy(locale: string | undefined): string { |
| 88 | + const picked = locale ? resolveLocale(locale, COPY_LOCALES) : undefined; |
| 89 | + return NOT_OFFERED_COPY[(picked ?? 'en') as keyof typeof NOT_OFFERED_COPY]; |
| 90 | +} |
| 91 | + |
| 92 | +/** The locale codes the i18n service reports, de-duplicated, in its order. */ |
| 93 | +function offeredCodes(i18n: LocaleListService | undefined): string[] { |
| 94 | + const reported = i18n && typeof i18n.getLocales === 'function' ? i18n.getLocales() : undefined; |
| 95 | + if (!Array.isArray(reported)) return []; |
| 96 | + const seen = new Set<string>(); |
| 97 | + for (const code of reported) { |
| 98 | + if (typeof code === 'string' && code.trim()) seen.add(code.trim()); |
| 99 | + } |
| 100 | + return [...seen]; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * The option source of `localization.locale`. `i18n` is asked at each use |
| 105 | + * (the i18n service registers after this plugin's `init`); it answers |
| 106 | + * `undefined` when the composition carries none. |
| 107 | + */ |
| 108 | +export function localizationLocaleOptionSource( |
| 109 | + i18n: () => LocaleListService | undefined, |
| 110 | +): SettingsOptionSource { |
| 111 | + return ({ current, locale } = {}) => { |
| 112 | + const codes = offeredCodes(i18n()); |
| 113 | + if (codes.length === 0) return undefined; |
| 114 | + |
| 115 | + // The table language each offered code means, where the table names one. |
| 116 | + const offeredTags = new Set(codes.map((code) => platformLocaleTagOf(code)).filter(Boolean)); |
| 117 | + const admits = (value: string): boolean => { |
| 118 | + if (codes.includes(value)) return true; |
| 119 | + const tag = platformLocaleTagOf(value); |
| 120 | + return tag !== undefined && offeredTags.has(tag); |
| 121 | + }; |
| 122 | + |
| 123 | + const options: SettingsServedOption[] = codes.map((code) => ({ value: code, label: localeSelfName(code) })); |
| 124 | + |
| 125 | + if (typeof current === 'string' && current.trim() && !codes.includes(current)) { |
| 126 | + const tag = platformLocaleTagOf(current); |
| 127 | + const sameLanguage = tag === undefined ? -1 : codes.findIndex((code) => platformLocaleTagOf(code) === tag); |
| 128 | + if (sameLanguage !== -1) { |
| 129 | + // Stored as a second spelling (`en-US` where `en` is offered): offer |
| 130 | + // that language under the stored spelling, so the page shows it chosen. |
| 131 | + options[sameLanguage] = { value: current, label: options[sameLanguage].label }; |
| 132 | + } else { |
| 133 | + options.push({ value: current, label: `${localeSelfName(current)} (${notOfferedCopy(locale)})` }); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return { options, admits }; |
| 138 | + }; |
| 139 | +} |
| 140 | + |
| 141 | +/** The settings service surface {@link registerLocalizationLocaleOptions} needs. */ |
| 142 | +export interface LocaleOptionsHost { |
| 143 | + registerOptionSource(namespace: string, key: string, source: SettingsOptionSource): void; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Serve `localization.locale`'s options from the i18n service. Called before |
| 148 | + * the manifest registers, so the manifest's registration-time audit of |
| 149 | + * `OS_LOCALIZATION_LOCALE` leaves the key to be judged when it is read. |
| 150 | + */ |
| 151 | +export function registerLocalizationLocaleOptions( |
| 152 | + host: LocaleOptionsHost, |
| 153 | + i18n: () => LocaleListService | undefined, |
| 154 | +): void { |
| 155 | + host.registerOptionSource('localization', 'locale', localizationLocaleOptionSource(i18n)); |
| 156 | +} |
0 commit comments