Laravel Quick Start

This guide will show you how to integrate our Tml PHP library with a Laravel application.

Before Getting Started

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

1. Install the SDK

To create a new Laravel project, run the following command:

$ laravel new YOUR_APPLICATION_NAME
$ cd YOUR_APPLICATION_NAME

Add TML library reference by editing composer.json file in the root of your new project:

"require": {
  ...
  "translationexchange/tml": "*"
}

To install the library, run:

$ composer update

The TML helper methods need to be available globally in all controllers and views. To do that, we can require the TML initialization script in the application bootstrap. Open bootstrap/app.php and add the following line at the top of the file:

require __DIR__.'/../vendor/translationexchange/tml/src/init.php';  

2. Configure the SDK

Now that the library is included we can create our middleware where we will initialize the application. Add a new Tml.php file under app/Http/Middleware/Tml.php and paste the following code:

<?php  
namespace App\Http\Middleware;  
use Closure;

class Tml {  
  public function handle($request, Closure $next) {
    tml_init(array(
      "key"   => "YOUR_APPLICATION_KEY"
    ));
    return $next($request);
  }

  public function terminate($request, $response) {
    tml_complete_request();
  }
}

Let’s create a sample controllerapp/Http/Controllers/SampleController.php by using the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PageController extends Controller {  
  public function index() {
    return view('index');
  }
}

3. Add the Translation Exchange Script

We will use Blade templates and create a separate file for layout. Copy and paste the following code between the <head> and </head> tags of your

<?php tml_scripts() ?>  
<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
  <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="chrome=1">
  <link rel="stylesheet" href="{{ asset('stylesheets/master.css') }}">
  <?php tml_scripts() ?>
</head>  
<body>  
  @yield('content')
</body>  
</html>  

To add a language selector, use the following code:

 {!! tml_language_selector_tag("default", ["element" => "li"]) !!}

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.

4. Mark Up Your Content

Now we can use TML inside of our resources/views/index.blade.php view:

{!! tr("About Us") !!}
{!! tr("You have {count || message}", ["count" => 1])  !!}
...

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

To read a full walkthrough on how to use the Tml PHP library with Laravel, visit our blog post: http://blog.translationexchange.com/2015/06/22/getting-started-with-php-laravel-and-tml/

5. Activate Translation Mode

Once the script has been added to your layout 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.

Now that 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.