Localization

Complex web applications are often multi-language. Entropy has many built-in localization features so you can easly support multiple languages without any additional libraries.

Setting App Locale

You can set the default (fallback) app locale in src/main.ts file:

      

src/main.ts

const server = await createServer({ config: { locales: { default: 'en', }, }, // ... });

Translations

Translations are stored in JSON files inside /locales directory. It does not exist by default, but you can create it if you're using localization features.

For example, if your app supports Polish language with English as default, just create a new locales/pl.json file with translations. To get translated contents, use the translate request method and pass the default message to it:

      

locales/pl.json

{ "Hello World": "Witaj Świecie" }
      

src/posts/post.controller.ts

// Locale is automatically detected from request... const message = await request.translate('Hello World');

Pluralization

You can also use pluralization in your translations. Just pass the quantity as the second argument:

      

src/posts/post.controller.ts

// Automatically determine whether to translate singular or plural word const message = await request.translate('New user', users.length);

In translation file you can define pluralization rules by using | delimiter and passing an array of translations. The first translation is used for singular, the second for plural:

      

locales/pl.json

{ "New user|New users": ["Nowy użytkownik", "Nowi użytkownicy"] }

View Translations

You can also display translated text directly in view templates using $translate function (or its double-underscore __ shorthand):

        

views/home.html

<h1>{{ $translate('Hello World') }}</h1> <section> <p>{{ __('New post', posts.length) }}</p> </section>
HTTP Client CSRF Protection