Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Zend\I18n\Translator\Translator – Translate your Web Application

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on July 18, 2012

Zend Framework 2 comes with i18n Component which one of the features is translator. It can translate your content of the page that you register in file(s) as dictionary and called with this component. The type of format that can be handled by this components are :

  • PHP arrays
  • Gettext
  • Tmx
  • Xliff

On this time, i want to expose a PHP arrays type for this components. For example, i want to translate Indonesian to English and English to Indonesian. Firstly, add the following files like this :
This is the example of the content of these files :
For English dictionary :

     return array('beranda' => 'home');
     //filename : lang.array.en.php

For Indonesian dictionary :

     return array('congratulation' => 'selamat');
     //filename : lang.array.id.php

In that case, we will dinamically change the web content via URL. so add the ‘lang’ param in route like this :

'route'  => '/[:lang]/test',
             'defaults' => array(
                  '__NAMESPACE__' => 'Test\Controller',
                  'controller'    => 'Test',
                  'action'        => 'index',
                  'lang'          => 'en'

that we can call like this : http://localhost/zf2app/public/en/test or http://localhost/zf2app/public/id/test.
Setup Complete. Now, we add the following code in controller :

$lang = $this->getEvent()->getRouteMatch()->getParam('lang','en');
if (strtolower($lang) != 'en' && strtolower($lang) != 'id')
    $lang = 'en';

$loc = $this->getServiceLocator();
$translator = $loc->get('translator');


And in View, we test this :

    echo $this->translate('congratulation');
    echo "<br />";
    echo $this->translate('beranda');

Done !

Reference :
1. http://zend-framework-2-doc.readthedocs.org/en/latest/modules/zend.i18n.view.helpers.html#translate-helper
2. http://zend-framework-2-doc.readthedocs.org/en/latest/modules/zend.i18n.translating.html

Tagged with:

18 Responses

Subscribe to comments with RSS.

  1. […] ZendI18nTranslatorTranslator – Translate your Web Application […]

  2. Jeroen said, on April 5, 2013 at 2:58 pm

    Is it possible to have a general languages folder in the root dir and that all the modules use that directory instead of the module specific language folder?

  3. Daniel Salvagni said, on August 1, 2013 at 6:34 pm

    But in this way I’ll need to specify the “lang” param in the $this->url() method, right? There’s other way to set the “lang” param at the onbootstrap method, without pass this in the $this->url() method?

    • samsonasik said, on August 1, 2013 at 8:06 pm

      you can set default value of segment on segment route

      • Daniel Salvagni said, on August 1, 2013 at 8:30 pm

        Right, thanks for your answer. But what I’m trying to do is this:

        there’s a default :lang param in the module.config, but when I call i.e: “localhost/[:lang]/company” with the :lang set as “en”, the param will not remain in the others urls, such as “localhost/[:lang]/about”, for example. In this last example, the url will be only “localhost/about”, without the param defined in the first called url. I must pass the param in the controller and then set it in $this->url() method?

        This is my doubt. I’m trying a solution that I’ll not need to change all the url methods. You know some way to do that?

      • Daniel Salvagni said, on August 1, 2013 at 11:10 pm

        Sorry man. I just needed to set to true the parameter $reuseMatchedParams on the method $this->url().
        Into the navigation array, I need to set ‘use_route_match’ as true, to keep the parameters on the menu url as well.

        Sorry if I’m spamming here. I’m a begginer with ZF2 and your post was very helpful.

        Ref: http://framework.zend.com/manual/2.0/en/modules/zend.view.helpers.html

      • samsonasik said, on August 2, 2013 at 3:23 am

        No problem 😉

  4. cany said, on June 19, 2014 at 2:15 pm

    Warning: Zend\I18n\Translator\Loader\Gettext::load(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone ‘UTC’ for now, but please set date.timezone to select your timezone. in C:\Program Files\Zend\ZendServer\data\libraries\Zend_Framework_2\2.3.1\library\Zend\I18n\Translator\Loader\Gettext.php on line 144

    Notice: Undefined offset: 1 in C:\Program Files\Zend\ZendServer\data\libraries\Zend_Framework_2\2.3.1\library\Zend\I18n\Translator\Loader\Gettext.php on line 144
    what this means how to solve it

    • samsonasik said, on June 21, 2014 at 4:27 am

      based on the error, you need to set timezone, please use google.

      • cany said, on June 21, 2014 at 3:26 pm

        i have done this in php.ini but it keeps disappearing at end of day. permanently how to resolve.

  5. Kumar said, on July 3, 2014 at 2:33 pm

    Excellent Man. Great salute to U.God bless U

  6. Daniel said, on October 2, 2014 at 6:35 pm

    So good!!! My problem was that i didn’t attach the view model with the translator.
    Five stars!!

  7. Pardus said, on September 20, 2016 at 4:11 am

    Hello I’m new in ZF and trying this on ZF3 Skeleton but got this error:

    A plugin by the name “getServiceLocator” was not found in the plugin manager Zend\Mvc\Controller\PluginManager

    I also searched the whole internet for a working tutorial but find nothing.
    Can you help me out please.

  8. Amine said, on July 15, 2017 at 10:54 pm

    How to translate from database

    • samsonasik said, on July 16, 2017 at 2:21 pm

      you can write custom loader for that, google hint: “zf2 database translate”

  9. Abin kuruvilla chacko said, on December 12, 2018 at 1:39 pm

    thank you so much sir your code updates helped me a lot in different situations.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: