gipity add i18n makes a web app multi-language: a language picker, localStorage persistence, automatic RTL (<html dir>), and plural/translation lookup. It installs into src/packages/i18n/.
Install, then route copy through t()
Templates don't ship a copy table or an @gipity/i18n import, so install the kit first:
gipity add i18n
The install:
- maps
@gipity/i18nto the kit's runtime and@app/stringstosrc/js/strings.jsin the page's import map; - scaffolds
src/js/strings.js(only if it doesn't exist yet) with a few sample keys.
Then move the app's user-facing text into strings.js and replace hard-coded copy with t('key'):
// src/js/strings.js - the one place copy lives (English is the base)
export const strings = {
welcome: 'Welcome',
greeting: 'Hello, {name}!',
items_one: '{count} item',
items_other: '{count} items',
};
import { t, tn, getLang } from '@gipity/i18n';
el.textContent = t('welcome');
el.textContent = t('greeting', { name: 'Sam' }); // "Hello, Sam!" - {placeholder} interpolation
el.textContent = tn('items', cart.length); // plural-aware: items_one / items_other
A missing key renders as the key itself, never blank.
Add languages
"translate strings to Spanish and Japanese"
Translations land in src/packages/i18n/translations.js, mirroring the keys in strings.js. English stays the base in strings.js and never appears there.
export const translations = {
es: { welcome: 'Bienvenido', items_one: '{count} artículo', items_other: '{count} artículos' },
ja: { welcome: 'ようこそ', items_other: '{count}個' },
};
The runtime API
import { t, tn, getLang, setLang, availableLangs, isRtl } from '@gipity/i18n';
setLang('es'); // switch + persist; getLang()/<html lang|dir>/listeners all updated synchronously
availableLangs(); // ['en', ...languages present in translations.js]
The picker is a <select> auto-mounted to <body> once at least one translation exists - nothing to wire. Style it via the .lang-picker class (web-simple's styles.css already positions it top-right). To place it yourself, mountLangPicker({ getLang, setLang, availableLangs }, parentEl).
Re-rendering on language change
setLang() (and the picker) fires an i18n:changed event on document, synchronously - by the time it returns, getLang() and <html lang>/<html dir> are already updated. Re-pull your text in a render() wired to it (add this to main.js yourself; the templates don't include it):
function render() { /* your t('key') → DOM assignments */ }
document.addEventListener('i18n:changed', render);
render();
Notes
- Add it early. Routing copy through
t()as you build the UI is cheap; bolting it onto a finished app with hard-coded strings means moving all that copy intostrings.jsafterward. - Plurals use
_one/_other(plus_zero/_two/_few/_manywhere a language needs them) perIntl.PluralRules. - RTL scripts (
ar,he,fa,ur,yi) flip layout automatically. - Default language is predictable. First load defaults to English (the base copy in
strings.js); the kit only auto-switches to a detected browser language when the app actually ships that language (i.e. it's intranslations.js). A guest on anarbrowser visiting an English-only app stays English (no surprise RTL flip) - the always-visible picker lets them switch if the guess was wrong, and that explicit choice persists inlocalStorage. To make a different language the default instead of English, set the owner's language as the first/base copy. - Number/date/currency aren't wrapped -
Intl.*is a one-liner withgetLang():new Intl.NumberFormat(getLang(), { style: 'currency', currency: 'USD' }).format(99.5). - Web only. For server-side / function copy, format with
Intl.*directly.