Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/36504-menu-rename-translation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/spec': minor
'@objectstack/runtime': patch
'@object-ui/app-shell': patch
'@object-ui/i18n': patch
---

feat(i18n): `GET /i18n/locales` names the source locale, and a design-mode menu rename outside it is stored as that language's translation

Clause-②: yes (widening)
Metadata-protocol: added
Skills-docs: follow-up owed — skills/objectstack-i18n/SKILL.md

- **`GET /api/v1/i18n/locales` names the source locale.** The body gains an optional `sourceLocale` (`GetLocalesResponseSchema.sourceLocale`): the language the app's own text, a plain-string `label`, is written in. It is the declared `i18n.sourceLocale`, else the default locale, the same rule the metadata translators use. It differs from the `isDefault` locale on an app written in one language that defaults its users to another. It is absent only when the server knows neither. Nothing that parsed before stops parsing.
- **A menu rename in design mode is stored where the reader's language reads it.** The name field of 「Rename and icon」 is now open in every language. A name typed in the source language, over an entry shown by its stored label, is still written to the stored label. Any other name is written as that language's translation of the entry: a name typed in another language, or one typed in the source language over a translation the reader was shown. The stored label stays unchanged, so every other language keeps its name. The rename says so when the new name will be a translation ("The new name is shown in LANGUAGE. The app's other languages keep their names.").
- **Where the translation goes.** It is written to the `translation` item `APP_menu_LOCALE` (for example `crm_menu_zh_cn`), at `apps.APP.navigation.ID.label`, the key app navigation is translated by. It goes into the package the menu is written to: the app's own package when that is writable, else its customization package, whose own translations are read first (ADR-0146 D2). Like the menu, it is saved as a draft with the first-write pin and published at once. A pending draft of it is refused and the reason is shown. A translation-only rename writes the translation and leaves the app alone.
- **A link added in another language** is stored without a name, and the name typed for it becomes that language's translation. The source language names it when someone sets a name there. Until then, a reader in the source language sees the link's URL, as for any link with no label.
- Before this change, the console allowed a rename only when the reader was in the deployment's default language. The default language could differ from the source language, and a rename there was written to the stored label for every language.
2 changes: 1 addition & 1 deletion content/docs/api/plugin-endpoints.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protocol `ai.chatStream` parses and owns message state for you.

| Method | Endpoint | Description |
|:-------|:---------|:------------|
| GET | `/i18n/locales` | List available locales |
| GET | `/i18n/locales` | List available locales, and the source locale (`sourceLocale`) |
| GET | `/i18n/translations/:locale` | Get translation bundle |
| GET | `/i18n/labels/:object/:locale` | Get field labels |

Expand Down
2 changes: 1 addition & 1 deletion content/docs/kernel/services-checklist.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ declares no `i18n` block must not clear what another declared.

| Method | Path | Description |
|:-------|:-----|:------------|
| `GET` | `/api/v1/i18n/locales` | List available locales |
| `GET` | `/api/v1/i18n/locales` | List available locales (`isDefault` marks the default one) and name the source locale (`sourceLocale`: the declared `i18n.sourceLocale`, else the default locale) |
| `GET` | `/api/v1/i18n/translations/:locale` | Get all translations for a locale |
| `GET` | `/api/v1/i18n/labels/:object/:locale` | Get translated field labels for an object |

Expand Down
1 change: 1 addition & 0 deletions content/docs/references/api/protocol.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@ This schema accepts one of the following structures:
| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **locales** | `{ code: string; label: string; isDefault?: boolean }[]` | ✅ | Available locales |
| **sourceLocale** | `string` | optional | The language the app's own text (a plain-string `label`) is written in: the declared `i18n.sourceLocale`, else the default locale. Absent when the server knows neither. Text typed in any other locale is a translation |

### Nested Shape: `GetLocalesResponse.locales[number]`

Expand Down
3 changes: 2 additions & 1 deletion packages/runtime/src/domains/i18n-anonymous-deny.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ const FACES: readonly Face[] = [
{
face: 'the locale list',
subPath: '/locales',
served: { locales: [{ code: 'en', label: 'en', isDefault: true }, { code: 'fr', label: 'fr', isDefault: false }] },
// The source locale is the default one here: the provider declares no `sourceLocale` (#36504).
served: { locales: [{ code: 'en', label: 'en', isDefault: true }, { code: 'fr', label: 'fr', isDefault: false }], sourceLocale: 'en' },
reads: 'getLocales',
},
{
Expand Down
18 changes: 12 additions & 6 deletions packages/runtime/src/domains/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* dispatcher-owned (moving it into one provider would 404 the other).
*
* Routes (path is the sub-path after `/i18n`):
* GET /locales → getLocales
* GET /locales → getLocales (+ the source locale)
* GET /translations/:locale → getTranslations (locale from path)
* GET /translations?locale=xx → getTranslations (locale from query)
* GET /labels/:object/:locale → getFieldLabels (both from path)
Expand Down Expand Up @@ -237,11 +237,17 @@ export async function handleI18nRequest(
// array through made one endpoint answer in two shapes depending on
// which provider mounted it, the dispatcher's contradicting the SDK's
// own `GetLocalesResponse` type.
const locales = toLocaleDescriptors(
i18nService.getLocales(),
typeof i18nService.getDefaultLocale === 'function' ? i18nService.getDefaultLocale() : undefined,
);
return { handled: true, response: deps.success({ locales }) };
const defaultLocale = typeof i18nService.getDefaultLocale === 'function' ? i18nService.getDefaultLocale() : undefined;
const locales = toLocaleDescriptors(i18nService.getLocales(), defaultLocale);
// [#36504] The language the app's own text is authored in: the
// declared `i18n.sourceLocale`, else the default locale — the
// collapse every metadata-document translator makes, and the CALLER's
// to make (`II18nService.getSourceLocale` reports only what was
// declared). A client that edits labels reads it to tell an edit of
// the source text from a translation. Absent when neither is known.
const declaredSource = typeof i18nService.getSourceLocale === 'function' ? i18nService.getSourceLocale() : undefined;
const sourceLocale = declaredSource || defaultLocale || undefined;
return { handled: true, response: deps.success({ locales, ...(sourceLocale ? { sourceLocale } : {}) }) };
}

// GET /i18n/translations/:locale OR /i18n/translations?locale=xx
Expand Down
29 changes: 29 additions & 0 deletions packages/runtime/src/i18n-supported-locales.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/

import { describe, it, expect, vi } from 'vitest';
// Paid at collection, not inside a clocked case: `AppPlugin` loads it lazily (check:test-source-alias).
import '@objectstack/metadata';
import { AppPlugin } from './app-plugin.js';
import { HttpDispatcher } from './http-dispatcher.js';
import { createMemoryI18n } from '@objectstack/core';
Expand Down Expand Up @@ -213,3 +215,30 @@ describe('GET /i18n/locales reports the app\'s declared supportedLocales (#7679)
]);
});
});

