Skip to content
27 changes: 27 additions & 0 deletions .changeset/33634-serve-labels-by-source-locale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
'@objectstack/spec': minor
'@objectstack/core': minor
'@objectstack/runtime': minor
'@objectstack/service-i18n': minor
'@objectstack/rest': minor
'@objectstack/objectql': minor
'@objectstack/plugin-email': minor
'@objectstack/service-analytics': minor
'@objectstack/service-storage': minor
---

feat(i18n): the serving layer reads `i18n.sourceLocale` as the authored label's language

Clause-②: yes
Metadata-protocol: semantic

`II18nService` (`@objectstack/spec/contracts`) gains an optional `getSourceLocale()` / `setSourceLocale(locale)` pair, shaped like the fallback pair; `FileI18nAdapter` and the kernel's in-memory fallback implement both.

An app authored in one language that defaults its users to another now serves its inline labels to a request in the authored language. Take `defaultLocale: 'zh-CN'`, `sourceLocale: 'en'`, English inline labels, a `zh-CN` bundle and no `fallbackLocale`:

- Before: an `en` request was not treated as the authored language, walked the fallback chain (`zh-CN`) ahead of the inline text and was served the Chinese bundle row, unless the stack also declared `fallbackLocale: 'en'`.
- Now: an `en` request answers the English inline label; a `zh-CN` request answers the `zh-CN` row; any other locale walks the declared fallback chain exactly as before.

`AppPlugin` threads the declared `i18n.sourceLocale` into the i18n service, and every metadata-document translation (the REST metadata reads, a formula's `label()`, an email template's `option` hole, analytics select-option labels and the upload-link page) passes `getSourceLocale()`, else `getDefaultLocale()`, as `ResolveOptions.defaultLocale`. A stack that declares no `sourceLocale` is served exactly as before. A `fallbackLocale: 'en'` added only to work around this can stay; it is no longer needed for the `en` request.

