mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
* perf(i18n): load built-in catalogs once, without copies or a runtime plural fill
Each catalog load parsed the JSON, rebuilt it to fill CLDR plural forms
from _other, then deep-copied and deep-merged it into the store (about
26 ms after parsing per 1.3 MB catalog), and concurrent loads of one
locale fetched it twice.
- sync:localization-catalog now commits the plural forms a locale's
CLDR rules need beside each _other (fr/es/pt many, tr one, a few
de/ru), and verify:localization-catalog fails when one is missing.
Plural forms may include {{count}} where English omits it.
- Renderer and main load built-in catalogs as-is; plugin packs keep the
runtime fill.
- Renderer catalog requests are shared per locale; a timed-out request
is dropped so a retry fetches again. Bundles are added with skipCopy.
* fix(i18n): require {{count}} in plural forms when English shows it
* refactor(i18n): share one i18next setup between main and renderer
- createOrcaI18n (src/shared/orca-i18n-instance.ts) owns the backend,
init options, plugin-pack registration, resource-language resolution
and the translate wrapper; main and renderer inject only how a
built-in catalog loads (dynamic import vs. fetch) and extra modules.
- Remove setRendererUiLanguage (tests only); tests load then switch.
- resolveUiLocale maps built-ins by lookup instead of an if-chain, and
catalog URLs/loaders are direct map lookups.
- Drop <I18nextProvider>: initReactI18next already binds the instance.
* refactor(i18n): native i18next plurals at call sites, plural-aware extraction
Call sites branched on count === 1 and passed literal _one/_other keys,
which resolvePluralFamilyKey stripped again so CLDR could pick the form.
They now call translate('x', otherDefault, { count, defaultValue_one })
and i18next selects the form natively; catalog keys stay x_one/x_other.
- Converted 284 families with an AST codemod plus a few by hand; added
defaultValue_one to 21 base-key families that relied on en.json.
- Extraction runs with plurals enabled, so both English defaults are
checked for drift. A key that only fills {{count}} (no defaultValue_one)
stays singular in en.json.
- verify/sync:localization-catalog expand a native plural call into its
x_one/x_other English entries.
- Removed 22 base keys that a sibling plural family shadowed in every
catalog.
* fix(i18n): pick English plural defaults by English rules; require count for plural refs
* refactor(i18n): convert plural sites merged from main; treat key-as-default plural extraction as dynamic
* fix(i18n): keep an undefined pass-through count absent; only dotted key-as-default is dynamic
* refactor(i18n): native plurals for exact-1 families; drop desktop suffix stripping
Twelve families kept special exact-1 copy ("Used the browser") beside an
_other-only family, so CLDR one (ru 21, 31) reused _other wording. They
now call the base key with an English _one translator source, the
exact-1 branch stays a separate key, and ru one forms get the right
noun ("21 раз"). With no suffixed call sites left, desktop translate
no longer strips plural suffixes; mobile keeps resolvePluralFamilyKey.
* test(i18n): update plural call expectations in two renderer tests
* refactor(i18n): convert a plural site merged from main
---------
Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
102 lines
4.0 KiB
TypeScript
102 lines
4.0 KiB
TypeScript
import i18next, {
|
|
type BackendModule,
|
|
type i18n as I18nInstance,
|
|
type InitOptions,
|
|
type Module,
|
|
type ReadCallback,
|
|
type TOptions
|
|
} from 'i18next'
|
|
import { isPseudoLocalizationLocale, pseudoLocalizeString } from './pseudo-localization'
|
|
import { DEFAULT_UI_LOCALE } from './ui-locale'
|
|
import type { PluginLanguagePackRegistration } from './plugins/artifacts/plugin-language-pack-artifact'
|
|
import { replacePluginLanguagePackResources } from './plugins/plugin-language-pack-i18n-resources'
|
|
|
|
export type OrcaI18n = {
|
|
i18n: I18nInstance
|
|
translate: (key: string, fallback: string, options?: TOptions) => string
|
|
/** Returns whether any pack's resources changed. */
|
|
setPluginLanguagePacks: (packs: readonly PluginLanguagePackRegistration[]) => boolean
|
|
getPluginLanguagePacks: () => readonly PluginLanguagePackRegistration[]
|
|
/** Maps a resolved UI locale to its i18next resource language; an unknown pack falls back to English. */
|
|
resolveResourceLanguage: (locale: string) => string
|
|
}
|
|
|
|
const ENGLISH_PLURAL_RULES = new Intl.PluralRules(DEFAULT_UI_LOCALE)
|
|
|
|
/** Main and renderer share one setup; only how a built-in catalog loads differs. */
|
|
export function createOrcaI18n(options: {
|
|
/** Undefined for English and unknown locales, which use caller fallbacks. */
|
|
loadCatalog: (language: string) => Promise<Record<string, unknown>> | undefined
|
|
englishResources: Record<string, unknown>
|
|
modules?: Module[]
|
|
initOptions?: InitOptions
|
|
}): OrcaI18n {
|
|
const i18n = i18next.createInstance()
|
|
let pluginLanguagePacks: readonly PluginLanguagePackRegistration[] = []
|
|
|
|
const backend: BackendModule = {
|
|
type: 'backend',
|
|
init: () => {},
|
|
read: (language: string, _namespace: string, callback: ReadCallback) => {
|
|
const load = options.loadCatalog(language)
|
|
if (!load) {
|
|
callback(null, false)
|
|
return
|
|
}
|
|
load.then(
|
|
(catalog) => callback(null, catalog),
|
|
(error) => callback(error instanceof Error ? error : new Error(String(error)), false)
|
|
)
|
|
}
|
|
}
|
|
|
|
i18n.use(backend)
|
|
for (const module of options.modules ?? []) {
|
|
i18n.use(module)
|
|
}
|
|
// In-memory resources make init synchronous, so callers can translate at module load.
|
|
void i18n.init({
|
|
fallbackLng: DEFAULT_UI_LOCALE,
|
|
lng: DEFAULT_UI_LOCALE,
|
|
// Default 'all' expands zh-Hant to [zh-Hant, zh, en], so missing keys would render Simplified.
|
|
load: 'currentOnly',
|
|
// Serve eager English from resources while the backend lazily supplies other languages.
|
|
partialBundledLanguages: true,
|
|
resources: { [DEFAULT_UI_LOCALE]: { translation: options.englishResources } },
|
|
// An empty catalog value falls back to English instead of rendering blank.
|
|
returnEmptyString: false,
|
|
interpolation: { escapeValue: false },
|
|
...options.initOptions
|
|
})
|
|
|
|
return {
|
|
i18n,
|
|
translate: (key, fallback, translateOptions) => {
|
|
const { defaultValue_one: one, count, ...others } = translateOptions ?? {}
|
|
// A pass-through `count: undefined` must interpolate like an absent count.
|
|
const rest = count === undefined ? others : { ...others, count }
|
|
// i18next would pick the English default by the active language's rule (ru 21 -> _one); English text needs English's.
|
|
const defaultValue =
|
|
typeof one === 'string' &&
|
|
typeof count === 'number' &&
|
|
ENGLISH_PLURAL_RULES.select(count) === 'one'
|
|
? one
|
|
: fallback
|
|
const value = i18n.t(key, { defaultValue, ...rest })
|
|
return isPseudoLocalizationLocale(i18n.language) ? pseudoLocalizeString(value) : value
|
|
},
|
|
setPluginLanguagePacks: (packs) => {
|
|
if (packs === pluginLanguagePacks) {
|
|
return false
|
|
}
|
|
const changed = replacePluginLanguagePackResources(i18n, pluginLanguagePacks, packs)
|
|
pluginLanguagePacks = packs
|
|
return changed.size > 0
|
|
},
|
|
getPluginLanguagePacks: () => pluginLanguagePacks,
|
|
resolveResourceLanguage: (locale) =>
|
|
pluginLanguagePacks.find((pack) => pack.id === locale)?.resourceLanguage ??
|
|
(locale.startsWith('plugin:') ? DEFAULT_UI_LOCALE : locale)
|
|
}
|
|
}
|