Skip to content

Use the language of the webapp ​

The webapp passes its language to your view in world.webapp.language as a code such as 'it' or 'en'. Keep the strings of your view in every language you support, pick them by that code and let the browser format numbers and dates for that language. The example simulates a webapp in Italian.

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}`;
});
Live, on a fake world
Open

How it works ​

  • While you build, the code comes from your browser and can carry a region such as 'en-US'. Take the part before the hyphen as the language and fall back to one you support when your view has no strings for it.
  • Intl.NumberFormat and Intl.DateTimeFormat format numbers and dates for the language (12,345 in English and 12.345 in Italian) without a table of your own.
  • Translate only your own strings. What comes from the world (its name, its messages, now.text) stays as it is.
  • When the person changes the language, subscribe calls its listener again and the view follows without a reload. The example writes the code into the lang attribute of the page to help screen readers pronounce the text.
  • To try a language while you build, pass it with connectToHost({ fake: { world }, webapp: { language: 'it' } }).

See also ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.