Spanish Total.js
English
Documentation For AI agents
Contents

Localization

Translate texts and adapt dates, numbers, currencies and plurals to the language of the user.

.md GitHub
On this page

Localization has two parts: translating the texts of the interface with a dictionary, and formatting dates, numbers and currencies the way the user expects.

Marking text for translation

Write a translatable text as @(text). TRANSLATE(text) replaces every marked text with its translation from DEF.dictionary. A text without a translation stays in the original language:

DEF.dictionary[HASH('Save').toString(36)] = 'Guardar';
DEF.dictionary[HASH('Cancel').toString(36)] = 'Cancelar';

TRANSLATE('@(Save) | @(Cancel) | @(Delete)');
// Guardar | Cancelar | Delete

The marked text can contain parentheses, as in @(Save (draft)).

Dictionary keys

The key of a translation is the hash of the original text in base 36:

HASH('Save').toString(36); // '1j2ql'

The same key with the prefix T works as well. The library looks for the key without the prefix first.

Loading translations

LOCALIZE(text) adds translations from a text with one key : translation per line. Empty lines, lines without a colon and lines that start with // are skipped. Only the first colon separates the key, so a translation can contain colons:

var key1 = HASH('Save').toString(36);
var key2 = HASH('Opening hours').toString(36);

LOCALIZE([
    '// Spanish',
    key1 + ' : Guardar',
    key2 + ' : Abierto: 9:00 - 17:00'
].join('\n'));

TRANSLATE('@(Opening hours)'); // Abierto: 9:00 - 17:00

Load the dictionary ahead of the components, because the texts are translated when the components start.

Where texts are translated

The library translates marked texts in:

  • the config attribute of <ui-component> and <ui-bind>,
  • templates of <ui-bind>,
  • content loaded with IMPORT() and ui-import,
  • text passed to ADAPT().

Static HTML of the page is not translated:

<script>
    LOCALIZE(HASH('Your name').toString(36) + ': Tu nombre');

    COMPONENT('label', function(self, config) {
        self.make = function() {
            self.text(config.title);
        };
    });

    var user = { name: 'Peter' };
</script>

<!-- Tu nombre -->
<ui-component name="label" config="title:@(Your name)"></ui-component>

<!-- Tu nombre: Peter -->
<ui-bind path="user" config="template">
    <script type="text/html"><b>@(Your name)</b>: {{ value.name }}</script>
</ui-bind>

<!-- @(Your name) -->
<p>@(Your name)</p>

Dates and numbers

These settings of DEF control the default formats:

SettingDefaultDescription
DEF.dateformatyyyy-MM-ddThe format of date.format() without an argument.
DEF.thousandsseparatora spaceThe separator of thousands.
DEF.decimalseparator.The decimal separator.

The names of months and days are in the global arrays MONTHS and DAYS. DAYS starts with Sunday:

MONTHS = 'enero,febrero,marzo,abril,mayo,junio,julio,agosto,septiembre,octubre,noviembre,diciembre'.split(',');
DAYS = 'domingo,lunes,martes,miércoles,jueves,viernes,sábado'.split(',');

DEF.dateformat = 'dd.MM.yyyy';
DEF.thousandsseparator = '.';
DEF.decimalseparator = ',';

var date = new Date(2024, 2, 5, 14, 7);

date.format();                     // 05.03.2024
date.format('dddd, d. MMMM yyyy'); // martes, 5. marzo 2024
(1234567.891).format(2);           // 1.234.567,89

Tangular helpers use the same settings:

Tangular.render('{{ date | format }} {{ price | format(2) }}', { date: date, price: 1500 });
// 05.03.2024 1.500,00

The tokens of a date format are listed in the Date reference.

Currencies

DEF.currencies holds one formatting function per currency, and DEF.currency is the default one. number.currency([name]) uses them. An unknown currency formats the number with two decimals:

DEF.currencies.eur = function(value) {
    return value.format(2) + ' EUR';
};

DEF.currencies.usd = function(value) {
    return '$' + value.format(2);
};

DEF.currency = 'eur';

(1250).currency();      // 1 250.00 EUR
(1250).currency('usd'); // $1 250.00
(1250).currency('gbp'); // 1 250.00

The currency helper in Tangular does the same:

Tangular.render("{{ price | currency }} {{ price | currency('usd') }}", { price: 10 });
// 10.00 EUR $10.00

Plurals

number.pluralize(zero, one, few, other) chooses a text by the number. few covers the numbers from 2 to 4. # writes the number and ## writes it formatted:

(0).pluralize('no files', '# file', '# files', '# files'); // no files
(1).pluralize('no files', '# file', '# files', '# files'); // 1 file
(3).pluralize('no files', '# file', '# files', '# files'); // 3 files

(1500).pluralize('none', 'one', 'few', '## files');        // 1 500 files

Keeping formats in one place

ENV(name, value) stores a value, and formatting methods read it when you write its name in square brackets. A translation of the application can then change the formats in one place:

ENV('files', 'no files,# file,# files,# files');
(2).pluralize('[files]'); // 2 files

ENV('short', 'dd/MM');
new Date(2024, 2, 5).format('[short]'); // 05/03

Telling the server the language

DEF.language adds a language parameter to every request made with AJAX(). DEF.languagehtml adds it only to addresses that contain .html, which is useful when the server translates templates. DEF.languagekey is the name of the parameter:

DEF.language = 'sk';
AJAX('GET /api/products/', callback);
// GET /api/products/?language=sk

DEF.language = '';
DEF.languagehtml = 'de';
AJAX('GET /templates/form.html', callback);
// GET /templates/form.html?language=de

DEF.languagehtml = '';
DEF.language = 'en';
DEF.languagekey = 'lang';
AJAX('GET /api/products/', callback);
// GET /api/products/?lang=en
Made in Europe