Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

3.3 KB
Raw Blame
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bento authors
//
// Facade: the i18n ENGINE lives in the shared kernel; CATALOGS are per-app
// string data and live in ./i18n/. This module registers them at import time
// and re-exports the engine, so registration is guaranteed to precede the first
// t() call by ES module evaluation order — the same arrangement slides and
// spaces use.
//
// ENGLISH-STRING-AS-KEY (gettext style): the source sentence IS the key, so a
// missing entry falls back to readable English rather than a blank. Never call
// t() in a module-level const — it would freeze at import time, before the
// viewer's locale is resolved. That rule is why the chrome sets its labels from
// script rather than from the markup template.
//
// The per-locale catalogs in ./i18n/*.ts are the SOURCE OF TRUTH (that is what
// a translator reviews and a contributor submits). What SHIPS is the key-once
// packing of them, generated by scripts/build-type-i18n.mjs — importing the
// per-locale files here instead would put every English key in the bundle
// seven times over. Regenerate after editing any catalog:
//
//     node scripts/build-type-i18n.mjs
//
// CATALOGS LANDED (was: deliberately none). The comment that used to live here
// explained why translating an 85-string app on its way to a few hundred would
// waste the work and ship half-translated chrome — lists, tables, images,
// links and find were all still to come. They are in now: comments, citations,
// mathematics, cross-references, tables, images, tracked changes, named
// styles, embeds, collaboration, the selection toolbar, the block grip. The
// string set has stopped moving, so the sweep (already done, ahead of this)
// and the translation (this) no longer have to be done twice.

import { PACKED, PACKED_LOCALES } from './i18n/packed.ts';
import { registerI18n } from '../../kernel/src/i18n.ts';
import type { LocaleChoice } from '../../kernel/src/i18n.ts';

export type { Catalog } from '../../kernel/src/i18n.ts';

/** Offered in the About picker, each labelled in its own language. */
const CHOICES: LocaleChoice[] = [
  { code: 'en', label: 'English' },
  { code: 'ja', label: '日本語' },
  { code: 'zh-Hans', label: '简体中文' },
  { code: 'zh-Hant', label: '繁體中文' },
  { code: 'es', label: 'Español' },
  { code: 'fr', label: 'Français' },
  { code: 'de', label: 'Deutsch' },
  { code: 'it', label: 'Italiano' },
];

registerI18n({
  packed: {
    locales: PACKED_LOCALES,
    table: PACKED,
    // Regional codes a browser actually reports, mapped onto a bundled column.
    // Without these a zh-TW or zh-HK reader falls all the way back to English
    // despite the traditional catalog being right there.
    alias: {
      zh: 'zh-Hans',
      'zh-CN': 'zh-Hans',
      'zh-SG': 'zh-Hans',
      'zh-TW': 'zh-Hant',
      'zh-HK': 'zh-Hant',
      'zh-MO': 'zh-Hant',
    },
  },
  choices: CHOICES,
});

/**
 * The BUNDLED locales only. Call sites that need the live list — including any
 * language a future runtime pack added — must use `localeChoices()` instead:
 * this const is frozen at import and can never grow.
 */
export const LOCALE_CHOICES = CHOICES;

export { t, locale, setLocale, localeChoices } from '../../kernel/src/i18n.ts';
export type { LocaleChoice } from '../../kernel/src/i18n.ts';