diff --git a/gui/app-next/eslint.config.js b/gui/app-next/eslint.config.js
index d1fc80a10..68d1573aa 100644
--- a/gui/app-next/eslint.config.js
+++ b/gui/app-next/eslint.config.js
@@ -3,6 +3,7 @@ import boundaries from 'eslint-plugin-boundaries';
import solid from 'eslint-plugin-solid/configs/typescript';
import globals from 'globals';
import tseslint from 'typescript-eslint';
+import l10n from './eslint/l10n.js';
// Each entry is allowed only in the files listed in `allowedIn`
const restricted = {
@@ -67,6 +68,7 @@ export default tseslint.config(
plugins: {
...solid.plugins,
boundaries,
+ l10n,
},
settings: {
'import/resolver': { typescript: { alwaysTryTypes: true, project: './tsconfig.json' } },
@@ -96,6 +98,7 @@ export default tseslint.config(
],
},
],
+ 'l10n/message-id': 'error',
'no-throw-literal': 'error',
'no-duplicate-imports': 'error',
'no-restricted-syntax': restrictedSyntax(),
@@ -105,5 +108,9 @@ export default tseslint.config(
],
},
},
- ...restrictedOverrides()
+ ...restrictedOverrides(),
+ // Local rules run in node
+ { files: ['eslint/**/*.js'], languageOptions: { globals: globals.node } },
+ // Tests build their own bundles with made-up ids
+ { files: ['src/**/*.test.{ts,tsx}'], rules: { 'l10n/message-id': 'off' } }
);
diff --git a/gui/app-next/eslint/l10n.js b/gui/app-next/eslint/l10n.js
new file mode 100644
index 000000000..bf133128b
--- /dev/null
+++ b/gui/app-next/eslint/l10n.js
@@ -0,0 +1,184 @@
+// Checks Fluent ids against the en-US translation.ftl: literal ids passed to `t`, `t.attr`,
+// the `id` prop of l10n components, and string values of objects that
+// `satisfies Record<..., MessageId>`. Ids must be full literals, args must match the message.
+import { parse } from '@fluent/syntax';
+import { readFileSync, statSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+
+const FTL_FILE = fileURLToPath(new URL('../../app/public/i18n/en/translation.ftl', import.meta.url));
+const COMPONENTS = new Set(['Localized']);
+
+let cache = { mtime: -1, messages: new Map() };
+
+function collect(node, out) {
+ if (!node || typeof node !== 'object') return;
+ if (Array.isArray(node)) {
+ for (const child of node) collect(child, out);
+ return;
+ }
+ if (node.type === 'VariableReference') out.vars.add(node.id.name);
+ else if (node.type === 'MessageReference') {
+ out.refs.push({ id: node.id.name, attr: node.attribute?.name ?? null });
+ }
+ for (const [key, value] of Object.entries(node)) {
+ if (key !== 'span' && typeof value === 'object') collect(value, out);
+ }
+}
+
+function patternInfo(pattern) {
+ const out = { vars: new Set(), refs: [] };
+ collect(pattern, out);
+ return out;
+}
+
+function loadMessages() {
+ const mtime = statSync(FTL_FILE).mtimeMs;
+ if (mtime === cache.mtime) return cache.messages;
+ const messages = new Map();
+ for (const entry of parse(readFileSync(FTL_FILE, 'utf8'), { withSpans: false }).body) {
+ if (entry.type !== 'Message') continue;
+ messages.set(entry.id.name, {
+ value: entry.value ? patternInfo(entry.value) : null,
+ attrs: new Map(entry.attributes.map((a) => [a.id.name, patternInfo(a.value)])),
+ });
+ }
+ cache = { mtime, messages };
+ return messages;
+}
+
+function requiredVars(messages, id, attr, seen = new Set()) {
+ const key = `${id}.${attr ?? ''}`;
+ const message = messages.get(id);
+ const info = attr === null ? message?.value : message?.attrs.get(attr);
+ if (!info || seen.has(key)) return new Set();
+ seen.add(key);
+ const vars = new Set(info.vars);
+ for (const ref of info.refs) {
+ for (const v of requiredVars(messages, ref.id, ref.attr, seen)) vars.add(v);
+ }
+ return vars;
+}
+
+function literalIds(node, context) {
+ if (node.type === 'Literal' && typeof node.value === 'string') return [node.value];
+ if (node.type === 'TemplateLiteral') {
+ if (node.expressions.length === 0) return [node.quasis[0].value.cooked];
+ context.report({ node, messageId: 'dynamic' });
+ return null;
+ }
+ if (node.type === 'ConditionalExpression') {
+ const a = literalIds(node.consequent, context);
+ const b = literalIds(node.alternate, context);
+ return a && b ? [...a, ...b] : null;
+ }
+ return null;
+}
+
+function propName(prop) {
+ if (prop.computed) return null;
+ if (prop.key.type === 'Identifier') return prop.key.name;
+ if (prop.key.type === 'Literal') return String(prop.key.value);
+ return null;
+}
+
+const messageId = {
+ meta: {
+ type: 'problem',
+ docs: { description: 'Fluent ids exist in en-US and get the args they need' },
+ schema: [],
+ messages: {
+ dynamic: 'Fluent ids must be full literals so they stay greppable.',
+ unknown: 'Unknown Fluent id "{{id}}" (not in en-US translation.ftl).',
+ noValue: 'Fluent message "{{id}}" has no value, only attributes.',
+ unknownAttr: 'Fluent message "{{id}}" has no attribute "{{attr}}".',
+ missingArgs: 'Fluent message "{{id}}" needs args: {{vars}}.',
+ unknownArg: 'Fluent message "{{id}}" does not use arg "{{arg}}".',
+ },
+ },
+ create(context) {
+ const messages = loadMessages();
+
+ function checkArgs(node, id, attr, argsNode) {
+ const vars = requiredVars(messages, id, attr);
+ if (!argsNode) {
+ if (vars.size) context.report({ node, messageId: 'missingArgs', data: { id, vars: [...vars].join(', ') } });
+ return;
+ }
+ if (argsNode.type !== 'ObjectExpression') return;
+ if (argsNode.properties.some((p) => p.type !== 'Property' || propName(p) === null)) return;
+ const given = new Set(argsNode.properties.map(propName));
+ const missing = [...vars].filter((v) => !given.has(v));
+ if (missing.length) {
+ context.report({ node: argsNode, messageId: 'missingArgs', data: { id, vars: missing.join(', ') } });
+ }
+ for (const prop of argsNode.properties) {
+ const arg = propName(prop);
+ if (!vars.has(arg)) context.report({ node: prop, messageId: 'unknownArg', data: { id, arg } });
+ }
+ }
+
+ function check(idNode, { attr, argsNode, withArgs }) {
+ const ids = literalIds(idNode, context);
+ if (!ids) return;
+ for (const id of ids) {
+ const message = messages.get(id);
+ if (!message) {
+ context.report({ node: idNode, messageId: 'unknown', data: { id } });
+ continue;
+ }
+ if (attr === undefined && !message.value) {
+ context.report({ node: idNode, messageId: 'noValue', data: { id } });
+ continue;
+ }
+ if (typeof attr === 'string' && !message.attrs.has(attr)) {
+ context.report({ node: idNode, messageId: 'unknownAttr', data: { id, attr } });
+ continue;
+ }
+ if (withArgs && attr !== null) checkArgs(idNode, id, attr ?? null, argsNode);
+ }
+ }
+
+ return {
+ CallExpression(node) {
+ const { callee } = node;
+ const [first, second, third] = node.arguments;
+ if (!first) return;
+ if (callee.type === 'Identifier' && callee.name === 't') {
+ check(first, { attr: undefined, argsNode: second, withArgs: true });
+ } else if (
+ callee.type === 'MemberExpression' &&
+ callee.object.type === 'Identifier' &&
+ callee.object.name === 't' &&
+ callee.property.type === 'Identifier' &&
+ callee.property.name === 'attr' &&
+ second
+ ) {
+ const attr = second.type === 'Literal' && typeof second.value === 'string' ? second.value : null;
+ check(first, { attr, argsNode: third, withArgs: true });
+ }
+ },
+ JSXOpeningElement(node) {
+ if (node.name.type !== 'JSXIdentifier' || !COMPONENTS.has(node.name.name)) return;
+ const attrs = node.attributes.filter((a) => a.type === 'JSXAttribute');
+ const idAttr = attrs.find((a) => a.name.name === 'id');
+ if (!idAttr?.value) return;
+ const idNode = idAttr.value.type === 'JSXExpressionContainer' ? idAttr.value.expression : idAttr.value;
+ const argsAttr = attrs.find((a) => a.name.name === 'args');
+ // Spread props may carry args, skip the args check then
+ const withArgs = !node.attributes.some((a) => a.type === 'JSXSpreadAttribute');
+ const argsNode = argsAttr?.value?.type === 'JSXExpressionContainer' ? argsAttr.value.expression : undefined;
+ check(idNode, { attr: undefined, argsNode, withArgs });
+ },
+ TSSatisfiesExpression(node) {
+ const type = context.sourceCode.getText(node.typeAnnotation);
+ const object = node.expression.type === 'TSAsExpression' ? node.expression.expression : node.expression;
+ if (!/\bMessageId\b/.test(type) || object.type !== 'ObjectExpression') return;
+ for (const prop of object.properties) {
+ if (prop.type === 'Property') check(prop.value, { attr: undefined, withArgs: false });
+ }
+ },
+ };
+ },
+};
+
+export default { rules: { 'message-id': messageId } };
diff --git a/gui/app-next/package.json b/gui/app-next/package.json
index b4c8dafb7..8e59ce1f8 100644
--- a/gui/app-next/package.json
+++ b/gui/app-next/package.json
@@ -17,7 +17,6 @@
"dependencies": {
"@fluent/bundle": "0.19.1",
"@fluent/langneg": "0.7.0",
- "@fluent/sequence": "0.8.0",
"@sentry/solid": "11.9.0",
"@slimevr/gui-shared": "workspace:*",
"@solidjs/router": "1.0.0",
diff --git a/gui/app-next/src/index.tsx b/gui/app-next/src/index.tsx
index e46d45499..2dd9cbc9a 100644
--- a/gui/app-next/src/index.tsx
+++ b/gui/app-next/src/index.tsx
@@ -1,6 +1,7 @@
import { render } from 'solid-js/web';
import { App } from '@/app/app';
import { createPlatform } from '@/app/platform/detect';
+import { initL10n } from '@/lib/l10n';
import { setPlatform } from '@/lib/platform';
import { loadPrefs } from '@/lib/storage';
import { createSocketClient, serverUrl, setClient } from '@/solarxr/client';
@@ -11,6 +12,7 @@ if (!root) throw new Error('missing #root element');
setPlatform(await createPlatform());
await loadPrefs();
+await initL10n();
setClient(createSocketClient({ url: serverUrl() }));
render(() => , root);
diff --git a/gui/app-next/src/lib/enums.test.ts b/gui/app-next/src/lib/enums.test.ts
new file mode 100644
index 000000000..efcf05d81
--- /dev/null
+++ b/gui/app-next/src/lib/enums.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from 'vitest';
+import { enumOptions, enumValues } from '@/lib/enums';
+
+enum Fruit {
+ APPLE = 0,
+ PEAR = 1,
+ PLUM = 2,
+}
+
+const labels = {
+ [Fruit.APPLE]: 'fruit-APPLE',
+ [Fruit.PEAR]: 'fruit-PEAR',
+};
+
+describe('enumValues', () => {
+ it('returns numeric members in order', () => {
+ expect(enumValues(Fruit)).toEqual([Fruit.APPLE, Fruit.PEAR, Fruit.PLUM]);
+ });
+});
+
+describe('enumOptions', () => {
+ it('skips members without a label', () => {
+ expect(enumOptions(Fruit, labels)).toEqual([
+ { value: Fruit.APPLE, label: 'fruit-APPLE' },
+ { value: Fruit.PEAR, label: 'fruit-PEAR' },
+ ]);
+ });
+
+ it('keeps enum order with only', () => {
+ expect(enumOptions(Fruit, labels, { only: [Fruit.PEAR, Fruit.APPLE] })).toEqual([
+ { value: Fruit.APPLE, label: 'fruit-APPLE' },
+ { value: Fruit.PEAR, label: 'fruit-PEAR' },
+ ]);
+ });
+});
diff --git a/gui/app-next/src/lib/enums.ts b/gui/app-next/src/lib/enums.ts
new file mode 100644
index 000000000..ecff9c93b
--- /dev/null
+++ b/gui/app-next/src/lib/enums.ts
@@ -0,0 +1,29 @@
+import type { MessageId } from '@/lib/l10n';
+
+type NumericEnum = Record;
+type EnumValue = Extract;
+
+export interface EnumOption {
+ value: V;
+ label: MessageId;
+}
+
+// Numeric members in declaration order
+export function enumValues(e: E): EnumValue[] {
+ return Object.values(e).filter((x): x is EnumValue => typeof x === 'number');
+}
+
+// Members without a label are skipped, `only` narrows further and keeps enum order
+export function enumOptions(
+ e: E,
+ labels: Partial, MessageId>>,
+ { only }: { only?: readonly EnumValue[] } = {}
+): EnumOption>[] {
+ const options: EnumOption>[] = [];
+ for (const value of enumValues(e)) {
+ const label = labels[value];
+ if (label === undefined || (only && !only.includes(value))) continue;
+ options.push({ value, label });
+ }
+ return options;
+}
diff --git a/gui/app-next/src/lib/l10n/crowdin.ts b/gui/app-next/src/lib/l10n/crowdin.ts
new file mode 100644
index 000000000..23ac29585
--- /dev/null
+++ b/gui/app-next/src/lib/l10n/crowdin.ts
@@ -0,0 +1,106 @@
+import * as v from 'valibot';
+import { defineSession } from '@/lib/storage';
+
+const CROWDIN_PROJECT = '566b32df1e96d3d76a6ddfd6a17e4cbe';
+const CROWDIN_DOMAIN = 'slimevr';
+const CROWDIN_ORIGIN = `https://${CROWDIN_DOMAIN}.crowdin.com`;
+const ACCOUNTS_ORIGIN = 'https://accounts.crowdin.com';
+const JIPT_CDN = 'https://cdn.crowdin.com/jipt';
+const STARTUP_TIMEOUT_MS = 20_000;
+
+export type InContextMode = 'off' | 'active' | 'failed';
+
+const mode = defineSession(
+ 'crowdin-in-context',
+ v.picklist(['off', 'active', 'failed']),
+ 'off'
+);
+
+// Fixed for the whole page load, every switch reloads
+export const inContextMode: InContextMode = mode();
+let started = false;
+
+export function reloadInContext(next: InContextMode) {
+ mode.set(next);
+ location.reload();
+}
+
+export function setInContext(enabled: boolean) {
+ reloadInContext(enabled ? 'active' : 'off');
+}
+
+interface CrowdinMessage {
+ msg_type?: string;
+ has_access?: boolean;
+ response?: string | { errorCode?: string };
+}
+
+function parseCrowdinMessage(data: unknown): CrowdinMessage | null {
+ try {
+ return typeof data === 'string' ? JSON.parse(data) : (data as CrowdinMessage);
+ } catch {
+ return null;
+ }
+}
+
+function onLogoutMessage(event: MessageEvent) {
+ if (event.origin !== ACCOUNTS_ORIGIN && event.origin !== CROWDIN_ORIGIN) return;
+ const isLogout =
+ event.data === 'logout' ||
+ parseCrowdinMessage(event.data)?.msg_type === 'logged_out';
+ if (isLogout) mode.set('off');
+}
+
+function startupOutcome(data: unknown): 'ready' | 'failed' | null {
+ const msg = parseCrowdinMessage(data);
+ switch (msg?.msg_type) {
+ case 'init_project_data':
+ return 'ready';
+ case 'request_jipt_error':
+ return typeof msg.response === 'string' || msg.response?.errorCode === 'timeout'
+ ? 'failed'
+ : 'ready';
+ case 'init_access_storage':
+ return msg.has_access ? null : 'ready';
+ default:
+ return null;
+ }
+}
+
+function watchStartup() {
+ const fail = () => reloadInContext('failed');
+ const onMessage = (event: MessageEvent) => {
+ if (event.origin !== CROWDIN_ORIGIN) return;
+ const outcome = startupOutcome(event.data);
+ if (outcome === 'failed') fail();
+ else if (outcome === 'ready') {
+ clearTimeout(timeout);
+ window.removeEventListener('message', onMessage);
+ }
+ };
+ const timeout = setTimeout(fail, STARTUP_TIMEOUT_MS);
+ window.addEventListener('message', onMessage);
+ return fail;
+}
+
+export function startCrowdinInContext() {
+ if (started) return;
+ started = true;
+
+ window._jipt = [
+ ['project', CROWDIN_PROJECT],
+ ['domain', CROWDIN_DOMAIN],
+ ['start_type', 'manual'],
+ ['escape', () => reloadInContext('off')],
+ ];
+
+ const fail = watchStartup();
+ window.addEventListener('message', onLogoutMessage);
+
+ const script = document.createElement('script');
+ script.src = `${JIPT_CDN}/jipt.js`;
+ script.async = true;
+ script.onload = () => window.jipt?.start();
+ script.onerror = fail;
+ document.body.appendChild(script);
+}
diff --git a/gui/app-next/src/lib/l10n/index.ts b/gui/app-next/src/lib/l10n/index.ts
new file mode 100644
index 000000000..3463c8d12
--- /dev/null
+++ b/gui/app-next/src/lib/l10n/index.ts
@@ -0,0 +1,22 @@
+export {
+ t,
+ createBundle,
+ setL10nBundles,
+ type MessageArgs,
+ type MessageId,
+} from '@/lib/l10n/translate';
+export {
+ Localized,
+ type LocalizedElems,
+ type LocalizedProps,
+} from '@/lib/l10n/localized';
+export {
+ DEFAULT_LOCALE,
+ availableLocales,
+ initL10n,
+ loadLocale,
+ locale,
+ savedLanguage,
+ setLanguage,
+} from '@/lib/l10n/locale';
+export { inContextMode, setInContext, type InContextMode } from '@/lib/l10n/crowdin';
diff --git a/gui/app-next/src/lib/l10n/l10n.test.ts b/gui/app-next/src/lib/l10n/l10n.test.ts
new file mode 100644
index 000000000..4de599799
--- /dev/null
+++ b/gui/app-next/src/lib/l10n/l10n.test.ts
@@ -0,0 +1,113 @@
+import { describe, expect, it, vi } from 'vitest';
+import * as l10n from '@/lib/l10n';
+import { setPlatform, type TranslationsApi } from '@/lib/platform';
+import { fakePlatform } from '@/lib/platform-fake';
+
+const EN = `
+greeting = Hello
+only-en = English only
+with-arg = Hi { $name }
+field = Field
+ .placeholder = Type here
+ .label = Label
+tray_menu-show = Show
+tray_menu-hide = Hide
+tray_menu-quit = Quit
+`;
+const FR = `
+greeting = Bonjour
+field = Champ
+ .placeholder = Tapez ici
+tray_menu-show = Afficher
+`;
+
+function setup({ override }: { override?: string } = {}) {
+ const translations: TranslationsApi = {
+ set: vi.fn(),
+ override: async () => override ?? false,
+ onCrowdinPopupClosed: () => () => {},
+ };
+ setPlatform(fakePlatform({ translations }));
+ const fetchMock = vi.fn(async (url: string) => {
+ const source = { '/i18n/en/translation.ftl': EN, '/i18n/fr/translation.ftl': FR }[
+ url
+ ];
+ return new Response(source ?? '', { status: source === undefined ? 404 : 200 });
+ });
+ vi.stubGlobal('fetch', fetchMock);
+ return { translations, fetchMock };
+}
+
+describe('t', () => {
+ it('uses the current locale and falls back to en-US per message', async () => {
+ setup();
+ await l10n.loadLocale(['fr-FR']);
+ expect(l10n.locale()).toBe('fr');
+ expect(document.documentElement.lang).toBe('fr');
+ expect(l10n.t('greeting')).toBe('Bonjour');
+ expect(l10n.t('only-en')).toBe('English only');
+ });
+
+ it('returns the id when no bundle has the message', async () => {
+ setup();
+ await l10n.loadLocale(['en']);
+ expect(l10n.t('nope')).toBe('nope');
+ });
+
+ it('formats args', async () => {
+ setup();
+ await l10n.loadLocale(['en']);
+ expect(l10n.t('with-arg', { name: 'Ana' })).toContain('Ana');
+ });
+
+ it('falls back to en-US per attribute', async () => {
+ setup();
+ await l10n.loadLocale(['fr']);
+ expect(l10n.t.attr('field', 'placeholder')).toBe('Tapez ici');
+ expect(l10n.t.attr('field', 'label')).toBe('Label');
+ expect(l10n.t.attr('field', 'missing')).toBe('field.missing');
+ });
+});
+
+describe('loadLocale', () => {
+ it('negotiates unknown locales down to en-US', async () => {
+ setup();
+ await l10n.loadLocale(['xx-YY', 'not a tag']);
+ expect(l10n.locale()).toBe('en');
+ });
+
+ it('uses the platform override file instead of fetching the locale', async () => {
+ const { fetchMock } = setup({ override: 'greeting = Overridden' });
+ await l10n.loadLocale(['fr']);
+ expect(l10n.t('greeting')).toBe('Overridden');
+ expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
+ '/i18n/en/translation.ftl',
+ ]);
+ });
+
+ it('sends tray translations to the platform', async () => {
+ const { translations } = setup();
+ await l10n.loadLocale(['fr']);
+ expect(translations.set).toHaveBeenLastCalledWith({
+ 'tray_menu-show': 'Afficher',
+ 'tray_menu-hide': 'Hide',
+ 'tray_menu-quit': 'Quit',
+ });
+ });
+
+ it('applies overlapping loads in call order', async () => {
+ setup();
+ const first = l10n.loadLocale(['fr']);
+ const second = l10n.loadLocale(['en']);
+ await Promise.all([first, second]);
+ expect(l10n.locale()).toBe('en');
+ expect(l10n.t('greeting')).toBe('Hello');
+ });
+
+ it('setLanguage saves the choice', async () => {
+ setup();
+ await l10n.setLanguage('fr');
+ expect(l10n.savedLanguage()).toBe('fr');
+ expect(l10n.locale()).toBe('fr');
+ });
+});
diff --git a/gui/app-next/src/lib/l10n/locale.ts b/gui/app-next/src/lib/l10n/locale.ts
new file mode 100644
index 000000000..5f010a8b9
--- /dev/null
+++ b/gui/app-next/src/lib/l10n/locale.ts
@@ -0,0 +1,135 @@
+import { negotiateLanguages } from '@fluent/langneg';
+import { createSignal, untrack } from 'solid-js';
+import * as v from 'valibot';
+import {
+ inContextMode,
+ reloadInContext,
+ startCrowdinInContext,
+} from '@/lib/l10n/crowdin';
+import { createBundle, setL10nBundles, t } from '@/lib/l10n/translate';
+import { platform } from '@/lib/platform';
+import { definePref } from '@/lib/storage';
+
+export const DEFAULT_LOCALE = 'en';
+// Crowdin pseudo-language, its strings are edit markers for the in-context tool
+const IN_CONTEXT_LOCALE = 'ach-UG';
+
+// Folders under public/i18n offered to users
+export const availableLocales = [
+ 'ar',
+ 'cs',
+ 'da',
+ 'de',
+ 'en',
+ 'es-419',
+ 'es-ES',
+ 'et',
+ 'fi',
+ 'fr',
+ 'it',
+ 'ja',
+ 'ko',
+ 'nb-NO',
+ 'nl',
+ 'pl',
+ 'pt-BR',
+ 'ru',
+ 'sv-SE',
+ 'uk',
+ 'th',
+ 'vi',
+ 'zh-Hans',
+ 'zh-Hant',
+ 'en-x-owo',
+] as const;
+
+const langPref = definePref('lang', v.string(), DEFAULT_LOCALE);
+
+const [locale, setLocale] = createSignal(DEFAULT_LOCALE);
+export { locale };
+
+// Loads run one after another, so the last call is applied last
+let queue: Promise = Promise.resolve();
+let initialized = false;
+
+async function fetchMessages(locale: string) {
+ const response = await fetch(`/i18n/${locale}/translation.ftl`);
+ if (!response.ok)
+ throw new Error(`fetching ${locale} translations: ${response.status}`);
+ return response.text();
+}
+
+function validLocales(requested: readonly string[]) {
+ return requested.filter((tag) => {
+ try {
+ Intl.getCanonicalLocales(tag);
+ return true;
+ } catch {
+ return false;
+ }
+ });
+}
+
+function syncTrayTranslations() {
+ if (inContextMode === 'active') return;
+ platform.translations?.set({
+ 'tray_menu-show': t('tray_menu-show'),
+ 'tray_menu-hide': t('tray_menu-hide'),
+ 'tray_menu-quit': t('tray_menu-quit'),
+ });
+}
+
+async function applyLocale(requested: readonly string[]) {
+ const inContext = inContextMode === 'active';
+ const [match = DEFAULT_LOCALE] = negotiateLanguages(
+ validLocales(requested),
+ availableLocales,
+ {
+ defaultLocale: DEFAULT_LOCALE,
+ strategy: 'lookup',
+ }
+ );
+
+ const override = inContext ? false : await platform.translations?.override();
+ const [current, fallback] = await Promise.all([
+ override || fetchMessages(inContext ? IN_CONTEXT_LOCALE : match),
+ fetchMessages(DEFAULT_LOCALE),
+ ]);
+
+ setL10nBundles([
+ createBundle(match, current),
+ createBundle(DEFAULT_LOCALE, fallback),
+ ]);
+ setLocale(match);
+ document.documentElement.lang = match;
+ syncTrayTranslations();
+}
+
+export function loadLocale(requested: readonly string[]) {
+ const run = queue.catch(() => {}).then(() => applyLocale(requested));
+ queue = run;
+ return run;
+}
+
+export function setLanguage(lang: string) {
+ langPref.set(lang);
+ return loadLocale([lang]);
+}
+
+export function savedLanguage() {
+ return langPref();
+}
+
+export async function initL10n() {
+ if (initialized) return;
+ initialized = true;
+
+ await loadLocale([langPref()]);
+
+ if (inContextMode === 'active') {
+ startCrowdinInContext();
+ platform.translations?.onCrowdinPopupClosed(() => reloadInContext('active'));
+ }
+ // Reload bundles when a .ftl file changes in dev
+ import.meta.hot?.on('locales-update', () => void loadLocale([untrack(locale)]));
+}
diff --git a/gui/app-next/src/lib/l10n/localized.test.tsx b/gui/app-next/src/lib/l10n/localized.test.tsx
new file mode 100644
index 000000000..51880b146
--- /dev/null
+++ b/gui/app-next/src/lib/l10n/localized.test.tsx
@@ -0,0 +1,54 @@
+import { render } from '@solidjs/testing-library';
+import { describe, expect, it } from 'vitest';
+import {
+ createBundle,
+ setL10nBundles,
+ Localized,
+ type LocalizedElems,
+} from '@/lib/l10n';
+import { splitMarkup } from '@/lib/l10n/localized';
+
+function renderLocalized(id: string, elems: LocalizedElems) {
+ return render(() => );
+}
+
+describe('splitMarkup', () => {
+ it('splits tags and plain text', () => {
+ expect(splitMarkup('a bold c')).toEqual([
+ { tag: null, text: 'a ' },
+ { tag: 'b', text: 'bold' },
+ { tag: null, text: ' c' },
+ ]);
+ });
+
+ it('leaves unclosed tags as text', () => {
+ expect(splitMarkup('a bold')).toEqual([
+ { tag: null, text: 'a bold' },
+ ]);
+ });
+});
+
+describe('Localized', () => {
+ it('maps whitelisted tags and renders others as text', () => {
+ setL10nBundles([
+ createBundle(
+ 'en',
+ 'msg = Click here or or
x now'
+ ),
+ ]);
+ const { container } = renderLocalized('msg', {
+ b: (c) => {c},
+ });
+ expect(container.querySelector('strong')?.textContent).toBe('here');
+ expect(container.querySelector('script')).toBeNull();
+ expect(container.querySelector('img')).toBeNull();
+ expect(container.textContent).toBe('Click here or alert(1) or x now');
+ });
+
+ it('follows the locale', () => {
+ setL10nBundles([createBundle('en', 'msg = One')]);
+ const { container } = renderLocalized('msg', { b: (c) => {c} });
+ setL10nBundles([createBundle('fr', 'msg = Un')]);
+ expect(container.querySelector('em')?.textContent).toBe('Un');
+ });
+});
diff --git a/gui/app-next/src/lib/l10n/localized.tsx b/gui/app-next/src/lib/l10n/localized.tsx
new file mode 100644
index 000000000..9adcd4c80
--- /dev/null
+++ b/gui/app-next/src/lib/l10n/localized.tsx
@@ -0,0 +1,45 @@
+import type { JSX } from 'solid-js';
+import { t, type MessageArgs, type MessageId } from '@/lib/l10n/translate';
+
+export type LocalizedElems = Partial<
+ Record JSX.Element>
+>;
+
+export interface LocalizedProps {
+ id: MessageId;
+ args?: MessageArgs;
+ elems?: LocalizedElems;
+}
+
+interface Part {
+ tag: string | null;
+ text: string;
+}
+
+const MARKUP = /<([A-Za-z][\w-]*)>([\s\S]*?)<\/\1>/g;
+
+export function splitMarkup(text: string): Part[] {
+ const parts: Part[] = [];
+ let last = 0;
+ for (const m of text.matchAll(MARKUP)) {
+ if (m.index > last)
+ parts.push({ tag: null, text: text.slice(last, m.index) });
+ parts.push({ tag: m[1]!, text: m[2]! });
+ last = m.index + m[0].length;
+ }
+ if (last < text.length) parts.push({ tag: null, text: text.slice(last) });
+ return parts;
+}
+
+function renderParts(parts: Part[], elems: LocalizedElems) {
+ return parts.map((part) => {
+ const render = part.tag === null ? undefined : elems[part.tag];
+ return render ? render(part.text) : part.text;
+ });
+}
+
+export function Localized(props: LocalizedProps) {
+ return (
+ <>{renderParts(splitMarkup(t(props.id, props.args)), props.elems ?? {})}>
+ );
+}
diff --git a/gui/app-next/src/lib/l10n/translate.ts b/gui/app-next/src/lib/l10n/translate.ts
new file mode 100644
index 000000000..1738563ac
--- /dev/null
+++ b/gui/app-next/src/lib/l10n/translate.ts
@@ -0,0 +1,60 @@
+import { FluentBundle, FluentResource, type FluentVariable } from '@fluent/bundle';
+import { createSignal } from 'solid-js';
+
+export type MessageId = string;
+export type MessageArgs = Record;
+
+const [bundles, setBundles] = createSignal([]);
+
+export function setL10nBundles(next: FluentBundle[]) {
+ setBundles(next);
+}
+
+export function createBundle(locale: string, source: string) {
+ const bundle = new FluentBundle(locale);
+ for (const error of bundle.addResource(new FluentResource(source))) {
+ console.warn(`[l10n] ${locale}:`, error);
+ }
+ return bundle;
+}
+
+function format(
+ bundle: FluentBundle,
+ pattern: Parameters[0],
+ args: MessageArgs | undefined
+) {
+ const errors: Error[] = [];
+ const out = bundle.formatPattern(
+ pattern,
+ args as Record,
+ errors
+ );
+ for (const error of errors) console.warn(`[l10n] ${bundle.locales[0]}:`, error);
+ return out;
+}
+
+function missing(id: string) {
+ if (import.meta.env.DEV) console.warn(`[l10n] missing message ${id}`);
+ return id;
+}
+
+function translate(id: MessageId, args?: MessageArgs): string {
+ for (const bundle of bundles()) {
+ const value = bundle.getMessage(id)?.value;
+ if (value) return format(bundle, value, args);
+ }
+ return missing(id);
+}
+
+function translateAttr(id: MessageId, attr: string, args?: MessageArgs): string {
+ for (const bundle of bundles()) {
+ const pattern = bundle.getMessage(id)?.attributes[attr];
+ if (pattern) return format(bundle, pattern, args);
+ }
+ return missing(`${id}.${attr}`);
+}
+
+// Reactive: reads the bundles signal, so JSX and memos update on locale change.
+// Callers read it in their own tracked scope.
+// eslint-disable-next-line solid/reactivity
+export const t = Object.assign(translate, { attr: translateAttr });
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 459396f65..42aa3949f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -359,9 +359,6 @@ importers:
'@fluent/langneg':
specifier: 0.7.0
version: 0.7.0
- '@fluent/sequence':
- specifier: 0.8.0
- version: 0.8.0(@fluent/bundle@0.19.1)
'@sentry/solid':
specifier: 11.9.0
version: 11.9.0(@solidjs/router@1.0.0(solid-js@1.9.17))(solid-js@1.9.17)
@@ -7284,10 +7281,6 @@ snapshots:
dependencies:
'@fluent/bundle': 0.18.0
- '@fluent/sequence@0.8.0(@fluent/bundle@0.19.1)':
- dependencies:
- '@fluent/bundle': 0.19.1
-
'@fluent/syntax@0.19.0': {}
'@fontsource/jetbrains-mono@5.3.0': {}