PHP

This guide provides a step by step tutorial on how to integrate the Translation Exchange SDK with your PHP based web applications and allow you to translate your site into multiple languages.

Before Getting Started

Make sure you have a Translation Exchange account, and have created a project in your dashboard.

1. Install the SDK

The PHP SDK can be installed using the composer dependency manager. If you don't already have composer installed on your system, you can get it using the following command:

$ cd YOUR_APPLICATION_FOLDER
$ curl -s http://getcomposer.org/installer | php

Create composer.json in the root folder of your application, and add the following library under the require section:

{
  "require": {
    "translationexchange/tml": "3.2.12"
  }
}

Now install the library by executing the following command:

$ composer install

Composer will automatically create a vendor folder and put the SDK into vendor/translationexchange/tml directory.

2. Configure the SDK

Include the main Tml script in the header of your app:

<?php require_once(__DIR__ . '/vendor/translationexchange/tml/src/init.php'); ?>  

Call the initialization function with the key of your application:

<?php tml_init(array(  
    "key" => "YOUR_APPLICATION_KEY"
)); ?>

For the best performance, translations should be cached in your application. Multiple cache adapters are supported.

To learn more about configuring the SDK, go to the PHP Configuration section of the docs.

3. Add the Translation Exchange Script

Copy and paste the following code between the <head> and </head> tags of your php files.

<?php tml_scripts(); ?>  

In order for the SDK to know when to send new strings to the service it must know when the request is completed. Add the following line as the last statement in your application script:

<?php tml_complete_request(); ?>  

Full Page Example

<?php require_once(__DIR__ . '/vendor/translationexchange/tml/src/init.php'); ?>

<?php tml_init(array(  
    "key" => "YOUR_APPLICATION_KEY"
)); ?>

<!doctype html>  
<html lang="<?php echo tml_current_locale(); ?>">  
  <head>
    <?php tml_scripts(); ?>
  </head>
  <body>
    <?php tre("Hello World") ?>
  </body>
</html>

<?php tml_complete_request() ?>  

4. Add a Language Selector

Use the following code anywhere on your page to add a language selector. Optionally, you can pass the type of selector you want.

<?php tml_language_selector_tag("default"); ?>  

There are a few language selectors to choose from. You can view all the available selectors or learn how to create your own in the Language Selectors section of the docs.

5. Mark Up Your Content

<?php tre("Hello World") ?>  
# Hello World

<?php tre("You have {count || message}", array("count" => 5)) ?>  
# You have 5 messages

<?php tre("{user} sent {target} <bold>{count || gift}</bold>", array("count" => 5, "user" => current_user, "target" => target_user)) ?>  
# Michael sent Anna 5 gifts

<?php tre("{user} uploaded [bold: {count || photo}] to {actor | his, her} album.", array("count" => 1, "user" => current_user) ?>  
# Michael uploaded 1 photo to his album.

Learn more about marking up your content in the Translation Markup Language section of the docs

6. Activate Translation Mode

Once the script has been added to your page you can start using translation mode. To activate translation mode, press CTRL+Shift+i on your keyboard.

Once translation mode has been activated, the content of your application will be synchronized with Translation Exchange and your strings will appear in your Dashboard.

Make sure to browse your entire application while in translation mode to ensure that all your content gets sent to the Translation Exchange service.

Once your strings have been synchronized, you can start translating. Go to the Translation Options section of the docs to learn more about the different ways your content can be translated.

Make sure to Publish your translations so that they can be seen by your users.