Java Configuration

TML library comes with many customization options that can be adjusted based on your application needs.

Basics

Create a new tml listener file in your project with the following configuration:

Tml.getConfig().setApplication(Utils.buildMap(  
   "key", YOUR_APPLICATION_KEY
));
Name Type Required Description
key String True Your Project's Application Key
token String False Your Project's Token. This token is only necessary if your application does not have a user interface where translators can provide translations inline, like in the case of an API service.

Logging

If you would like to see what the SDK is doing behind the scene, you can enable Tml logging. To minimize configuration efforts for developers, the SDK provides Logger based on LoggerFactory class, an utility class that produced Loggers for various logging APIs, such as log4j, logback, JDK 1.4 logging and etc.

For example, in order to configure log4j Logger to stream log into system output just create the file called src/main/resources/log4j.properties with the following content:

log4j.rootLogger=INFO  
log4j.logger.com.translationexchange.core.Logger=DEBUG, STDOUT  
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender  
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout  
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n  

Default Data Tokens

Sometimes you may need to use the same data token values across your entire application. For instance, a name of the company is not translatable and can be used in multiple places:

<tml:tr label="2016 Copyright {company}" company="Translation Exchange, Inc."/>  

Instead of providing the same token value in every place you refer to the company, you can configure it as the default data token:

Tml.getConfig().addDefaultTokenValue("company", "data", "html", "hello world");  

Now you can use the data token without having to specify its value:

Tml.translate("2016 Copyright {company}");

// or in jsp
<tml:tr label="2016 Copyright {company}"/>  

If you need to overwrite the value for a specific case, you can still do it using the tokens hash:

Tml.translate("2016 Copyright {company}", Utils.buildMap("company", "Google, Inc."));

// or in jsp
<tml:tr label="2016 Copyright {company}" company="Google, Inc."/>  

Default Decoration Tokens

Similarly to the data tokens, decoration tokens can also have default implementations. For instance, in the following example, bold has been preconfigured to use the strong HTML tag:

Tml.translate("Welcome to [bold: my website]");

// or in jsp
<tml:tr label="Welcome to [bold: my website]" />  

where bold has been pre-configured as:

Tml.getConfig().addDefaultTokenValue("bold", "decoration", "html", "<strong>{$0}</strong>");  


In the above example, the $0 value will be replaced with the translation of 'my website'.

Another example of a more complex decoration token is a link. Consider the following example:

Tml.translate("Welcome to [link: my website]", Utils.buildMap("link", "www.google.com"));

// or in jsp
<tml:tr label="Welcome to [link: my website]" link="www.google.com" />  

where link has been pre-configured as:

Tml.getConfig().addDefaultTokenValue("link", "decoration", "html", "<a href='{$href}'>{$0}</a>");  

If the value of the decoration token is passed as a hash, the hash attributes will be mapped against the named attributes in the token substitution string.

Here is another example of a custom decoration token with pre-configured value:

Tml.getConfig().addDefaultTokenValue("colorize", "decoration", "html", "<span style='color: {$color}'>{$0}</span>");  

and use it as:

Tml.translate("Welcome to [colorize: my website]",  
              Utils.buildMap("colorize", Utils.buildMap("color", "red")));

// or in jsp
<tml:tr label="Welcome to [colorize: my website]" colorize="{color: 'red'}" />  

The following decoration tokens come pre-configured with the SDK, but you can easily change them to your liking.

{
      "strong":   "<strong>{$0}</strong>",
      "bold":     "<strong>{$0}</strong>",
      "b":        "<strong>{$0}</strong>",
      "em":       "<em>{$0}</em>",
      "italic":   "<i>{$0}</i>",
      "i":        "<i>{$0}</i>",
      "link":     "<a href='{$href}'>{$0}</a>",
      "br":       "<br>{$0}",
      "strike":   "<strike>{$0}</strike>",
      "div":      "<div id='{$id}' class='{$class}' style='{$style}'>{$0}</div>",
      "span":     "<span id='{$id}' class='{$class}' style='{$style}'>{$0}</span>",
      "h1":       "<h1>{$0}</h1>",
      "h2":       "<h2>{$0}</h2>",
      "h3":       "<h3>{$0}</h3>"
}

Context Data Extraction

TML library has a built in rules engine that is used to evaluate language rules and provide optimal translations.

For instance, if you have a user object defined as following:

User michael = new User("Michael", "male");  

And further in the application you defined translation of a form:

Tml.translate("{user} updated {user | his, her} profile",  
              Utils.buildMap("user", michael));

// or in jsp
<tml:tr label="{user} updated {user | his, her} profile" token.user="${michael}" />  

The rules engine will automatically extract the gender of the user from the gender method and use its value to determine which translation to use.