---
title: Localization
project: Total.js V5
version: 0.0.20
section: guides
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/guides/localization/
updated: 2026-11-16T15:32:47.498Z
---

# Localization

> Translate views, files and messages with resource files, choose the language of each request and create dictionaries with total5 translate.

Total.js translates text that you mark with `@(...)`. The text stays readable in your views and code, and the framework replaces it with its translation from a **resource file** of the current language. Text without a translation stays as it is, so an application works before any translation exists.

```html
<!-- /views/index.html -->
<h1>@(Welcome)</h1>
<button>@(Add to cart)</button>
```

```
# /resources/sk.resource
T1bx8wg2      : Vitajte
T1oapati      : Pridať do košíka
```

The key of a translation is `T` followed by a hash of the original text (`'T' + 'Welcome'.hash(true).toString(36)` is `T1bx8wg2`). You do not compute the keys yourself: the `total5 translate` command collects all `@(...)` texts of the project into a resource file for you.

## Resource files

A resource file is a plain text file with the `.resource` extension in the `/resources/` folder (subfolders are read too). The name of the file without the extension is the name of the dictionary, usually a language code: `sk.resource` is the dictionary `sk`. Files whose path contains `-bk` or `_bk` are skipped. The files are loaded when the application starts.

Each line is a key, a separator and a value:

```
# a comment
// also a comment
T1bx8wg2      : Vitajte
name          : Meno
title = Obchod
```

- The separator is the first `:` of the line, or the first `=` when the line has no `:`. Keep one space after the separator: the value starts at the second character after it.
- The key and the value are trimmed, so you can align the values with spaces. A value can contain more `:` characters.
- Empty lines and lines that start with `#` or `//` are ignored.

Each dictionary is a plain object in `F.resources`. Loading a dictionary of the same name again replaces it as a whole; it does not merge the keys.

### The default dictionary

`default.resource` is the fallback. When a key is missing in the dictionary of the current language, or the language has no dictionary, the key is read from `default`. Text in `@(...)` without a translation in either dictionary stays untranslated.

## Creating the dictionary

Run the `translate` command of the `total5` tool in the root of your project:

```bash
total5 translate
total5 translate resources/sk.resource
```

It reads all `.js` and `.html` files in the current directory and its subfolders, finds every `@(...)` text and writes the file (`translate.resource` when you do not give a name). Each line has the key and the original text:

```
T1bx8wg2     : Welcome
T1oapati     : Add to cart
```

Copy the file into `/resources/` under the name of a language and translate the values.

## Choosing the language

The language of a request is a string in `$.language`. It is empty by default, which means the `default` dictionary. Register a delegate with `LOCALIZE()` that returns the language for each request:

```js
// /definitions/localize.js
LOCALIZE(function($) {
	var language = $.query.language || ($.headers['accept-language'] || '').substring(0, 2).toLowerCase();
	return language === 'sk' || language === 'de' ? language : 'en';
});
```

The delegate gets the controller of the request. It runs after the authorization, so `$.user` is set and you can return the language of the user:

```js
LOCALIZE(function($) {
	return $.user ? $.user.language : 'en';
});
```

The delegate runs for routes (before their middleware), for static `.html` files and for WebSocket routes. It is not called when the language is already set, for example by the `localize` option of the built-in session authorization (see [Authorization](../../guides/authorization/)). In a route you can also set the language yourself before you render a view:

```js
function index($) {
	$.language = 'sk';
	$.view('index');
}
```

Inside an action, `$.language` returns the language of the request that called the action. It is read-only there.

## Views and static HTML files

A view is translated when it is compiled, before its commands run, so `@(...)` holds plain text only. In release mode every language gets its own compiled copy of the view. The language of the view is the language of the request at the time you call `$.view()`; `@{language}` prints it. For `F.view()` set `view.language` in the `prepare` function; `@{language}` is empty there. The [Views](../../guides/views/) guide explains the rest.

```js
var html = F.view('mail/order', { id: 'A100' }, function(view) {
	view.language = 'de';
});
```

Static `.html` files from `/public/` are translated the same way and cached per language, except files whose name contains `.min`, `-min` or `@min`.

## Translating in code

`TRANSLATE(language, text)` replaces every `@(...)` in a string and returns the result. Use it for text you build in code, JSON responses or files.

```js
var title = TRANSLATE($.language, '@(Order) #' + order.number);
```

`F.resource(language, key)` reads one key of a dictionary by its own name, with the same fallback to `default`. It returns an empty string when the key does not exist.

```js
// /resources/de.resource contains: currency : EUR
var currency = F.resource('de', 'currency') || 'USD';
```

## Error messages

Errors are translated into the language of the request when they are sent. A message in `@(...)` is translated like any other text:

```js
NEWACTION('Orders/create', {
	name: 'Create an order',
	input: '*name:String',
	action: function($, model) {
		if (!model.name.trim()) {
			$.invalid('@(The name is required)');
			return;
		}
		$.success();
	}
});
```

A field that fails the validation of a schema is looked up in the resources by the name of the field (`name : Enter your name`). Without such a key the message is `The field "name" is invalid`. `ErrorBuilder.output(language)` and `toString(language)` translate the errors when you work with an `ErrorBuilder` yourself.

## E-mails and templates

`MAIL()` takes the language after the model. The subject and the view are translated into that language:

```js
MAIL(user.email, '@(Your order)', 'mail/order', order, user.language, function(err) {
	err && console.error(err);
});
```

`HTMLMAIL(email, subject, body, [language], [callback])` translates the subject and the HTML body. `TEMPLATE(body, model, $)` translates the template into `$.language`. The [Mail](../../guides/mail/) and [Views](../../guides/views/) guides have the details.

## Names of months and days

`date.format(format, [dictionary])` reads the names of months (`MMMM`, `MMM`) and days (`dddd`, `ddd`) from a dictionary, with the English name as the key:

```
# /resources/sk.resource
January : január
Monday  : pondelok
```

```js
new Date(2024, 0, 1).format('dddd d. MMMM', 'sk'); // pondelok 1. január
```

## Loading resources from code

`LOADRESOURCE(name, text)` parses the text of a resource file and stores it as a dictionary. Without the text, the first argument is a URL: the framework downloads it and loads every dictionary in the JSON response.

```js
LOADRESOURCE('sk', 'T1bx8wg2 : Vitajte');
LOADRESOURCE('https://example.com/translations.json');
```
