Using DooTranslator for translation


DooTranslator finnaly came out, it supports “Csv“, “Gettext“, “Array“, “Ini” for now, soon it will be implemented other “file types”. So for example you have your translation file with this contents:

en.svn

1.
test;”This is just a test!”
Lets initialize DooTranslator class:

1.
$translator = Doo::translator(‘Csv’, $this->_basePath . ‘languages/en/LC_MESSAGES/en.csv’);
2.
echo $translator->translate(“test”);
As you can see its very simple, first argument is file type for translation and second is path to the file, third argument can be options array. For Csv you can set options like: delimiter, enclosure and length. Default value for delimiter is “;” and for enclosure is “.

Now we will add some options to our call, lets say we want some other delimiter then “;” and we want to add some caching.

1.
$translator = Doo::translator(‘Csv’, $this->_basePath . ‘languages/en/LC_MESSAGES/main.csv’, array(‘cache’ => ‘apc’, ‘delimiter’ => ‘|’));
So now we have added cache mechanism that is “apc” and we added delimiter that is “|”, now in our en.csv we must change delimiter:

1.
test|”This is just a test!”
Supported cache mechanisms are “apc”, “php”, “xcache” and “eaccelerator”.

Now lets add placeholders for translate method:

1.
$translator->translate(“Hello [_1], wellcome to my website!”, array(“John”));
This will show translated string with: “Hello John, wellcome to my website!”.

Ok thats about it, if you have any questions please do ask, this is simple translation class its still

Previous PostNextNext Post

Leave a Reply

Your email address will not be published.