Translations
Translate your app with locale files, the t function, and a runtime language switch.
Puzzle's translations are opt-in. Declare your locales in puzzle.config.js, write one JSON file per locale, and print strings with the standard t function. The compiler validates the files and ships each one as a small hashed file, and the browser fetches only the active locale. An app that configures no translations ships none of this.
Configure locales
Add i18n to puzzle.config.js:
// puzzle.config.js
export default {
styles: { use: ['tailwindcss'] },
i18n: {
locales: ['en', 'es', 'pl'],
defaultLocale: 'en',
},
};locales lists BCP 47 tags written with a hyphen (pt-BR, never pt_BR) and must include defaultLocale. Every listed locale needs a file.
Locale files
Put one file per locale in app/locales/, named by its tag: app/locales/en.json, app/locales/es.json. Files may nest, and nesting flattens to dotted keys, so home.cart.title below is a key:
{
"brand": "Puzzle Market",
"search": { "hint": "Search products" },
"home": {
"greeting": "Welcome back, {name}!",
"cart": {
"title": "Your cart",
"items": {
"zero": "Your cart is empty",
"one": "{count} item",
"other": "{count} items"
}
}
}
}- A value is a string, a nested namespace, or a plural entry: an object whose keys are all CLDR categories (
zero,one,two,few,many,other). A plural entry must haveother. - The build checks every file and reports a positioned error for a bad value type, two keys that flatten to the same name, a plural entry without
other, or a file named with an underscore. - A key missing from another locale is filled from the default locale, with a one-line warning per locale, so a page never shows a hole.
The t function
Call t with a key anywhere an expression goes: text, attributes, and props:
<h1>{ t('home.greeting', { name: user.name }) }</h1>
<h2>{ t('home.cart.title') }</h2>
<p>{ t('home.cart.items', { count: cart.items.length }) }</p>
<input placeholder={ t('search.hint') } />- Placeholders. Pass variables as one object, either an inline literal (
t('home.greeting', { name: user.name })) or any object value, including a store record.{name}placeholders fill in one pass, and a model's computed getters work as variables. A placeholder with no matching variable stays visible as written. - Plurals. With a numeric
count, a plural entry picks its form throughIntl.PluralRulesfor the active locale, and{count}prints in that locale's number format. An exact0uses thezeroform when the entry has one, in every locale including English, so "Your cart is empty" needs no template branch. - Missing keys print the key, never a blank, with a development warning. A literal key the default locale lacks is also a build warning.
- Keys can be built at runtime:
{ t('status.' + order.status) }. - Output is text. Markup inside a translation prints literally.
In script code, this.ctx.i18n.t(key, vars) is the same function.
Switching locales
With i18n configured, this.ctx.i18n (and app.i18n) carries locale, locales, defaultLocale, t(), and setLocale(tag). A language picker is a few lines:
<!-- components/LocaleSwitcher.pzl -->
<puzzle-view>
<div class="flex gap-2">
{#for option in options}
<button key={ option.locale } aria-pressed="{ option.active }" @click={ choose(option.locale) }>{ option.label }</button>
{/for}
</div>
</puzzle-view>import { PuzzleView } from '@magic-spells/puzzle';
export default class LocaleSwitcher extends PuzzleView {
data(params, props) {
return { options: this.ctx.i18n.locales };
}
events = {
choose: (tag) => {
this.ctx.i18n.setLocale(tag).catch((err) => console.error(err));
},
};
}setLocale fetches the new file first, then switches the strings, the locale the date and number functions use, and <html lang> together (memory mode leaves the document language alone), remembers the choice, and rebuilds the page at the same location: no history entry, no scroll jump, no animations. It rejects if the file fails to load (nothing changes) or the rebuild fails, and an unconfigured tag throws a RangeError. Store records survive the switch, but setData local state does not, so keep state that must outlive a language change in the store.
On startup the app picks the stored choice, then the browser's preferred languages (an exact tag, then the base language, then a configured tag with the same base), then the default. The first render always has its strings.
Dates and numbers follow the locale
Once translations are configured, the active locale replaces the viewer's in every locale-aware function: date, time, datetime, number_with_delimiter, compact_number, the pluralize count, and timeago. An explicit locale argument (date(at, 'long', 'fr')) still wins, and currency is unchanged. Without i18n, those functions use the viewer's locale.
Prerendered pages
--static and --hybrid builds prerender eligible pages in the default locale, set <html lang> to it, and carry its table inline, so a default-locale visitor makes no extra request. Any other locale loads one small file.
Testing
mountView and createTestApp from @magic-spells/puzzle/testing take an i18n: { locale, strings } option, so a test can render a view with a known table and no locale files.
Not in this release
Rich-text translations and typed keys for puzzle check are future work.
Next
- Templates: the rest of the function library.
- Static Builds: how prerendered pages carry the default locale.