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
16 changes: 16 additions & 0 deletions .changeset/35425-one-language-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@object-ui/i18n': patch
'@object-ui/app-shell': patch
'@object-ui/console': patch
---

feat(i18n): the sign-in page and the in-app language menu offer one list, every interface language

Clause-②: yes (widening) — @object-ui/i18n adds `appContentLanguages` to the i18n context and the locale key `console.localeSwitcher.appContentDefault` in all eleven packs; `offerableLanguages` is never `null` any more; nothing removed
Metadata-protocol: none

The in-app language menu used to list only the languages the app declares (four on HotCRM), while the sign-in page, which has no session to ask the app with, listed all eleven interface languages. A language picked on the sign-in page (German, Korean) was then missing from the menu after sign-in.

Both now list all eleven, as does the profile page's language card. A language the app's own content is not translated into stays selectable: the interface shows in it, and object, field and view names show in the deployment's default language. The menu row of such a language carries one muted line, "App content shows in the default language", and the profile card shows the same line under the picker when one is chosen. The sign-in page marks nothing, because nothing has been read about the app there.

`offerableLanguages` on `useObjectTranslation()` is now every platform locale table language whatever the app ships. The app's list (`loadLocales`) feeds the new `appContentLanguages`: the table languages it covers, or `null` when not known (in flight, signed out, failed, or naming no table language). `GET /api/v1/i18n/locales` is unchanged and no language pack is added. The organization's default-language setting keeps the app's declared list.
29 changes: 24 additions & 5 deletions ui/apps/console/src/pages/system/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,11 @@ interface LanguageCardProps {
* ## What is offered, and what happens when the write route is not there
*
* The option list is `offerableLanguages` — the i18n provider's own answer
* (the platform locale table ∩ the deployment's published locales, as table
* tags), which is what `LocaleSwitcher` renders too. ⛔ No second list is introduced here.
* (every platform locale table language, as table tags, objectstack#35425),
* which is what `LocaleSwitcher` renders too. ⛔ No second list is introduced here.
* A chosen language the deployment's own content is not translated into
* (outside the provider's `appContentLanguages`, when that is known) carries
* the same one-line hint the globe menu shows on that row.
* A stored tag that is not in that set is prepended so the control shows the
* truth rather than rendering blank.
*
Expand All @@ -598,7 +601,7 @@ interface LanguageCardProps {
* card's own "hidden ... rather than rendering a control that answers 403".
*/
function LanguageCard({ userId }: LanguageCardProps) {
const { t, offerableLanguages } = useObjectTranslation();
const { t, offerableLanguages, appContentLanguages } = useObjectTranslation();
const adapter = useAdapter();
const perms = usePermissions();

Expand Down Expand Up @@ -645,15 +648,24 @@ function LanguageCard({ userId }: LanguageCardProps) {
}, [adapter, userId]);

const codes = useMemo(() => {
const offered = offerableLanguages ?? [];
const offered = offerableLanguages;
// A stored tag the deployment no longer publishes is still this account's
// truth; showing it beats a select whose value matches no option.
return stored && stored.length > 0 && !offered.includes(stored)
? [stored, ...offered]
: [...offered];
}, [offerableLanguages, stored]);

if (!adapter || !rowRead || offerableLanguages === null) return null;
if (!adapter || !rowRead) return null;

// The globe menu's hint, for the language chosen here: only when the app's
// list is known and leaves that language out. "Use the deployment default"
// is never marked — it IS the default language — and neither is a stored
// tag outside the table (`th`), which the app list is never read for.
const choiceInDefault =
offerableLanguages.includes(choice) &&
appContentLanguages !== null &&
!appContentLanguages.includes(choice);

const writable = resolveFieldAffordance('editFormFields', perms, 'sys_user', 'locale');
const dirty = choice !== (stored ?? USE_DEPLOYMENT_DEFAULT);
Expand Down Expand Up @@ -753,6 +765,13 @@ function LanguageCard({ userId }: LanguageCardProps) {
invalid={!!fieldError}
describedBy={fieldError ? 'profile-language-error' : undefined}
/>
{choiceInDefault && (
<p className="text-xs text-muted-foreground" data-testid="profile-language-app-content-default">
{t('console.localeSwitcher.appContentDefault', {
defaultValue: 'App content shows in the default language',
})}
</p>
)}
{fieldError && (
<p id="profile-language-error" className="text-sm text-destructive">
{fieldError}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ vi.mock('@object-ui/fields', async (importOriginal) => ({

vi.mock('@object-ui/i18n', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useObjectTranslation: () => ({ t: translate, offerableLanguages: ['en', 'zh'] }),
useObjectTranslation: () => ({ t: translate, offerableLanguages: ['en', 'zh'], appContentLanguages: null }),
}));

vi.mock('@object-ui/react', async (importOriginal) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ vi.mock('@object-ui/fields', async (importOriginal) => ({

vi.mock('@object-ui/i18n', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useObjectTranslation: () => ({ t: translate, language: 'en', offerableLanguages: ['en'] }),
useObjectTranslation: () => ({ t: translate, language: 'en', offerableLanguages: ['en'], appContentLanguages: null }),
}));

vi.mock('@object-ui/react', async (importOriginal) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ import { describe, expect, it, vi, beforeEach } from 'vitest';
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { normaliseClientError } from '@object-ui/data-objectstack';

const { user, updateUser, findOne, update, offerable, writable } = vi.hoisted(() => ({
const { user, updateUser, findOne, update, offerable, appContent, writable } = vi.hoisted(() => ({
user: { value: { id: 'usr_1', name: 'Ada', email: 'ada@example.com' } as Record<string, unknown> | null },
updateUser: vi.fn(async () => {}),
findOne: vi.fn<(resource: string, id: string) => Promise<unknown>>(async () => ({ id: 'usr_1' })),
update: vi.fn<(resource: string, id: string, data: unknown) => Promise<unknown>>(async () => ({})),
offerable: { value: ['en', 'zh', 'ja'] as readonly string[] | null },
offerable: { value: ['en', 'zh', 'ja'] as readonly string[] },
appContent: { value: null as readonly string[] | null },
writable: { value: true },
}));

Expand Down Expand Up @@ -98,6 +99,7 @@ vi.mock('@object-ui/i18n', async (importOriginal) => ({
String(options?.[name] ?? ''),
),
offerableLanguages: offerable.value,
appContentLanguages: appContent.value,
}),
}));

Expand Down Expand Up @@ -149,6 +151,7 @@ describe('ProfilePage — my language (objectui#7501)', () => {
vi.clearAllMocks();
user.value = { id: 'usr_1', name: 'Ada', email: 'ada@example.com' };
offerable.value = ['en', 'zh', 'ja'];
appContent.value = null;
writable.value = true;
findOne.mockResolvedValue({ id: 'usr_1' });
update.mockResolvedValue({});
Expand Down Expand Up @@ -276,6 +279,7 @@ describe('ProfilePage — a stored spelling of a table language (objectstack#305
beforeEach(() => {
vi.clearAllMocks();
user.value = { id: 'usr_1', name: 'Ada', email: 'ada@example.com' };
appContent.value = null;
writable.value = true;
update.mockResolvedValue({});
});
Expand Down Expand Up @@ -304,4 +308,39 @@ describe('ProfilePage — a stored spelling of a table language (objectstack#305
expect(await listed(select)).toHaveLength(4);
expect(select).toHaveTextContent('ไทย');
});

it('says the app content shows in the default language for a chosen language the app does not ship (objectstack#35425)', async () => {
offerable.value = ['en', 'zh-Hans', 'ja'];
appContent.value = ['en', 'zh-Hans'];
findOne.mockResolvedValue({ id: 'usr_1', locale: 'en' });
const select = await renderProfile();

// A shipped language, and the deployment default: no hint.
expect(screen.queryByTestId('profile-language-app-content-default')).not.toBeInTheDocument();
await pick(select, 'Use the deployment default');
expect(screen.queryByTestId('profile-language-app-content-default')).not.toBeInTheDocument();

// 日本語 is offered (every table language is) but the app ships no `ja`.
await pick(select, '日本語');
expect(screen.getByTestId('profile-language-app-content-default')).toHaveTextContent(
'App content shows in the default language',
);
});

it('marks nothing while the app list is not known', async () => {
offerable.value = ['en', 'zh-Hans', 'ja'];
findOne.mockResolvedValue({ id: 'usr_1', locale: 'ja' });
await renderProfile();

expect(screen.queryByTestId('profile-language-app-content-default')).not.toBeInTheDocument();
});

it('does not mark a stored tag outside the table', async () => {
appContent.value = ['en'];
offerable.value = ['en', 'zh-Hans'];
findOne.mockResolvedValue({ id: 'usr_1', locale: 'th' });
await renderProfile();

expect(screen.queryByTestId('profile-language-app-content-default')).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ vi.mock('@object-ui/fields', async (importOriginal) => ({
/** Each call site's key, so the pin names the pack key it reads. */
vi.mock('@object-ui/i18n', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useObjectTranslation: () => ({ t: (key: string) => `«${key}»`, offerableLanguages: ['en'] }),
useObjectTranslation: () => ({ t: (key: string) => `«${key}»`, offerableLanguages: ['en'], appContentLanguages: null }),
}));

vi.mock('@object-ui/react', async (importOriginal) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ vi.mock('@object-ui/i18n', async (importOriginal) => ({
useObjectTranslation: () => ({
t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key),
offerableLanguages: offerable.value,
appContentLanguages: null,
}),
}));

Expand Down
64 changes: 42 additions & 22 deletions ui/packages/app-shell/src/layout/LocaleSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
* Uses the i18n context to change language at runtime.
*
* The menu is not a list of its own (objectui#4039): it renders whatever the
* i18n provider says is offerable — the platform locale table ∩ the app's own
* locale list, as table tags (objectstack#30514). This file owns only the *labels*, because the native
* i18n provider says is offerable — every language of the platform locale
* table, as table tags, on the sign-in page and in the app alike
* (objectstack#35425, which reversed the narrowing to the app's own locale
* list). A language the app's own content is not translated into stays
* selectable and carries one muted line saying that content shows in the
* default language; the provider's `appContentLanguages` says which those are,
* and a page that does not know (signed out) marks none. This file owns the
* *labels* and that hint, because the native
* names are the one piece of that no API response supplies:
* `GET /api/v1/i18n/locales` returns descriptors whose `label` is the code
* echoed back (`toLocaleDescriptors` in @objectstack/spec), so a server label
Expand Down Expand Up @@ -76,19 +82,12 @@ export function localeLabel(code: string): string {
}

export function LocaleSwitcher() {
const { t, language, offerableLanguages } = useObjectTranslation();
const { t, language, offerableLanguages, appContentLanguages } = useObjectTranslation();
// ⛔ Not the i18n context's bare `changeLanguage`: while signed in the switch
// has to reach `sys_user.locale`, or the globe and the profile page go on
// naming two settings (objectui#10059). Signed out it IS the bare switch.
const selectLanguage = useLanguageSelection();

// Still asking the app which locales it ships. Render nothing rather than a
// fallback list that would be replaced a tick later — the same idiom the
// sibling menus in this folder use for data they do not have yet
// (`AppSwitcher`, `WorkspaceSwitcher`), and it keeps the ten from flashing
// past on an app that only ships two.
if (offerableLanguages === null) return null;

// The entries are table tags (`zh-Hans`); the session may be running in a
// stored spelling of one (`zh-CN`). Compare through the same resolution the
// provider builds the entries with, so that session ticks 简体中文.
Expand All @@ -113,18 +112,39 @@ export function LocaleSwitcher() {
<span className="sr-only">{t('console.localeSwitcher.label')}</span>
</Button>
</DropdownMenuTrigger>
{/* 44 px rows below `md`, as the top bar's menus (objectstack#29075). */}
<DropdownMenuContent align="end" className="max-h-72 overflow-y-auto max-md:[&_[role=menuitem]]:min-h-11">
{offerableLanguages.map((code) => (
<DropdownMenuItem
key={code}
onClick={() => pick(code)}
className="gap-2"
>
{localeLabel(code)}
{current === code && <span className="ml-auto text-xs">✓</span>}
</DropdownMenuItem>
))}
{/* 44 px rows below `md`, as the top bar's menus (objectstack#29075). The
width is bounded so a long hint (de, ru) wraps instead of widening the
menu past a phone screen. */}
<DropdownMenuContent
align="end"
className="max-h-72 max-w-[min(20rem,calc(100vw-2rem))] overflow-y-auto max-md:[&_[role=menuitem]]:min-h-11"
>
{offerableLanguages.map((code) => {
// Marked only when the app's list is known and leaves this language
// out; "not known" marks nothing rather than every row.
const appContentInDefault =
appContentLanguages !== null && !appContentLanguages.includes(code);
return (
<DropdownMenuItem
key={code}
onClick={() => pick(code)}
className="gap-2"
>
<span className="flex min-w-0 flex-col">
<span>{localeLabel(code)}</span>
{appContentInDefault && (
<span
className="text-xs text-muted-foreground"
data-testid="locale-switcher-app-content-default"
>
{t('console.localeSwitcher.appContentDefault')}
</span>
)}
</span>
{current === code && <span className="ml-auto text-xs">✓</span>}
</DropdownMenuItem>
);
})}
</DropdownMenuContent>
</DropdownMenu>
);
Expand Down
Loading
Loading