Usa la lingua della webapp
La webapp comunica alla tua vista la sua lingua in world.webapp.language come un codice del tipo 'it' o 'en'. Tieni le stringhe della tua vista in ogni lingua che supporti, sceglile in base a quel codice e lascia che il browser formatti numeri e date per quella lingua. L'esempio simula una webapp in italiano.
jsx
import { useEffect } from 'react';
import { useWorld } from './useWorld.js';
// The words of your view, in each language it speaks.
const words = {
en: { visitors: 'Visitors today', language: 'Language' },
it: { visitors: 'Visitatori oggi', language: 'Lingua' },
};
// A fake museum lets you build with no account and no network.
const museum = { name: 'Museo civico', data: { visitors: 12345 } };
export default function App() {
// While building: the fake world, with the webapp in Italian. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: museum }, webapp: { language: 'it' } });
const code = world ? world.webapp.language : null; // 'it', 'en', and while building maybe 'en-US'
// The language of the page, for screen readers, written again when the person changes it.
useEffect(() => {
if (code) document.documentElement.lang = code;
}, [code]);
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
// Pick your words by the first part of the code ('en-US' gives 'en'), or English when you have no other.
const text = words[code.split('-')[0]] ?? words.en;
// The browser writes numbers and dates the way of the language (12.345 in Italian).
const count = new Intl.NumberFormat(code).format(world.data.visitors);
return (
<>
{/* The words of the world, shown as the world says them. */}
<h2>{world.name}</h2>
<p>{text.visitors}: {count}</p>
<p>{text.language}: {code}</p>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// The words of your view, in each language it speaks.
const words = {
en: { visitors: 'Visitors today', language: 'Language' },
it: { visitors: 'Visitatori oggi', language: 'Lingua' },
};
// A fake museum lets you build with no account and no network.
const museum = { name: 'Museo civico', data: { visitors: 12345 } };
// While building: the fake world, with the webapp in Italian. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: museum }, webapp: { language: 'it' } });
const title = document.createElement('h2');
const visitors = document.createElement('p');
const language = document.createElement('p');
document.body.append(title, visitors, language);
// Called at once, then after every change, including when the person changes the language of the webapp.
world.subscribe((node) => {
const code = node.webapp.language; // 'it', 'en', and while building maybe 'en-US'
// Pick your words by the first part of the code ('en-US' gives 'en'), or English when you have no other.
const text = words[code.split('-')[0]] ?? words.en;
// The browser writes numbers and dates the way of the language (12.345 in Italian).
const count = new Intl.NumberFormat(code).format(node.data.visitors);
document.documentElement.lang = code;
title.textContent = node.name; // the words of the world, shown as the world says them
visitors.textContent = `${text.visitors}: ${count}`;
language.textContent = `${text.language}: ${code}`;
});Come funziona
- Mentre sviluppi il codice arriva dal tuo browser e può contenere una regione come in
'en-US'. Prendi come lingua la parte prima del trattino e ripiega su una lingua che supporti quando la tua vista non ha stringhe per quel codice. Intl.NumberFormateIntl.DateTimeFormatformattano numeri e date per la lingua (12,345 in inglese e 12.345 in italiano) senza una tabella tua.- Traduci solo le tue stringhe. Quello che arriva dal mondo (il suo nome, i suoi messaggi,
now.text) resta com'è. - Quando la persona cambia lingua,
subscribechiama di nuovo il suo listener e la vista si adegua senza ricaricare. L'esempio scrive il codice nell'attributolangdella pagina per aiutare gli screen reader a pronunciare il testo. - Per provare una lingua mentre sviluppi passala con
connectToHost({ fake: { world }, webapp: { language: 'it' } }).