Getting Started with PHP and TML

In this post we will create a simple PHP application and translate it using TML library and Translation Exchange service. For the sake of simplicity, we will use plain PHP without any framework.

Our site will be a one pager, called “Welp”. It will feature a few restaurant reviews and a search form. Some of the content will be based on numeric and gender based context rules.

laravel_welp_english

Once you follow this guide, you will see how you can use Translation Exchange tools to quickly and easily translate the app to any number of languages.

laravel_language_selector

You can activate translation mode by clicking on “Help Us Translate” link.

laravel_inline_mode

All of the source code for this post can be found at:

https://github.com/translationexchange/blog/tree/master/getting-started-with-php-and-tml/welp

Let’s start by creating a folder where our application will reside.

[shell]
$ mkdir welp
$ cd welp
[/shell]

All our PHP will reside in the public_html folder. Let’s create a folder and add a index page with “Hello World” message:

[shell]
$ mkdir public_html
$ edit public_html/index.php
[/shell]

[php title=”public_html/index.php”]

[/php]

We can now start the server to see our “Hello World” message:

[shell]
$ php -S localhost:8000 -t public_html
$ open “http://localhost:8000″
[/shell]

Let’s setup our dependencies using composer. Create the composer.json file and add the following information:

[js title=”composer.json”]
{
“name”: “samples/welp”,
“description”: “Sample application”,
“keywords”: [“sample”, “tml”, “welp”],
“license”: “MIT”,
“type”: “project”,
“require”: {
“php”: “>=5.3.0”,
“translationexchange/tml”: “dev-master”
}
}
[/js]

Install the dependencies using:

[shell]
$ composer install
[/shell]

TML library will be placed under the vendor folder. Now we can create our application and use the library for translation. We will break our application into 4 files: includes/_header.php, includes/_footer.php, includes/_navigation.php and index.php.

Let’s first add the header and footer files under the public_html/includes folder.

[php title=”includes/_header.php” class=”lang:php” mark=”2,3-10,23″]
“YOUR_APPLICATION_KEY”,
“token” => “YOUR_APPLICATION_TOKEN”,
“cache” => array(
“enabled” => true,
“adapter” => “memcache”,
“host” => “localhost”,
“port” => 11211
)
));
?>




Welp





[/php]

Let’s look at some of the above code. On line 2, we included the TML init file, which contains all the necessary helper function that we will be using to translate our pages.
On lines 3-10, we initialize the TML library. Please visit your dashboard to obtain your application token.
And finally, on line 23, we added client side tools that provide an in-context translation tools and widgets for translators.

[php title=”includes/_footer.php” class=”lang:php” mark=”6″]




[/php]

The only element to note in the footer is on line 6, which ensures that any new page content is sent to Translation Exchange service.

The remaining content of _includes/navigation.php and index.php contains TML syntax for actual translations of the application.

[php title=”includes/_navigation.php” class=”lang:php”]


[/php]

To learn more about TML syntax, please visit our documentation at:
http://welcome.translationexchange.com/docs/tml/basics

You can find all of the source code for this post at the following url:
https://github.com/translationexchange/blog/tree/master/getting-started-with-php-and-tml/welp

Get Started Today!

Create an account to get started now! No credit card required.

Get Started