Skip to content
Open
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
2 changes: 1 addition & 1 deletion apps/web/lingui.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { formatter } from '@lingui/format-po';

export default defineConfig({
sourceLocale: 'en',
locales: ['ar', 'de', 'en', 'es', 'fr', 'it', 'uk', 'vi'],
locales: ['ar', 'de', 'en', 'es', 'fr', 'it', 'uk', 'vi', 'zh'],
// PO for translators (D96, Weblate). No line numbers, so catalogues don't churn on every edit.
format: formatter({ lineNumbers: false }),
catalogs: [
Expand Down
13 changes: 9 additions & 4 deletions apps/web/src/components/condition/pdf-sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { useState } from 'react';
import { isApiError } from '@/api/client';
import { conditionApi } from '@/api/condition/queries';
import type { ConditionPdfBody, ConditionReport } from '@/api/condition/types';
import { LanguageSelect } from '@/components/languages';
import { LanguageSelect, SERVER_LANGUAGES } from '@/components/languages';
import { Notice, useErrorText } from '@/components/page';
import { ReportProgress, reportFilenameOf } from '@/components/reports/progress';
import { Sheet } from '@/components/things/sheet';
import { Button } from '@/components/ui/button';
import { DialogFooter } from '@/components/ui/dialog';
import { Switch } from '@/components/ui/switch';
import { useOnline } from '@/lib/online';
import { type Locale, usePrefs } from '@/lib/prefs';
import { type ServerLocale, serverLocale, usePrefs } from '@/lib/prefs';

const filename = reportFilenameOf('condition-report');

Expand Down Expand Up @@ -56,7 +56,7 @@ function PdfForm({
const online = useOnline();
const errorText = useErrorText();
const [photos, setPhotos] = useState(true);
const [locale, setLocale] = useState<Locale>(prefs.locale);
const [locale, setLocale] = useState<ServerLocale>(serverLocale(prefs.locale));
const [runId, setRunId] = useState<string | null>(null);
const body = (): ConditionPdfBody => ({
reportId: report.id,
Expand Down Expand Up @@ -124,7 +124,12 @@ function PdfForm({
<Switch isSelected={photos} onChange={setPhotos}>
<Trans>Photos</Trans>
</Switch>
<LanguageSelect label={t`Language`} value={locale} onChange={setLocale} />
<LanguageSelect
label={t`Language`}
value={locale}
onChange={setLocale}
languages={SERVER_LANGUAGES}
/>
{start.error ? <Notice tone="danger">{startError(start.error)}</Notice> : null}
{online ? null : (
<p className="m-0 text-small text-ink-2">
Expand Down
9 changes: 5 additions & 4 deletions apps/web/src/components/export/export-sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useState } from 'react';
import { portabilityApi, portabilityKeys } from '@/api/portability/queries';
import type { ExportOptions, ExportRun } from '@/api/portability/types';
import type { LocationSummary } from '@/api/types';
import { LanguageSelect } from '@/components/languages';
import { LanguageSelect, SERVER_LANGUAGES } from '@/components/languages';
import { Notice } from '@/components/page';
import { Button } from '@/components/ui/button';
import { Combobox } from '@/components/ui/combobox';
Expand All @@ -29,7 +29,7 @@ import { servedOverHttp } from '@/lib/https';
import { useLocationName } from '@/lib/labels';
import { useOnline } from '@/lib/online';
import { useMyDataLocation, usePersonalName } from '@/lib/personal-name';
import { type Digits, type Locale, usePrefs } from '@/lib/prefs';
import { type Digits, type Locale, serverLocale, usePrefs } from '@/lib/prefs';
import { useExportErrorText } from './labels';
import { type Passphrase, PassphraseFields, passphraseReady } from './passphrase-field';

Expand Down Expand Up @@ -93,7 +93,7 @@ export function ExportSheet({
aiCalls: true,
readable: true,
pdf: true,
locale: prefs.locale,
locale: serverLocale(prefs.locale),
digits: prefs.digits,
...startOptions,
});
Expand Down Expand Up @@ -203,8 +203,9 @@ export function ExportSheet({
<div className="grid gap-3">
<LanguageSelect
label={t`The readable copy's language`}
value={(options.locale as Locale) ?? 'en'}
value={serverLocale((options.locale ?? 'en') as Locale)}
onChange={(locale) => set({ locale })}
languages={SERVER_LANGUAGES}
/>
{options.locale === 'ar' ? (
<Segmented<Digits>
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/components/languages.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('LanguageSelect', () => {
'Italiano',
'Українська',
'Tiếng Việt',
'简体中文',
]);
const arabic = screen.getByRole('option', { name: 'العربية' });
expect(arabic.querySelector('[lang="ar"]')).toHaveAttribute('dir', 'rtl');
Expand Down
58 changes: 39 additions & 19 deletions apps/web/src/components/languages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { findLanguages, languageSearch } from '@kept/shared';
import { useLingui } from '@lingui/react/macro';
import CN from 'country-flag-icons/react/3x2/CN';
import DE from 'country-flag-icons/react/3x2/DE';
import EG from 'country-flag-icons/react/3x2/EG';
import ES from 'country-flag-icons/react/3x2/ES';
Expand All @@ -18,14 +19,21 @@ import UA from 'country-flag-icons/react/3x2/UA';
import VN from 'country-flag-icons/react/3x2/VN';
import { useMemo, useState } from 'react';
import type { Key } from 'react-aria-components';
import { directionOf, LOCALES, type Locale, localeOfTag } from '@/lib/prefs';
import {
directionOf,
LOCALES,
type Locale,
localeOfTag,
type ServerLocale,
serverLocale,
} from '@/lib/prefs';
import { cn } from '@/lib/utils';
import { Combobox, ComboboxItem } from './ui/combobox';

type Flag = typeof GB;

/** `label` is the list's text value, for screen readers and the box: the name in itself. */
export type Language = { id: Locale; name: string; label: string };
export type Language<T extends Locale = Locale> = { id: T; name: string; label: string };

/** Each language's name in itself, so a reader finds theirs whatever the interface says. */
export const LANGUAGE_NAMES: Readonly<Record<Locale, string>> = {
Expand All @@ -37,6 +45,7 @@ export const LANGUAGE_NAMES: Readonly<Record<Locale, string>> = {
it: 'Italiano',
uk: 'Українська',
vi: 'Tiếng Việt',
zh: '简体中文',
};

const FLAGS: Readonly<Record<Locale, Flag>> = {
Expand All @@ -48,6 +57,7 @@ const FLAGS: Readonly<Record<Locale, Flag>> = {
it: IT,
uk: UA,
vi: VN,
zh: CN,
};

export const LANGUAGES: readonly Language[] = LOCALES.map((id) => ({
Expand All @@ -56,6 +66,11 @@ export const LANGUAGES: readonly Language[] = LOCALES.map((id) => ({
label: LANGUAGE_NAMES[id],
}));

/** The languages the server writes mail, PDFs and readable copies in; see `serverLocale`. */
export const SERVER_LANGUAGES: readonly Language<ServerLocale>[] = LANGUAGES.filter(
(l): l is Language<ServerLocale> => serverLocale(l.id) === l.id,
);

/** A 3:2 flag, decorative: the language's name always sits beside it. */
export function LanguageFlag({ locale, className }: { locale: Locale; className?: string }) {
const Flag = FLAGS[locale];
Expand All @@ -81,57 +96,62 @@ export function LanguageName({ locale }: { locale: Locale }) {
}

/** A Combobox filter for the languages (#450): the matcher the docs site's menu uses too. */
function useLanguageFilter() {
function useLanguageFilter(languages: readonly Language[]) {
const { i18n } = useLingui();
return useMemo(() => {
const search = languageSearch(LANGUAGES, i18n.locale);
const search = languageSearch(languages, i18n.locale);
// React Aria hands the filter an item's text value, and own names are unique.
return (ownName: string, query: string) =>
findLanguages(search, query).some((language) => language.name === ownName);
}, [i18n.locale]);
}, [languages, i18n.locale]);
}

function LanguageItem({ item }: { item: Language }) {
return <ComboboxItem item={item} content={<LanguageName locale={item.id} />} />;
}

/**
* The interface language: one choice (Settings → Me, the sign-in pages). Typing only filters the
* list: the language changes when one is chosen, and a box left empty or with anything else in it
* goes back to the current language's name. The box's direction stays the page's, so its text
* doesn't jump sides when typing starts; the name's letters still run the right way.
* A language choice. The interface language (Settings → Me, the sign-in pages) offers every
* catalogue language; the report and export pickers pass `SERVER_LANGUAGES`, the languages the
* server writes mail, PDFs and readable copies in. Typing only filters the list: the language
* changes when one is chosen, and a box left empty or with anything else in it goes back to the
* current language's name. The box's direction stays the page's, so its text doesn't jump sides
* when typing starts; the name's letters still run the right way.
*/
export function LanguageSelect({
export function LanguageSelect<T extends Locale = Locale>({
label,
value,
onChange,
languages = LANGUAGES as readonly Language<T>[],
compact = false,
className,
}: {
label: string;
value: Locale;
value: T;
/** A promise says the change is still happening; it mustn't reject (say what went wrong here). */
onChange: (locale: Locale) => unknown;
onChange: (locale: T) => unknown;
/** The choices; defaults to every catalogue language. */
languages?: readonly Language<T>[];
/** No visible label and a small box (the sign-in header); the label stays accessible. */
compact?: boolean;
className?: string;
}) {
const filter = useLanguageFilter();
const filter = useLanguageFilter(languages as readonly Language[]);
// The list closes when the selection changes, and a language can take a moment to load, or fail
// to. Until then the box holds the choice; if the value doesn't follow, it goes back to the value.
const [choosing, setChoosing] = useState<Locale | null>(null);
const [choosing, setChoosing] = useState<T | null>(null);
const shown = choosing ?? value;
return (
<Combobox<Language>
<Combobox<Language<T>>
{...(compact ? { 'aria-label': label } : { label })}
items={LANGUAGES}
items={languages}
defaultFilter={filter}
selectedKey={shown}
onSelectionChange={(key) => {
// A cleared box reports no selection: that isn't a choice.
if (key == null || key === value) return;
setChoosing(key as Locale);
void Promise.resolve(onChange(key as Locale)).finally(() => setChoosing(null));
setChoosing(key as T);
void Promise.resolve(onChange(key as T)).finally(() => setChoosing(null));
}}
showsChoice
leading={<LanguageFlag locale={shown} />}
Expand Down Expand Up @@ -163,7 +183,7 @@ export function LanguagesMultiSelect({
isDisabled?: boolean;
}) {
const { t } = useLingui();
const filter = useLanguageFilter();
const filter = useLanguageFilter(LANGUAGES);
const chosen = new Set(value.map(localeOfTag).filter((l): l is Locale => l !== null));
const change = (keys: Key[]) => {
const next = new Set(keys as Locale[]);
Expand Down
13 changes: 9 additions & 4 deletions apps/web/src/components/moving/contents-sheets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { useState } from 'react';
import { isApiError } from '@/api/client';
import { movingApi } from '@/api/moving/queries';
import type { ContentsSheetsBody, Move } from '@/api/moving/types';
import { LanguageSelect } from '@/components/languages';
import { LanguageSelect, SERVER_LANGUAGES } from '@/components/languages';
import { Notice, useErrorText } from '@/components/page';
import { ReportProgress, reportFilenameOf } from '@/components/reports/progress';
import { Button } from '@/components/ui/button';
import { DialogFooter } from '@/components/ui/dialog';
import { useOnline } from '@/lib/online';
import { type Locale, usePrefs } from '@/lib/prefs';
import { type ServerLocale, serverLocale, usePrefs } from '@/lib/prefs';
import { SheetsPrinted } from './sheets-printed';

const filename = reportFilenameOf('contents-sheets');
Expand All @@ -34,7 +34,7 @@ export default function SheetsForm({ onClose, move, containerIds }: SheetsFormPr
const prefs = usePrefs();
const online = useOnline();
const errorText = useErrorText();
const [locale, setLocale] = useState<Locale>(prefs.locale);
const [locale, setLocale] = useState<ServerLocale>(serverLocale(prefs.locale));
const [runId, setRunId] = useState<string | null>(null);
const body = (): ContentsSheetsBody => ({
moveId: move.id,
Expand Down Expand Up @@ -94,7 +94,12 @@ export default function SheetsForm({ onClose, move, containerIds }: SheetsFormPr
<Trans>Add a box first.</Trans>
</Notice>
) : null}
<LanguageSelect label={t`Language`} value={locale} onChange={setLocale} />
<LanguageSelect
label={t`Language`}
value={locale}
onChange={setLocale}
languages={SERVER_LANGUAGES}
/>
{start.error ? <Notice tone="danger">{startError(start.error)}</Notice> : null}
{online ? null : (
<p className="m-0 text-small text-ink-2">
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/reports/claim-pack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { canShareFiles, downloadsWork, shareFiles } from '@/lib/files';
import { sep, useBytes, useFormat } from '@/lib/format';
import { useLocationName } from '@/lib/labels';
import { useOnline } from '@/lib/online';
import { usePrefs } from '@/lib/prefs';
import { serverLocale, usePrefs } from '@/lib/prefs';
import { downloadsAreOff } from '@/try/files';
import { TRY } from '@/try/flag';
import type { ReportSearch } from './insurance-report';
Expand Down Expand Up @@ -155,7 +155,7 @@ function StartPack({ scope, what }: { scope: CreateClaimPackBody['scope']; what:
try {
const created = await householdApi.createClaimPack({
scope,
locale: prefs.locale,
locale: serverLocale(prefs.locale),
digits: prefs.digits,
acknowledged: true,
});
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/reports/insurance-report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { toast } from '@/components/ui/toast';
import { canShareFiles, downloadsWork, saveFile, shareFiles } from '@/lib/files';
import { useLocationName } from '@/lib/labels';
import { useOnline } from '@/lib/online';
import { usePrefs } from '@/lib/prefs';
import { serverLocale, usePrefs } from '@/lib/prefs';
import { ReportProgress, reportFilenameOf } from './progress';

export type ReportSearch = { loc?: string; incident?: string; things?: string; run?: string };
Expand Down Expand Up @@ -205,7 +205,7 @@ function InsuranceForm({
asOf: day,
...(currency ? { reportCurrency: currency } : {}),
include: { photos },
locale: prefs.locale,
locale: serverLocale(prefs.locale),
digits: prefs.digits,
});
setRunId(run.id);
Expand Down
13 changes: 9 additions & 4 deletions apps/web/src/components/vehicles/history-report-sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { isApiError } from '@/api/client';
import { vehiclesApi } from '@/api/vehicles/queries';
import type { VehicleHistoryReportBody } from '@/api/vehicles/types';
import { DocumentIcon } from '@/components/icons';
import { LanguageSelect } from '@/components/languages';
import { LanguageSelect, SERVER_LANGUAGES } from '@/components/languages';
import { Notice, useErrorText } from '@/components/page';
import { ReportProgress, reportFilenameOf } from '@/components/reports/progress';
import { accessOf } from '@/components/schedules/access';
Expand All @@ -30,7 +30,7 @@ import { DialogFooter } from '@/components/ui/dialog';
import { Segmented } from '@/components/ui/segmented';
import { Switch } from '@/components/ui/switch';
import { useOnline } from '@/lib/online';
import { type Locale, usePrefs } from '@/lib/prefs';
import { type ServerLocale, serverLocale, usePrefs } from '@/lib/prefs';

/** The shared PDF's name (inferred: the server's `filenameOf` names only the inventory and
* insurance kinds so far; T15 decides the vehicle one). */
Expand Down Expand Up @@ -82,7 +82,7 @@ function ReportForm({ onClose }: { onClose: () => void }) {
const [photos, setPhotos] = useState(true);
const [fuel, setFuel] = useState(fuelOn);
const [documents, setDocuments] = useState(true);
const [locale, setLocale] = useState<Locale>(prefs.locale);
const [locale, setLocale] = useState<ServerLocale>(serverLocale(prefs.locale));
const [error, setError] = useState<string | null>(null);
const [runId, setRunId] = useState<string | null>(null);

Expand Down Expand Up @@ -211,7 +211,12 @@ function ReportForm({ onClose }: { onClose: () => void }) {
)}
</p>
</fieldset>
<LanguageSelect label={t`Language`} value={locale} onChange={setLocale} />
<LanguageSelect
label={t`Language`}
value={locale}
onChange={setLocale}
languages={SERVER_LANGUAGES}
/>
{start.error ? <Notice tone="danger">{startError(start.error)}</Notice> : null}
{online ? null : (
<p className="m-0 text-small text-ink-2">
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const loaders: Record<Locale, () => Promise<{ messages: Messages }>> = {
it: () => import('../locales/it/messages.po'),
uk: () => import('../locales/uk/messages.po'),
vi: () => import('../locales/vi/messages.po'),
zh: () => import('../locales/zh/messages.po'),
};

/**
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/lib/prefs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ afterEach(() => {

describe('languages', () => {
it('has the languages, and knows a tag by its language', () => {
expect(LOCALES).toEqual(['ar', 'de', 'en', 'es', 'fr', 'it', 'uk', 'vi']);
expect(LOCALES).toEqual(['ar', 'de', 'en', 'es', 'fr', 'it', 'uk', 'vi', 'zh']);
expect(isLocale('it')).toBe(true);
expect(isLocale('vi')).toBe(true);
expect(isLocale('uk')).toBe(true);
expect(isLocale('es')).toBe(true);
expect(isLocale('zh')).toBe(true);
expect(isLocale('pt')).toBe(false);
expect(localeOfTag('ar-EG')).toBe('ar');
expect(localeOfTag('fr_CA')).toBe('fr');
Expand Down
Loading
Loading