Introducing the Google I18n Extension

Posted over 2 years ago by jwage

To demonstrate the use of the new Doctrine extensions repository I have written an extension of the core provided I18n behavior and created the GoogleI18n extension. This behavior offers automatic translation of your internationalized records by using the Google Translation API.

Here are some basic examples where we enable the behavior.

<?php

class GoogleTranslateArticle extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('name', 'string', 255);
        $this->hasColumn('title', 'string', 255);
        $this->hasColumn('description', 'string', 255);
    }

    public function setUp()
    {
        $this->actAs('Sluggable', array('fields' => array('name')));
        $this->actAs('GoogleI18n', array(
            'languages' => array(
                'en', 'fr', 'es', 'ja', 'it'),
            'fields' => array(
                'title','description'
            )
        ));
    }
}

Now here is what it looks like when we use it.

<?php

$article = new GoogleTranslateArticle();
$article->name = 'Testing this out';
$article->Translation['en']->title = 'Hello, how are you?';
$article->Translation['en']->description = 'Good evening';
$article->save();

print_r($article->toArray());

The above code would output the following.

Array
(
    [id] => 1
    [name] => Testing this out
    [slug] => testing-this-out
    [Translation] => Array
        (
            [en] => Array
                (
                    [id] => 1
                    [title] => Hello, how are you?
                    [description] => Good evening
                    [lang] => en
                )

            [fr] => Array
                (
                    [id] => 1
                    [title] => Bonjour, comment allez-vous?
                    [description] => Bonsoir
                    [lang] => fr
                )

            [es] => Array
                (
                    [id] => 1
                    [title] => Hola, cómo estás?
                    [description] => Buenas tardes
                    [lang] => es
                )

            [ja] => Array
                (
                    [id] => 1
                    [title] => こんにちは、お元気ですか?
                    [description] => こんばんは
                    [lang] => ja
                )

            [it] => Array
                (
                    [id] => 1
                    [title] => Ciao, come stai?
                    [description] => Buona sera
                    [lang] => it
                )

        )

)

You'll notice that my original text was automatically translated to all the other languages I specified in the setUp of the Article model. You can read more about the GoogleI18n behavior here. Each extension can provide its own documentation that will be published on the extensions page.


Comments (3) [ add comment ]

Testability Posted by Giorgio Sironi about over 2 years ago.

This is nice but I'm scared from a testability point of view; it seems that running unit tests on the Article "bean" requires to send queries to google. How to mock out the google translate server?

thx Posted by Amo about over 2 years ago.

Thx jonathan for this, now i can delete my Translation Class :)

help Posted by Ken about 10 months ago.

how to translate into vietnamese. I added 'languages' => array( 'en', 'fr', 'es', 'ja', 'it', 'vn') but it doesn't work.

Create Comment