2020 *
2121 * The server resolves translated metadata through the deployment's declared
2222 * fallback chain: the requested locale, then each fallback, then the authored
23- * text. `GET /api/v1/i18n/locales` names that chain (`fallbackChain`) and the
24- * authored language (`sourceLocale`). The loader reads both beside the bundle
25- * and, unless `lang` is the authored language (the server walks no chain
26- * then), fetches each fallback locale's bundle too, keeping its `flows`
27- * group. It lays them beside the app namespace
23+ * text. `GET /api/v1/i18n/locales` names that chain (`fallbackChain`), the
24+ * authored language (`sourceLocale`) and the locales the server holds
25+ * (`locales[].code`). The loader reads them beside the bundle and first
26+ * negotiates `lang` against those codes, as the server negotiates a request's
27+ * tag before it resolves (`resolveLocaleAmong(lang, codes) ?? lang`;
28+ * `negotiatedTranslationLocale` in `@objectstack/rest`): a `zh-Hans` or `zh`
29+ * console on a stack holding `zh-CN` is the `zh-CN` request. Unless that
30+ * negotiated language is the authored one (the server walks no chain then),
31+ * it fetches each fallback locale's bundle too, keeping its `flows` group. It lays them beside the app namespace
2832 * (`withDeclaredFlowsFallback`, `@object-ui/i18n`), where the flow-label
2933 * readers find them, so a flow label `lang` does not carry reads the fallback
3034 * locale's text, as the server's own reads do. A server that declares no chain
@@ -38,6 +42,7 @@ import {
3842 type DeclaredFlowsFallback ,
3943 type FlowsTranslation ,
4044} from '@object-ui/i18n' ;
45+ import { resolveLocaleAmong } from '@objectstack/spec/system' ;
4146import { i18nFetch , whenSessionAnswered } from './i18nSession' ;
4247
4348const serverUrl = ( ) => import . meta. env . VITE_SERVER_URL || '' ;
@@ -56,11 +61,19 @@ async function fetchTranslations(lang: string): Promise<Record<string, unknown>
5661 return json ;
5762}
5863
64+ interface DeclaredChain {
65+ fallbackChain : string [ ] ;
66+ sourceLocale ?: string ;
67+ /** `locales[].code`: what the server negotiates a request's tag against. */
68+ codes : string [ ] ;
69+ }
70+
5971/**
60- * The declared chain and authored language off `GET /api/v1/i18n/locales`,
61- * or `null` when the server declares no chain or the read fails.
72+ * The declared chain, authored language and held locales off
73+ * `GET /api/v1/i18n/locales`, or `null` when the server declares no chain or
74+ * the read fails.
6275 */
63- async function fetchDeclaredChain ( ) : Promise < { fallbackChain : string [ ] ; sourceLocale ?: string } | null > {
76+ async function fetchDeclaredChain ( ) : Promise < DeclaredChain | null > {
6477 try {
6578 const res = await i18nFetch ( `${ serverUrl ( ) } /api/v1/i18n/locales` ) ;
6679 if ( ! res . ok ) return null ;
@@ -70,7 +83,11 @@ async function fetchDeclaredChain(): Promise<{ fallbackChain: string[]; sourceLo
7083 const fallbackChain = chain . filter ( ( code ) : code is string => typeof code === 'string' && code . length > 0 ) ;
7184 if ( fallbackChain . length === 0 ) return null ;
7285 const sourceLocale = typeof body ?. sourceLocale === 'string' && body . sourceLocale ? body . sourceLocale : undefined ;
73- return { fallbackChain, sourceLocale } ;
86+ const descriptors : unknown [ ] = Array . isArray ( body ?. locales ) ? body . locales : [ ] ;
87+ const codes = descriptors
88+ . map ( ( d ) => ( d && typeof d === 'object' ? ( d as { code ?: unknown } ) . code : undefined ) )
89+ . filter ( ( code ) : code is string => typeof code === 'string' && code . length > 0 ) ;
90+ return { fallbackChain, sourceLocale, codes } ;
7491 } catch {
7592 return null ;
7693 }
@@ -80,19 +97,21 @@ const sameTag = (a: string, b: string) => a.toLowerCase() === b.toLowerCase();
8097
8198/**
8299 * The fallback locales' `flows` groups for `lang`, or `undefined` when there
83- * is nothing to lay beside its bundle: no declared chain, or `lang` is the
84- * authored language (the resolvers walk no chain for it).
100+ * is nothing to lay beside its bundle: no declared chain, or `lang`,
101+ * negotiated against the server's locales, is the authored language (the
102+ * resolvers walk no chain for it).
85103 */
86104async function declaredFlowsFallback (
87105 lang : string ,
88- declared : { fallbackChain : string [ ] ; sourceLocale ?: string } | null ,
106+ declared : DeclaredChain | null ,
89107) : Promise < DeclaredFlowsFallback | undefined > {
90108 if ( ! declared ) return undefined ;
91- if ( declared . sourceLocale && sameTag ( lang , declared . sourceLocale ) ) return undefined ;
109+ const locale = resolveLocaleAmong ( lang , declared . codes ) ?? lang ;
110+ if ( declared . sourceLocale && sameTag ( locale , declared . sourceLocale ) ) return undefined ;
92111 const bundles : DeclaredFlowsFallback [ 'bundles' ] = { } ;
93112 await Promise . all (
94113 declared . fallbackChain
95- . filter ( ( code ) => ! sameTag ( code , lang ) )
114+ . filter ( ( code ) => ! sameTag ( code , locale ) )
96115 . map ( async ( code ) => {
97116 try {
98117 const translations = await fetchTranslations ( code ) ;
@@ -104,6 +123,7 @@ async function declaredFlowsFallback(
104123 } ) ,
105124 ) ;
106125 return {
126+ locale,
107127 fallbackChain : declared . fallbackChain ,
108128 ...( declared . sourceLocale ? { defaultLocale : declared . sourceLocale } : { } ) ,
109129 bundles,
0 commit comments