Translate and pluralise with i18n
Translate a page into two locales: parameters, plurals, numbers, currency and dates.
Apache-2.0Runs on this page
Package: @awacloud/fw
Two locale catalogs, a message with a parameter, a pluralised count and locale-aware number, currency and date formatting, switched with a select.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
Your page speaks more than one language and you want one place for the
strings, correct plurals per language (Intl.PluralRules) and native
formatting of numbers, money and dates without a locale data download.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. - Move
CATALOGSto JSON files if they grow;addCatalog(locale, dict)merges them at run time.
What it shows
i18n.create({ locale, fallback, catalogs }), thent(key, params)with{name}interpolation andtn(key, count)pickingone/otherfrom the locale's plural rules.format(value, 'number' | 'currency' | 'date' | 'relative', options)on top of theIntlformatters, following the active locale.setLocaleon one shared instance: the page re-renders from the same keys.- A missing key falls back to the fallback locale, then to the key itself, so a hole in a catalog never throws.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { i18n } from '@awacloud/fw/io/i18n/i18n.js';
fw.runtime.registerAllDeep([i18n]);
/** Catalogs are plain objects; plural entries are keyed by CLDR forms. */
export const CATALOGS = {
'en-US': {
greeting: 'Hello, {name}!',
inbox: { one: 'You have {count} unread message.', other: 'You have {count} unread messages.' },
},
'de-DE': {
greeting: 'Hallo, {name}!',
inbox: { one: 'Du hast {count} ungelesene Nachricht.', other: 'Du hast {count} ungelesene Nachrichten.' },
},
};
/** The app's translator: one instance, locale switched at run time. */
export const t = fw.runtime.resolve('i18n').create({ locale: 'en-US', fallback: 'en-US', catalogs: CATALOGS });
const SAMPLE_DATE = new Date('2026-03-14T09:26:53Z');
/**
* Everything the page prints for a locale and an unread count.
* @param {string} locale
* @param {number} count
* @returns {Record<string, string>}
*/
export function render(locale, count) {
t.setLocale(locale);
return {
greeting: t.t('greeting', { name: 'Ada' }),
inbox: t.tn('inbox', count),
number: t.format(1234567.891, 'number'),
price: t.format(19.99, 'currency', { currency: 'EUR' }),
date: t.format(SAMPLE_DATE, 'date', { dateStyle: 'long', timeZone: 'UTC' }),
relative: t.format(-3, 'relative', { unit: 'day' }),
};
}
const locale = /** @type {HTMLSelectElement} */ (document.getElementById('locale'));
const count = /** @type {HTMLInputElement} */ (document.getElementById('count'));
function update() {
for (const [id, text] of Object.entries(render(locale.value, Number(count.value) || 0))) {
/** @type {HTMLElement} */ (document.getElementById(id)).textContent = text;
}
}
locale.addEventListener('change', update);
count.addEventListener('input', update);
update();index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Translate and pluralise with i18n</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Translate and pluralise with i18n</h1>
<label for="locale">Locale</label>
<select id="locale">
<option value="en-US" selected>English (US)</option>
<option value="de-DE">Deutsch</option>
</select>
<label for="count">Unread messages</label>
<input id="count" type="number" min="0" value="1">
<dl>
<dt>Greeting</dt><dd><output id="greeting"></output></dd>
<dt>Inbox</dt><dd><output id="inbox"></output></dd>
<dt>Number</dt><dd><output id="number"></output></dd>
<dt>Price</dt><dd><output id="price"></output></dd>
<dt>Date</dt><dd><output id="date"></output></dd>
<dt>Relative</dt><dd><output id="relative"></output></dd>
</dl>
</main>
</body>
</html>