Writing your own i18n provider: implement `setSourceLocale` to receive the declaration and `getSourceLocale` to report it, answering `undefined` when nothing was declared — never `defaultLocale`; the serving layer does that collapse itself.
13 changes: 8 additions & 5 deletions content/docs/kernel/services-checklist.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,18 @@ AppPlugin will, in this order:
1. Set the default locale via `i18nService.setDefaultLocale()`
2. Set the declared `i18n.fallbackLocale` — the locale `t()` consults after the requested
one — via `i18nService.setFallbackLocale()` (#15694)
3. Narrow what `getLocales()` REPORTS to the app's declared `supportedLocales`, via
3. Set the declared `i18n.sourceLocale` — the language the inline labels are written in —
via `i18nService.setSourceLocale()` (#33634); the serving layer reads it back through
`getSourceLocale()`, else `getDefaultLocale()`, as the authored label's language
4. Narrow what `getLocales()` REPORTS to the app's declared `supportedLocales`, via
`i18nService.setSupportedLocales()` (#7679)
4. Call `i18nService.loadTranslations(locale, data)` for each locale in every bundle
5. Skip gracefully if no i18n service is registered (no errors, just a debug log)
5. Call `i18nService.loadTranslations(locale, data)` for each locale in every bundle
6. Skip gracefully if no i18n service is registered (no errors, just a debug log)

Each of the three setters is optional on `II18nService` and applied through the same
Each of the four setters is optional on `II18nService` and applied through the same
`typeof ... === 'function'` capability probe, so a provider that has not implemented one
keeps its own behaviour rather than breaking. `FileI18nAdapter` implements
`setDefaultLocale` and `setSupportedLocales` but **not** `setFallbackLocale`: both boot
`setDefaultLocale`, `setSourceLocale` and `setSupportedLocales` but **not** `setFallbackLocale`: both boot
paths (`os serve` and `DevPlugin`'s auto-wiring) construct it with
`fallbackLocale || defaultLocale || 'en'` already collapsed from the stack config, so the
probe skips step 2 and it keeps the value it was built with — omitting a setter is
Expand Down
8 changes: 4 additions & 4 deletions content/docs/protocol/kernel/i18n-standard.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ Fallback to: en (system default) ✓

The "system default" step is the stack's **declared** fallback
(`i18n.fallbackLocale`, else `defaultLocale`), never a literal `en`. For
metadata labels one more rule applies (#15711): the inline `label:` authored
on the metadata **is** the default locale's text, so a request for
`defaultLocale` stops at the authored label without consulting any other
locale's bundle — a `zh-CN` workspace with a courtesy `en` bundle serves
metadata labels one more rule applies (#15711, #33634): the inline `label:`
authored on the metadata **is** the source locale's text (`sourceLocale`, else
`defaultLocale`), so a request for that locale stops at the authored label
without consulting any other locale's bundle — a `zh-CN` workspace with a courtesy `en` bundle serves
Chinese to `zh-CN` and English to `en`. See
[Translations](/docs/ui/translations#how-a-locale-is-chosen).

Expand Down
2 changes: 1 addition & 1 deletion content/docs/references/system/translation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ Internationalization configuration
| **defaultLocale** | `string` | ✅ | Default locale (e.g., "en") |
| **supportedLocales** | `string[]` | ✅ | Supported BCP-47 locale codes |
| **fallbackLocale** | `string` | optional | Fallback locale code |
| **sourceLocale** | `string` | optional | Language the inline metadata text (a plain-string label) is written in, when it differs from defaultLocale (e.g. "en" for an English-authored app whose users default to "zh-CN"). Absent, it is defaultLocale. Read by os lint, os i18n check and os i18n extract as the source language; their --default-locale flag overrides it. Not yet read by the serving layer: the runtime label resolvers still treat defaultLocale as the authored label's language. |
| **sourceLocale** | `string` | optional | Language the inline metadata text (a plain-string label) is written in, when it differs from defaultLocale (e.g. "en" for an English-authored app whose users default to "zh-CN"). Absent, it is defaultLocale. Read by os lint, os i18n check and os i18n extract as the source language; their --default-locale flag overrides it. The serving layer reads it as the authored label's language too: a request in this locale is answered from the inline text. |


---
Expand Down
25 changes: 13 additions & 12 deletions content/docs/ui/translations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,10 @@ the next best text.

Two rules keep the declaration honest (#14882, #15711):

- **The authored label is the default locale's text.** A request for
`defaultLocale` consults that locale's own bundle and then answers with the
inline `label:` — it never walks the fallback chain. A stack declaring
- **The authored label is the source locale's text.** A request for the
language the inline labels are written in — `sourceLocale` when declared,
else `defaultLocale` (#33634) — consults that locale's own bundle and then
answers with the inline `label:`; it never walks the fallback chain. A stack declaring
`defaultLocale: 'zh-CN'` with `fallbackLocale: 'en'` and only an `en` bundle
serves its authored Chinese to a `zh-CN` request, and its `en` bundle to
everyone else. Shipping a `zh-CN` bundle (`os i18n extract --locales=zh-CN`)
Expand Down Expand Up @@ -372,9 +373,9 @@ default one, and your inline `label:` is already that locale's text — so there
is no gap to report and no need to reach for `--skip-i18n`. The same holds if
your source language isn't English: declare `defaultLocale: 'zh-CN'` and the
tooling stops asking for English you never promised. The runtime reads the
declaration the same way (#15711): a request for the default locale answers
with the authored label, so what the gate counts as covered is what gets
served.
declaration the same way (#15711, #33634): a request for the source locale
answers with the authored label, so what the gate counts as covered is what
gets served.

#### When users default to a language you did not author in

Expand All @@ -400,12 +401,12 @@ language every user without a preference is served. Declare `defaultLocale: 'zh-
reads your English labels as Chinese: it reports every key missing in `en` and
never reports a `zh-CN` gap.

`sourceLocale` is read by the authoring tools only. The runtime's
authored-label rule above still keys on `defaultLocale`, so in this setup it is
the `zh-CN` request that skips the fallback chain, and an `en` request walks
the declared fallback (`fallbackLocale`, else `defaultLocale`) before it
reaches the English inline label. Declare `fallbackLocale: 'en'` to keep an
`en` request on the authored text.
The runtime reads `sourceLocale` too (#33634): its authored-label rule above
keys on it, so in this setup an `en` request answers the English inline label
without walking the fallback chain, a `zh-CN` request is served the `zh-CN`
rows, and any other locale walks the declared fallback (`fallbackLocale`, else
`defaultLocale`) before it reaches the inline text. No `fallbackLocale: 'en'`
is needed to keep an `en` request on the authored text.

Translating is therefore opt-in, but once you opt in it covers the **whole**
declared surface — every row of the table above, including action labels
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/fallbacks/fallbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,28 @@ describe('createMemoryI18n declared fallbackLocale (#15694)', () => {
expect(typeof i18n.setFallbackLocale).toBe('function');
expect(i18n.getFallbackLocale).toBeUndefined();
});

// #33634 — the source-locale pair: unlike the fallback pair, both halves
// exist, because the accessor answers only what it was told.
it('getSourceLocale answers undefined until setSourceLocale is called, then exactly what it was told', () => {
const i18n = createMemoryI18n();
i18n.setDefaultLocale('zh-CN');
// Never derived from `defaultLocale`: that collapse is the caller's.
expect(i18n.getSourceLocale()).toBeUndefined();
i18n.setSourceLocale('en');
expect(i18n.getSourceLocale()).toBe('en');
expect(i18n.getDefaultLocale()).toBe('zh-CN');
});

it('setSourceLocale changes no t() lookup', () => {
const i18n = createMemoryI18n();
i18n.setDefaultLocale('zh-CN');
i18n.loadTranslations('en', { greeting: 'Hello' });
i18n.loadTranslations('zh-CN', { other: '其他' });
const before = [i18n.t('greeting', 'zh-CN'), i18n.t('greeting', 'en'), i18n.t('other', 'en')];
i18n.setSourceLocale('en');
expect([i18n.t('greeting', 'zh-CN'), i18n.t('greeting', 'en'), i18n.t('other', 'en')]).toEqual(before);
});
});

// #30456: the one matcher for "is this locale offered here, and as which code",
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/fallbacks/memory-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export function createMemoryI18n() {
// must leave `t()` answering exactly as it did before this existed — an app
// that never wrote a `fallbackLocale` is not opting into a longer chain.
let fallbackLocale: string | undefined;
// [#33634] The app's DECLARED `i18n.sourceLocale` — the language its inline
// labels are written in — injected by `AppPlugin.loadTranslations` beside
// the other three. `undefined` means the app declared nothing, and the
// serving layer then reads `defaultLocale` as the authored language, as it
// always has. It changes no `t()` lookup.
let sourceLocale: string | undefined;

/**
* Resolve a dot-notation key from a nested object.
Expand Down Expand Up @@ -257,5 +263,22 @@ export function createMemoryI18n() {
setFallbackLocale(locale: string): void {
fallbackLocale = locale;
},

/**
* @see II18nService.getSourceLocale — [#33634]
*
* Unlike the fallback pair, the accessor IS implemented here: it answers
* only what {@link setSourceLocale} was told, `undefined` otherwise, so it
* never has to derive a value — the collapse to `defaultLocale` is the
* serving layer's, not this provider's.
*/
getSourceLocale(): string | undefined {
return sourceLocale;
},

/** @see II18nService.setSourceLocale — [#33634] */
setSourceLocale(locale: string): void {
sourceLocale = locale;
},
};
}
27 changes: 27 additions & 0 deletions packages/objectql/src/engine-formula-option-label.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,30 @@ describe('#26608 — label() reads the option label in the reader\'s locale', ()
expect(b.display_name).toBe('v2 · Clean');
});
});

describe('#33634 — the declared sourceLocale, not defaultLocale, is the authored option label\'s language', () => {
// An app authored in English that defaults its users to zh-CN: `defaultLocale:
// 'zh-CN'`, `sourceLocale: 'en'`, no `fallbackLocale` (collapsed to zh-CN by
// both boot paths), English inline option labels, a zh-CN pack.
const enSource = (overrides: Record<string, unknown> = {}) => makeI18n({
getDefaultLocale: () => 'zh-CN',
getFallbackLocale: () => 'zh-CN',
getSourceLocale: () => 'en',
...overrides,
});
const en = { locale: 'en' } as never;

it('an en reader gets the inline English label; a zh-CN reader gets the pack', async () => {
const { engine } = await makeEngine(enSource());
const a = await engine.insert('ol_version', { version_no: 2, kind: 'clean' }, { context: en }) as Rec;
expect(a.display_name).toBe('v2 · Clean');
const b = await engine.insert('ol_version', { version_no: 3, kind: 'redline' }, { context: zhCN }) as Rec;
expect(b.display_name).toBe('v3 · 修订稿');
});

it('CONTROL — no sourceLocale declared: an en reader walks the chain to zh-CN, as before', async () => {
const { engine } = await makeEngine(enSource({ getSourceLocale: () => undefined }));
const a = await engine.insert('ol_version', { version_no: 2, kind: 'clean' }, { context: en }) as Rec;
expect(a.display_name).toBe('v2 · 清稿');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class ReferencesAndMasterDetailFacet {
getTranslations?: (locale: string) => Record<string, unknown>;
getFallbackLocale?: () => string | undefined;
getDefaultLocale?: () => string | undefined;
getSourceLocale?: () => string | undefined;
}): void {
this.i18nService = service;
this.logger.info('I18nService configured for validation messages');
Expand Down
2 changes: 2 additions & 0 deletions packages/objectql/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ export class ObjectQL implements IObjectQLEngine {
getTranslations?: (locale: string) => Record<string, unknown>;
getFallbackLocale?: () => string | undefined;
getDefaultLocale?: () => string | undefined;
getSourceLocale?: () => string | undefined;
};

// Crypto provider backing `secret`-typed fields. Optional: when absent,
Expand Down Expand Up @@ -2542,6 +2543,7 @@ export class ObjectQL implements IObjectQLEngine {
getTranslations?: (locale: string) => Record<string, unknown>;
getFallbackLocale?: () => string | undefined;
getDefaultLocale?: () => string | undefined;
getSourceLocale?: () => string | undefined;
}): void {
// [#29986 codemod] The body moved to `engine-references-and-master-detail.ts` (see `ReferencesAndMasterDetailHost`).
return ReferencesAndMasterDetailFacet.setI18nService.call(referencesAndMasterDetailHost.of(this), service);
Expand Down
9 changes: 8 additions & 1 deletion packages/objectql/src/formula-option-labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface OptionLabelI18n {
getTranslations?: (locale: string) => Record<string, unknown>;
getFallbackLocale?: () => string | undefined;
getDefaultLocale?: () => string | undefined;
/** [#33634] The declared `i18n.sourceLocale`; read before `getDefaultLocale` as the authored language. */
getSourceLocale?: () => string | undefined;
}

/** The slice of a formula plan entry this module reads: its expression. */
Expand Down Expand Up @@ -153,7 +155,12 @@ export function readerOptionLabels(
const bundle = bundleOf(i18n);
if (!bundle) return labelMap(fields);
const fallback = typeof i18n.getFallbackLocale === 'function' ? i18n.getFallbackLocale() : undefined;
const defaultLocale = typeof i18n.getDefaultLocale === 'function' ? i18n.getDefaultLocale() : undefined;
// [#33634] The authored language: the declared `sourceLocale`, else the
// users' `defaultLocale` (the metadata route's `metaTranslateOptions` rule).
const source = typeof i18n.getSourceLocale === 'function' ? i18n.getSourceLocale() : undefined;
const defaultLocale = typeof source === 'string' && source.length > 0
? source
: typeof i18n.getDefaultLocale === 'function' ? i18n.getDefaultLocale() : undefined;
const translated = translateObject({ name, fields }, bundle, {
locale,
...(typeof fallback === 'string' && fallback.length > 0 ? { fallbackChain: [fallback] } : {}),
Expand Down
9 changes: 8 additions & 1 deletion packages/plugins/plugin-email/src/option-label-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export interface OptionLabelI18n {
getTranslations?: (locale: string) => Record<string, unknown>;
getFallbackLocale?: () => string | undefined;
getDefaultLocale?: () => string | undefined;
/** [#33634] The declared `i18n.sourceLocale`; read before `getDefaultLocale` as the authored language. */
getSourceLocale?: () => string | undefined;
}

/**
Expand Down Expand Up @@ -130,7 +132,12 @@ function servedOptionFields(
const bundle = bundleOf(i18n);
if (!bundle) return optionFields;
const fallback = typeof i18n.getFallbackLocale === 'function' ? i18n.getFallbackLocale() : undefined;
const defaultLocale = typeof i18n.getDefaultLocale === 'function' ? i18n.getDefaultLocale() : undefined;
// [#33634] The authored language: the declared `sourceLocale`, else the
// users' `defaultLocale` (the metadata route's `metaTranslateOptions` rule).
const source = typeof i18n.getSourceLocale === 'function' ? i18n.getSourceLocale() : undefined;
const defaultLocale = typeof source === 'string' && source.length > 0
? source
: typeof i18n.getDefaultLocale === 'function' ? i18n.getDefaultLocale() : undefined;
const translated = translateObject({ name, fields: optionFields }, bundle, {
locale,
...(typeof fallback === 'string' && fallback.length > 0 ? { fallbackChain: [fallback] } : {}),
Expand Down
Loading
Loading