PHP Integration

The PHP SDK offers methods that can be used.

tr

The basic translation helper.

tr($label, $description = "", $tokens = array(), $options = array())

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

Some examples of how the method can be used:

<?php echo 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 Array false If label uses data or decoration tokens, the token values will be provided using this parameter
options Array 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


tre

Shorthand notation for echoing the results of tr

<?php tre("Hello World"); ?>  
// is equivalant to 
<?php echo tr("Hello World"); ?>  


trl

Translating labels. trl is the same as the tr function but sets the option to never use inline translation for the elements:

trl($label, $description = "", $tokens = array(), $options = array())

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

Example:

<img alt="<?php echo trl('Hello World'); ?>" src="...">  

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

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 Array false If label uses data or decoration tokens, the token values will be provided using this parameter
options Array false Additional attributes. Attributes include inline decoration flag, source locale and others.


trle

Shorthand notation for echoing the results of trl

<img alt="<?php trle('Hello World'); ?>" src="...">  
// is equivalant to 
<img alt="<?php echo trl('Hello World'); ?>" src="...">  


trh

Translate HTML Block helper

trh($label, $description = "", $tokens = array(), $options = array())

# The description parameter can be omitted
trh($label, $tokens = array(), $options = array())  

Example:

<?php echo trh("<p>your html block goes here</p>"); ?>  
Name Type Required Description
label String true The html 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 Array false If label uses data or decoration tokens, the token values will be provided using this parameter
options Array false Additional attributes. Attributes include inline decoration flag, source locale and others.


trhe

Shorthand notation for echoing the results of trh

<?php trhe("<p>your html block goes here</p>"); ?>  
// is equivalant to 
<?php echo trh("<p>your html block goes here</p>"); ?>  


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.

<?php tml_scripts(); ?>  

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


tml_complete_request

In order for the SDK to know when to send new strings to the service it must know when the request is completed. tml_complete_request should be the last statement in your application.

<?php tml_complete_request(); ?>  


tml_language_selector_tag

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_tag($style, $opts = array())  

Example:

<?php tml_language_selector_tag("default"); ?>  
Name Type Required Description
style String false The name of the language selector. Defaults to `:default`
options Array false Additional options specific to the chosen language selector

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


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:

<?php tml_begin_source('navigation') ?%>  
  <ul class="nav navbar-nav">
    <li><%= link_to(tr('Home'), '#') %></li>
    <li><%= link_to(tr('About Us'), '#') %></li>
    <li><%= link_to(tr('Search'), '#') %></li>
  </ul>
<?php tml_finish_source() ?%>  
Name Type Required Description
name String true The name of the source


tml_finish_source

Defines the end of a source block.

tml_finish_source()  


tml_begin_block_with_option

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

tml_begin_block_with_option($options = array()  

Example:

<?php tml_begin_block_with_option(array("source" => "my source 2", "locale" => "ru", "target_locale" => "fr")); ?>  
   <div>
     <?php tre("Hello world"); ?>
   </div>
<?php tml_finish_block_with_option(); ?>  

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


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 -
  • sources -


tml_current_locale

A string of the current selected language locale.

tml_current_locale()  
# "ru" 


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_current_language_direction

A string of the current language direction, "ltr" (left-to-right) or "rtl" (right-to-left)

tml_current_language_direction()  
# "ltr"