Express Integration

The Node SDK offers many methods and helper functions. The following sections will provide a detailed description for every method and its options.

Common Methods

Common Methods

tr

Translation helper available in controllers and views.

tr(label, description, tokens, options)

# The description parameter can be omitted
tr(label, tokens, options)  

Some examples of how the method can be used:

// EJS Templating
<%= tr("Hello World") %>  
Name Type Required Description
label String true The label to translate
description String false Description of the label that acts as a hint to the translator as well as contextualizes the key to be unique for the description
tokens Object false If label uses data or decoration tokens, the token values will be provided using this parameter
options Object false Additional attributes

Options:

  • source - sets source of strings in your dashboard, source is artificial separation of strings
  • locale - locale of the string it can be sometimes different from default locale
  • target_locale - used to set string to specific local which will not depend on default local, can be used for some text that you always want to see in specific locale
  • skip_decorations - if set to true, content will not be decorated

There are a number of ways the tokens can be passed to the tr method.

The token value can be a simple string:

<%= tr("Hello {user}", {user: "Michael"}) %>  

It can also be an object, in which case the object will be evaluated for the context rules and the result of toString method will be used for display:

<%= tr("Hello {user}", {user: currentUser}) %>  

If you would like to change the way the object is displayed, you can use an array where the first element is the object and the second element is the representation of the value:

<%= tr("Hello {user}", {user: [currentUser, currentUser.name]}) %>  

You can simply pass the name of the attribute as a symbol:

<%= tr("Hello {user}", {user: [currentUser, "name"]}) %>  

You could even define a custom function for extracting the content and pass it in the second element:

function userLink(user){  
  return "<a href='/users/"+user.id+"'>" + user.name + "</a>";
}

<%= tr("Hello {user}", {user: [current_user, userLink]}) %>  

You can also be more explicit and use objects instead of arrays:

<%= tr("Hello {user}", {user: {object: currentUser, attribute: "name"}}) %>  


trl

Translating labels, can be used in views, controllers, and models. trl is the same as tr function but sets the option to never use inline translation for the elements:

trl(label, description, tokens, options)

# The description parameter can be omitted
trl(label, tokens, options)  

Example:

<img alt="<%= trl('Hello World') %>" src="...">  

trl will not output decorators, only the translated string. All parameters from tr will work with trl.

trh

Translate HTML Block helper

trh(label, description, tokens, options)  

Example:

<%= trh("<p>your html block goes here</p>") %>  

All parameters from tr will work with trh.

tml_scripts

Generates the script for including the Translate Exchange Tools. This is required for enabling translation mode and using the in-context translation tools.

<%= tml_scripts %>  

The tml_scripts should always be added in the head section of your layout files.


tml_language_selector

Generates a Translation Exchange Language Selector. It has several options for you to try and select the best fit for your web site.

tml_language_selector(type, options)  

Example:

<%= tml_language_selector("default") %>  
Name Type Required Description
type String false The name of the language selector. Defaults to "default"
options Object false Additional options for the specific chosen language selector

For a list of all available language selectors go the Language Selectors section of the docs.


tml_application

Object containing information about the current application.

tml_application  

Properties:

  • key - The application key
  • name - The project name
  • default_locale - The source language locale code (e.g. "en")
  • languages - List of all application languages


tml_current_language

Object containing information about the current selected language.

tml_current_language  

Properties:

  • english_name - The english name of the language (e.g. "Russian")
  • native_name - The native name of the language (e.g. "Русский")
  • locale - The language locale code (e.g. "ru")
  • flag_url - flag icon image url
  • dir - The direction of the language, "ltr" or "rtl" (right-to-left)


tml_default_language

Object containing information about the default source language.

tml_default_language  


Adds a stylesheet based on the language direction.

tml_link(ltr, rtl, attrs)  

Example:

<%= tml_link('default_ltr.css', 'default_rtl.css') %>  


tml_begin_source

Defines the start of a source block. All translations within the source block will automatically be grouped and cached as a single source.

tml_begin_source(name)  

Example:

<% tml_begin_source('navigation') %>  
  <ul class="nav navbar-nav">
    <li><a href="..."><%= tr('Home') %></a></li>
    <li><a href="..."><%= tr('About Us') %></a></li>
    <li><a href="..."><%= tr('Search') %></a></li>
  </ul>
<% tml_end_source() %>  
Name Type Required Description
name String true The name of the source


tml_end_source()

Defines the end of a source block.

tml_end_source()  


tml_begin_block()

Define a section of your site to be available in one locale and not be translated to any other language.

tml_begin_block(options)  

Example:

<% tml_begin_block({source: "my source 2", locale: "ru", target_locale: "fr"}) %>  
   <div>
     <%= tr("Hello world") %>>
   </div>
<% tml_end_block() ?>  

Available Options:

Name Type Required Description
source String false An optional source name for the block
locale String false locale of the block, it can be sometimes different from default locale
target_locale String false used to set the block to a specific local which will not depend on the default locale