Localization
How to translate texts and adapt dates, numbers and currencies to a language.
On this page
Localization has two parts: translating the texts of the application, and showing dates, numbers and currencies the way the user expects.
Marking and translating texts
A translatable text is written as @(text). LOCALIZE(text) loads a dictionary and TRANSLATE(text) replaces the marked texts in a string.
The key of a dictionary entry is the hash of the original text in base 36, created with HASH(text).toString(36). Each line of the dictionary has the form key : translation, and lines starting with // are comments.
var key = HASH('Save changes').toString(36);
console.log(key);
LOCALIZE([
'// German',
key + ' : Änderungen speichern',
HASH('Time').toString(36) + ' : Zeit: 10:30'
].join('\n'));
console.log(TRANSLATE('<button>@(Save changes)</button>'));
console.log(TRANSLATE('@(Time) / @(Not translated)'));
console.log(JSON.stringify(DEF.dictionary));bqgoqo
<button>Änderungen speichern</button>
Zeit: 10:30 / Not translated
{"bqgoqo":"Änderungen speichern","1jvfx":"Zeit: 10:30"}A text without a translation keeps its original wording. Keys may also start with the letter T, as in the resource files of the Total.js framework. Loaded entries are kept in DEF.dictionary, and each call to LOCALIZE() adds to them.
Loading a dictionary from a file
AJAX('GET /locale/de.resource', function(text) {
LOCALIZE(text);
console.log(TRANSLATE('@(Close)')); // Schliessen
});Where texts are translated
Content loaded with IMPORT() and <ui-import> is translated automatically, so load the dictionary first. The static HTML of the page is not translated.
<div id="page">@(Save)</div>
<div id="part"></div>
<script>
LOCALIZE(HASH('Save').toString(36) + ' : Speichern');
// /parts/save.html contains: <button>@(Save)</button>
IMPORT('/parts/save.html', '#part', function() {
console.log($('#part').html()); // <button>Speichern</button>
console.log($('#page').html()); // @(Save)
});
</script>A component translates its own markup by calling TRANSLATE():
COMPONENT('savebutton', function(self) {
self.make = function() {
self.html(TRANSLATE('<button>@(Save)</button>'));
};
});More about loading content is in Importing content.
Sending the language to the server
DEF.language is added to the URL of every AJAX() request, unless the URL already contains the parameter. DEF.languagekey is the name of the parameter, and the default is language. ENVIRONMENT(name, version, language) sets the storage name, the version and the language in one call.
ENVIRONMENT('myapp', '1.5', 'de');
AJAX('GET /api/products/', NOOP);
AJAX('GET /parts/menu.html', NOOP);
AJAX('GET /api/products/?language=en', NOOP);The requests go to these addresses:
/api/products/?language=de
/parts/menu.html?version=1.5&language=de
/api/products/?language=enDates
MONTHS and DAYS hold the names used by Date.format(), and DEF.dateformat is the format used when no format is given. DAYS starts with Sunday.
MONTHS[0] = 'Januar';
DAYS[1] = 'Montag';
DEF.dateformat = 'dd.MM.yyyy';
var date = new Date(2026, 0, 5, 14, 3);
date.format(); // 05.01.2026
date.format('dddd, d. MMMM yyyy'); // Montag, 5. Januar 2026
date.format('HH:mm'); // 14:03The tokens of the format are listed in Date.
Numbers
DEF.thousandsseparator and DEF.decimalseparator control Number.format():
DEF.thousandsseparator = '.';
DEF.decimalseparator = ',';
(1234567.891).format(2); // 1.234.567,89Currencies
DEF.currencies holds a formatting function for each currency, and DEF.currency is the default one. Number.currency() and the Tangular helper currency use them.
DEF.currencies.eur = function(value) {
return value.format(2) + ' €';
};
DEF.currency = 'eur';
(1234.5).currency(); // 1.234,50 €
(1234.5).currency('eur'); // 1.234,50 €
Tangular.render('{{ price | currency }}', { price: 99 }); // 99,00 €The output above uses the separators from the previous section.
Plural forms
Number.pluralize(zero, one, few, other) selects a text by the number. few applies to the numbers from 2 to 4, and other to the rest. # is replaced by the number and ## by the formatted number.
var text = function(count) {
return count.pluralize('No files', '# file', '# files (few)', '# files');
};
text(0); // No files
text(1); // 1 file
text(3); // 3 files (few)
text(5); // 5 files
(1500).pluralize('none', 'one', 'few', '## files'); // 1 500 filesIn a template, the same function is available as the helper pluralize. See Templates with Tangular.