/**
* [#36504] `GET /i18n/locales` names the language the app's own text is
* written in, so a client that edits labels (the console's design mode) can
* tell an edit of the source text from a translation. The declared
* `i18n.sourceLocale` wins; an app declaring none is authored in its default
* locale, the collapse every metadata-document translator makes.
*/
describe('GET /i18n/locales names the source locale (#36504)', () => {
async function body(i18nConfig: Record<string, unknown> | undefined) {
const { dispatcher } = await bootStack(i18nConfig);
const result = await dispatcher.handleI18n('/locales', 'GET', {}, SIGNED_IN);
const parsed = GetLocalesResponseSchema.safeParse(result.response?.body?.data);
expect(parsed.success, JSON.stringify(parsed.error?.issues)).toBe(true);
return parsed.data!;
}

it('an app authored in English that defaults its users to zh-CN: the source is en, the default zh-CN', async () => {
const read = await body({ defaultLocale: 'zh-CN', sourceLocale: 'en', supportedLocales: ['zh-CN', 'en'] });
expect(read.sourceLocale).toBe('en');
expect(read.locales.find((l) => l.isDefault)?.code).toBe('zh-CN');
});

it('an app declaring no sourceLocale is authored in its default locale', async () => {
expect((await body({ defaultLocale: 'zh-CN', supportedLocales: ['zh-CN', 'en'] })).sourceLocale).toBe('zh-CN');
});
});
1 change: 1 addition & 0 deletions packages/spec/authorable-surface/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@
"api/GetInstalledPackageResponse:meta",
"api/GetInstalledPackageResponse:success",
"api/GetLocalesResponse:locales",
"api/GetLocalesResponse:sourceLocale",
"api/GetMetaDiagnosticsResponse:entries",
"api/GetMetaDiagnosticsResponse:scannedItems",
"api/GetMetaDiagnosticsResponse:scannedTypes",
Expand Down
6 changes: 6 additions & 0 deletions packages/spec/src/api/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ describe('ObjectStack Protocol', () => {
{ code: 'es-ES', label: 'Spanish (Spain)' },
],
}).success).toBe(true);
// [#36504] The source locale is declared, so the parse keeps it (an
// undeclared key would be stripped), and it is optional.
expect(GetLocalesResponseSchema.parse({
locales: [{ code: 'zh-CN', label: 'zh-CN', isDefault: true }],
sourceLocale: 'en',
}).sourceLocale).toBe('en');
expect(GetTranslationsRequestSchema.safeParse({ locale: 'en-US' }).success).toBe(true);
// The request is locale-only. `namespace`/`keys` were declared here but read
// by no serving surface, so they were trimmed (#3676). Asserting on the
Expand Down
15 changes: 15 additions & 0 deletions packages/spec/src/api/protocol.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4806,6 +4806,17 @@ export const GetLocalesRequestSchema = lazySchema(() => z.object({}));
* the requester's `Accept-Language`) is a caller's choice the server cannot
* make for it. Serving real names would put CLDR data behind an endpoint for
* something every client can already compute, and no consumer asks for it.
*
* `sourceLocale` (#36504) names the language the app's inline metadata text
* is authored in — the declared `i18n.sourceLocale`, else the default locale,
* the same collapse the document translators make (`ResolveOptions.defaultLocale`
* fed from `getSourceLocale?.() ?? getDefaultLocale?.()`). It differs from the
* `isDefault` locale on a deployment authored in one language that defaults
* its users to another. A client that edits labels reads it to tell an edit
* of the source text from a translation: the console's design mode stores a
* menu rename made in any other language as that language's translation.
* Named after its config key, as `defaultLocale`'s `isDefault` mark is the
* config's `defaultLocale`.
*/
export const GetLocalesResponseSchema = lazySchema(() => z.object({
locales: z.array(z.object({
Expand All @@ -4815,6 +4826,10 @@ export const GetLocalesResponseSchema = lazySchema(() => z.object({
),
isDefault: z.boolean().default(false).describe('Whether this is the default locale'),
})).describe('Available locales'),
sourceLocale: z.string().optional().describe(
'The language the app\'s own text (a plain-string `label`) is written in: the declared `i18n.sourceLocale`, '
+ 'else the default locale. Absent when the server knows neither. Text typed in any other locale is a translation',
),
}));

/**
Expand Down
Loading
Loading