diff --git a/packages/shell/src/frame/appI18n.ts b/packages/shell/src/frame/appI18n.ts index e83455f..8f5f155 100644 --- a/packages/shell/src/frame/appI18n.ts +++ b/packages/shell/src/frame/appI18n.ts @@ -21,6 +21,8 @@ export type AppLanguage = { englishName: string; dir: 'ltr' | 'rtl'; status: 'source' | 'machine' | 'reviewed'; + /** `supported`: automatic language choices may resolve to it. `community`: only when picked explicitly. */ + tier: 'supported' | 'community'; reviewers: string[]; fallback?: string; /** Share of English messages this catalog translates (its fallback's included), 0..1. */ diff --git a/packages/shell/src/frame/language.ts b/packages/shell/src/frame/language.ts index 0787da6..7de33a9 100644 --- a/packages/shell/src/frame/language.ts +++ b/packages/shell/src/frame/language.ts @@ -32,6 +32,12 @@ export type LanguageOptions = { native: { invoke: Invoke; listen: Listen }; /** Where the choice is kept for the app's other local windows (default `wb.language`). */ storageKey?: string; + /** + * The supported tier: automatic choices ("Follow the site", "Follow system") resolve only to + * these (English is always one). Any other catalog is a community language: it applies only + * when picked explicitly. Default: every catalog is supported. + */ + supported?: readonly string[]; }; type NativeLanguage = { preference?: string; locale?: string; system?: string[] }; @@ -40,11 +46,32 @@ type Stored = { preference: LanguagePreference; locale: string; siteLang?: strin /** Where the shell keeps its choice for the app's other local windows (localStorage). */ export const DEFAULT_STORAGE_KEY = `wb.language`; -/** The catalog to use for a preference, the site's reported language and the OS preferences. */ -export function resolveLocale(preference: LanguagePreference, siteLang: string | null, system: readonly string[], available: readonly string[]): string { - if (preference === 'site') return matchLocale(siteLang ? [siteLang] : system, available, siteLang ? matchLocale(system, available) : 'en'); - if (preference === 'system') return matchLocale(system, available); - return matchLocale([preference], available, matchLocale(system, available)); +/** + * The catalog to use for a preference, the site's reported language and the OS preferences. + * Automatic choices ("site", "system") only resolve to `supported` languages (default: all of + * `available`), through the normal fallback chain (pt-BR -> pt, zh-TW -> zh-Hant); a language + * picked explicitly applies whatever its tier. + */ +export function resolveLocale(preference: LanguagePreference, siteLang: string | null, system: readonly string[], available: readonly string[], supported: readonly string[] = available): string { + if (preference === 'site') { + if (!siteLang) return matchLocale(system, supported, 'en'); + const site = matchLocale([siteLang], supported, ''); + if (site) return site; + // The site's language is a community translation: English (it is offered as a switch), not the system language. + return matchLocale([siteLang], available, '') ? 'en' : matchLocale(system, supported); + } + if (preference === 'system') return matchLocale(system, supported); + return matchLocale([preference], available, matchLocale(system, supported)); +} + +/** + * The community language an automatic choice would have used had it been supported (the site's or + * the system's), or null: the panel offers it as a one-click switch. + */ +export function communitySuggestion(preference: LanguagePreference, siteLang: string | null, system: readonly string[], available: readonly string[], supported: readonly string[] = available): string | null { + if (preference !== 'site' && preference !== 'system') return null; + const wanted = resolveLocale(preference, siteLang, system, available, available); + return supported.includes(wanted) ? null : wanted; } export type Language = ReturnType; @@ -53,6 +80,7 @@ export function createLanguage(options: LanguageOptions) { const { i18n, nativePrefixes, native } = options; const storageKey = options.storageKey ?? DEFAULT_STORAGE_KEY; const available = i18n.available; + const supported = options.supported ? ['en', ...options.supported.filter(code => available.includes(code))] : available; const current = { preference: 'site' as LanguagePreference, siteLang: null as string | null, @@ -88,7 +116,7 @@ export function createLanguage(options: LanguageOptions) { let syncing: Promise = Promise.resolve(); async function apply() { - const locale = resolveLocale(current.preference, current.siteLang, current.system, available); + const locale = resolveLocale(current.preference, current.siteLang, current.system, available, supported); await i18n.setLocale(locale); paint(i18n.locale); writeStored({ preference: current.preference, locale: i18n.locale, siteLang: current.siteLang }); @@ -130,7 +158,7 @@ export function createLanguage(options: LanguageOptions) { /** Another local window (a popover) and previews: the shell's last choice, kept current through storage events. */ async function initFollower(forced?: string | null): Promise { const pick = async () => { - const locale = forced ? matchLocale([forced], available) : readStored()?.locale ?? matchLocale(browserLanguages(), available); + const locale = forced ? matchLocale([forced], available) : readStored()?.locale ?? matchLocale(browserLanguages(), supported); await i18n.setLocale(locale); paint(i18n.locale); }; @@ -141,7 +169,10 @@ export function createLanguage(options: LanguageOptions) { } function state() { - return { preference: current.preference, siteLang: current.siteLang, system: current.system, locale: i18n.locale }; + return { + preference: current.preference, siteLang: current.siteLang, system: current.system, locale: i18n.locale, + community: communitySuggestion(current.preference, current.siteLang, current.system, available, supported), + }; } async function set(preference: LanguagePreference): Promise { diff --git a/packages/shell/src/panel/LanguageSettings.tsx b/packages/shell/src/panel/LanguageSettings.tsx index 95df872..4ade9d9 100644 --- a/packages/shell/src/panel/LanguageSettings.tsx +++ b/packages/shell/src/panel/LanguageSettings.tsx @@ -1,12 +1,15 @@ // Settings → Language: follow the site, follow the system, or pick one of the languages the app has -// a catalog for (`PanelSite.languages`, the app's frame/language.ts state). Machine-drafted -// translations say so and link to the app's contributor guide. +// a catalog for (`PanelSite.languages`, the app's frame/language.ts state). Supported languages come +// first, community translations (machine drafts nobody has reviewed) in a group below; the two +// automatic choices only pick supported ones, so a community language the site or system uses is +// offered as a one-click switch. Machine-drafted translations link to the app's contributor guide. import { useMemo, useSyncExternalStore } from 'react'; import { invoke } from '../frame/bridge.ts'; import { useT } from '../frame/hooks.ts'; import { formatNumber } from '../frame/i18n.ts'; import type { LanguagePreference } from '../frame/language.ts'; import { Badge, Row, Section, Select, type Option } from './controls.tsx'; +import { groupLanguages } from './languageModel.ts'; import { panelSite, type PanelLanguages } from './site.ts'; /** The language state as a store for React (the snapshot only changes when the state does). */ @@ -14,7 +17,7 @@ function languageStore(languages: PanelLanguages) { let cached = languages.state(); const snapshot = () => { const next = languages.state(); - if (next.preference !== cached.preference || next.siteLang !== cached.siteLang || next.locale !== cached.locale || next.system !== cached.system) cached = next; + if (next.preference !== cached.preference || next.siteLang !== cached.siteLang || next.locale !== cached.locale || next.system !== cached.system || next.community !== cached.community) cached = next; return cached; }; return { subscribe: (listener: () => void) => languages.subscribe(listener), snapshot }; @@ -27,17 +30,20 @@ export function LanguageSettings() { const store = useMemo(() => languageStore(languages), [languages]); const current = useSyncExternalStore(store.subscribe, store.snapshot, store.snapshot); const active = LANGUAGES.find(language => language.code === current.locale); + const { supported, community } = groupLanguages(LANGUAGES); const siteLanguage = current.siteLang ? LANGUAGES.find(language => language.code === current.siteLang) : null; const options: Array> = [ { value: 'site', label: t('wb.settings.language.followSite'), hint: siteLanguage ? t('wb.settings.language.followSiteNow', { language: siteLanguage.name }) : t('wb.settings.language.followSiteHint') }, { value: 'system', label: t('wb.settings.language.followSystem'), hint: t('wb.settings.language.followSystemHint') }, - ...LANGUAGES.map(language => ({ + ...[...supported, ...community].map(language => ({ value: language.code, label: language.name, hint: language.name === language.englishName ? undefined : language.englishName, style: { direction: language.dir }, + group: community.length ? (language.tier === 'community' ? t('wb.settings.language.groupCommunity') : t('wb.settings.language.groupSupported')) : undefined, })), ]; + const suggested = current.community ? LANGUAGES.find(language => language.code === current.community) : null; const guide = languages.translateUrl; const open = () => { if (guide) invoke('open_external_url', { url: guide }).catch(() => window.open(guide, '_blank', 'noopener')); }; const machine = active && active.status === 'machine'; @@ -48,6 +54,9 @@ export function LanguageSettings() {