# Localized text

> pixi-silk/i18n draws translated rich text in any script from bundles shaped with HarfBuzz at build time. No font file and no shaper at runtime.

Source: https://pixi-silk.schmooky.dev/docs/localized-text/

`pixi-silk/i18n` shows translated, styled text from `.silk` bundles. The `silk` command of
`@pixi-silk/compiler` builds them from a spreadsheet, your fonts and a theme. It shapes every
message with HarfBuzz, bakes the line break opportunities and bidi levels, and writes MSDF atlas
pages. The browser only lays out lines and draws, so it downloads no font and no shaper.

Demo sources run in the docs demo runner, `defineDemo({ setup({ stage, params, tick }) })`. In your own app, `stage` is `app.stage` (or a container on it), `params` holds the values of the demo controls, and `tick(fn)` is `app.ticker.add`, with `t` and `dt` in seconds.

Live demo `i18n-shop` (source):

```ts
import { Assets, extensions, Graphics, type Texture } from 'pixi.js';
import { SilkGraphics } from 'pixi-silk';
import type { ThemeData } from 'pixi-silk/i18n';
import { Silk, SilkMessage, silkBundleLoader } from 'pixi-silk/i18n/pixi';
import { defineDemo, INK } from './runtime';

// The shop screen of the localized text guide: English and Arabic from baked .silk bundles, no
// font file and no shaper. The counter counts up through the value-fast tier, the plural form
// changes on the way, and the terms link reports taps.
export default defineDemo({
    title: 'Localized shop screen',
    size: [720, 380],
    phone: { size: [360, 640] },
    controls: {
        count: { type: 'range', label: 'coins', min: 0, max: 2000, step: 1, value: 1250 },
        gold: { type: 'select', label: 'gold', options: ['#FFC53D', '#7CFFB2', '#FF7AB8'], value: '#FFC53D' },
    },
    renderers: ['webgl', 'webgpu'],
    setup({ stage, params, tick, phone, silk: app }) {
        let alive = true;
        const messages: SilkMessage[] = [];
        let theme: ReturnType<Silk['createTheme']> | null = null;
        let lastCount = -1;
        let lastGold = '';

        (async () => {
            extensions.add(silkBundleLoader);
            const silk = await Silk.create({ dynamicText: { mode: 'canvas' } });
            const base = `${import.meta.env.BASE_URL.replace(/\/$/, '')}/i18n`;
            const [en, ar, themeData] = await Promise.all([
                Assets.load(`${base}/en.silk`),
                Assets.load(`${base}/ar.silk`),
                fetch(`${base}/theme.json`).then((r) => r.json() as Promise<ThemeData>),
            ]);

            await silk.addBundle(en);
            await silk.addBundle(ar);
            if (!alive) return;
            const coin = new Graphics()
                .circle(32, 32, 30)
                .fill('#FFC53D')
                .circle(32, 32, 20)
                .stroke({ width: 4, color: '#B07A00' });
            const coinTexture: Texture = app.app.renderer.generateTexture({ target: coin, resolution: 2 });

            silk.registerInline({ coin: { texture: coinTexture, size: '1.1em', baseline: 0.15 } });
            theme = silk.createTheme(themeData);
            const columns = phone ? [16, 16] : [24, 376];

            for (const [i, locale] of ['en', 'ar'].entries()) {
                const set = await silk.prepare({
                    locale,
                    fallbackLocale: 'en',
                    theme,
                    defaults: { size: phone ? 22 : 24, maxWidth: phone ? 328 : 320, lineHeight: 1.35 },
                    messages: {
                        title: { key: 'shop.title', size: phone ? 34 : 40 },
                        // a name typed by the player: drawn by the canvas path, mixed direction on purpose
                        greeting: {
                            key: 'player.greeting',
                            values: { name: locale === 'ar' ? 'Alex' : '\u0644\u064a\u0644\u0649' },
                        },
                        coins: { key: 'shop.coins', values: { count: params.count } },
                        terms: { key: 'shop.terms', size: 18 },
                    },
                });
                const x = columns[i];
                const y0 = phone ? 24 + i * 300 : 40;
                const title = new SilkMessage(set.get('title'));
                const greeting = new SilkMessage(set.get('greeting'));
                const coins = new SilkMessage(set.get('coins'));
                const terms = new SilkMessage(set.get('terms'));

                title.position.set(x, y0);
                greeting.position.set(x, y0 + 70);
                coins.position.set(x, y0 + 120);
                terms.position.set(x, y0 + 190);
                terms.on('tagtap', (e) => {
                    terms.alpha = terms.alpha === 1 ? 0.6 : 1;
                    void e;
                });
                stage.addChild(title, greeting, coins, terms);
                messages.push(coins);
            }
        })().catch((e) => console.error('i18n-shop:', e));

        stage.addChild(
            new SilkGraphics().roundRect(8, 8, phone ? 344 : 704, phone ? 624 : 364, 20, 0.6).fill(INK.surface),
        );
        tick(() => {
            if (!theme) return;
            if (params.count !== lastCount) {
                lastCount = params.count;
                for (const m of messages) m.set({ count: params.count });
            }
            if (params.gold !== lastGold) {
                lastGold = params.gold;
                theme.set('gold', { fill: params.gold });
            }
        });

        return () => {
            alive = false;
        };
    },
});
```

## Compile the strings

```bash
npx silk compile --config silk.config.json --strings strings.xlsx --theme theme.json --out public/i18n
```

The sheet has a `key` column, then one column per locale and plural form, such as `en.other` or
`ar.few`. Messages use `{name}` for values, `<tag>...</tag>` for styles from the theme, and
`<coin/>` for inline objects.

## Show a message

```ts

extensions.add(silkBundleLoader);
const silk = await Silk.create();

await silk.addBundle(await Assets.load('/i18n/ar.silk'));
const theme = silk.createTheme(await (await fetch('/i18n/theme.json')).json());
const set = await silk.prepare({
    locale: 'ar',
    theme,
    defaults: { size: 24, maxWidth: 320 },
    messages: { coins: { key: 'shop.coins', values: { count: 0 } } },
});
const coins = new SilkMessage(set.get('coins'));

app.stage.addChild(coins);
coins.set({ count: 1250 });
theme.set('gold', { fill: '#FFF4B0' });
```

`coins.set` with a number of the same length rewrites only that number's glyphs. A new plural
form swaps the baked variant. `theme.set` with colours, outlines, glows, shadows or lines changes a
few style rows and moves no glyph.

## Text slots

A text slot, declared as `text(maxLength=16)` in the config's `slots`, holds text typed by players,
which the compiler cannot bake. Pick how it is drawn with `dynamicText`. Both paths draw the value as a distance field, so the slot's outline,
glow and shadow apply.

```ts

// the browser's fonts and shaping: nothing to download
const quick = await Silk.create({ dynamicText: { mode: 'canvas' } });

// the baked font, shaped with kb_text_shape in WebAssembly
const exact = await Silk.create({
    dynamicText: await createShapedText({
        font: '/fonts/NotoSansArabic-Regular.ttf',
        fallback: await createCanvasText(),
    }),
});
```

The canvas path is small and draws emoji, but the system font may not match the baked text.
`pixi-silk/i18n/shaper` loads 62 kB of WebAssembly (brotli) plus the font file. Slots then use the
same font, shaping and metrics as the baked text on every platform. It reads TrueType outlines
only. Values the font has no glyph for go to `fallback`.
