What gets translated

All text in a chart is HTML: the title, the names of the rows and columns, and the badges of annotations. A translation script can translate it like any other text on the page.

Numbers are not translated. They are formatted for the language of the page. See Numbers.

Translation keys

Many translation scripts find text by a translation key in an attribute, such as data-i18n. If the figure has data-chart-keys, Tablechart adds translation keys to the names in the table. The value of data-chart-keys is the start of each key: -row1, -row2 and so on for the names of the rows, and -col1, -col2 and so on for the column headers. The names in the chart get the same keys. The line under a candlestick chart whose first rows are cropped gets -crop.

HTML
<figure data-chart="column" data-chart-keys="apples">
	<table>
		<tbody>
			<tr><th data-i18n="apples-row1">Spring</th><td>58</td></tr>
			<tr><th data-i18n="apples-row2">Summer</th><td>72</td></tr>
		</tbody>
	</table>
</figure>

If a cell already has a key, Tablechart keeps it. The title and the badges are your own HTML, so give them a key yourself.

Without data-chart-keys, Tablechart adds no translation keys.

If you want to use another attribute for the keys, set the langattribute option. If you want to add no keys at all, also on figures with data-chart-keys, set it to false.

JS
init({ langattribute: "data-key" });

Example

The figure has data-chart-keys="harvest", so Tablechart adds the translation keys harvest-row1 to harvest-row3 to the names of the rows. The title and the subtitle have keys of their own. Each language has a file with a text for each key:

es.json
{
	"harvest-title": "Manzanas recogidas",
	"harvest-unit": "miles",
	"harvest-row1": "Primavera",
	"harvest-row2": "Verano",
	"harvest-row3": "Otoño"
}
fr.json
{
	"harvest-title": "Pommes cueillies",
	"harvest-unit": "milliers",
	"harvest-row1": "Printemps",
	"harvest-row2": "Été",
	"harvest-row3": "Automne"
}

The buttons do what a translation script does: they replace the text of each element with a key, and then set the lang attribute of the page. The formatting of the numbers changes with the language, and the chart is redrawn for the new names.

This page has no real translation engine. The buttons use a small script that only knows the words of this example, as a demo. On your site, use your own translation solution. In a Reveal.js presentation, you can use reveal.js-internation.

Apples picked
thousands
Spring 58.5
Summer 72.0
Autumn 70.5
HTML
<figure id="harvest" data-chart="column" data-chart-keys="harvest">
	<figcaption><b data-i18n="harvest-title">Apples picked</b><br><span data-i18n="harvest-unit">thousands</span></figcaption>
	<table>
		<tbody>
			<tr><th>Spring</th><td>58.5</td></tr>
			<tr><th>Summer</th><td>72.0</td></tr>
			<tr><th>Autumn</th><td>70.5</td></tr>
		</tbody>
	</table>
</figure>

When to translate

Tablechart makes the names in the chart when it starts. So a translation script needs to run after init() has finished. init() returns a promise.

JS
import { init } from "@martinomagnifico/tablechart";

await init();
translatePage();

Changing the language

If the lang attribute of the html element changes, the formatting of the numbers changes with it. Then the charts are redrawn, a moment later, so a translation script has time to change the names first.

If a script changes the text of a chart without changing lang, lay the charts out again with refresh().

JS
const tablechart = await init();

translatePage();
tablechart.refresh();

Next

If you want to check what screen readers read, see Accessibility.