donate Buy us a cup of coffee
Our service is free. But your donation can help us keep running. To rent a server and domain, to buy us a cup of coffee, to reduce our activity in paid stuff and make this service better. You can send us a donation to BCA Bank, 008 090 8440 in the name of Abdul Malik Ikhsan with Swifcode CENAIDJA. Please confirm to samsonasik@gmail.com after that ;).
download Download module      



I18n example with Zend Framework 2

Messages in current language (en_US)
  • Hello
  • Learn Zend Framework with this tutorials
  • This string is fetched from a gettext file
  • Zend Framework 2 is a web framework written in PHP

Messages always in English (en_US)
  • Hello
  • Learn Zend Framework with this tutorials
  • This string is fetched from a gettext file
  • Zend Framework 2 is a web framework written in PHP

Messages always in Bahasa (id_ID)
  • Halo
  • Belajar Zend Framework melalui tutorial-tutorial ini
  • String ini diambil dari sebuah file gettext
  • Zend Framework 2 adalah sebuah web framework yang ditulis dengan PHP

Messages always in Spanish (es_ES)
  • Hola
  • Aprende Zend Framework con estos tutoriales
  • Esta cadena de texto es leĆ­da de un archivo gettext
  • Zend Framework 2 es un framework web escrito en PHP

The Zend\I18n component is used to handle translations in Zend Framework 2 applications.

It includes the Translator, which is the main service, but also a view helper that consumes it and can be used to translate texts directly inside views.

The first thing we have to do is to define a translator top level configuration block, so that the Translator service can be properly created.

[
    'translator' => [
        'locale' => 'en_US',
        'translation_file_patterns' => [
            [
                'base_dir' => __DIR__ . '/../languages/phpArray',
                'type'     => 'phpArray',
                'pattern'  => '%s.php',
            ],
            [
                'base_dir' => __DIR__ . '/../languages/gettext',
                'type'     => 'gettext',
                'pattern'  => '%s.mo',
            ],
        ],
    ],
]

The translator configuration has two elements.

The Translator service can handle multiple language files in different formats. Usually you will define a group of language files per module, and each module will have its own translator configuration. At runtime all of them will be merged and you will have something like the example, with multiple translation_file_patterns.


The service

The Zend\Mvc component registers a service with the key MvcTranslator, which is the one that consumes the translator top level configuration, but some other services will try to find a translator service.

To fix this problem we have to define a service alias like this.

[
    'service_manager' => [
        'aliases' => [
            'translator' => 'mvctranslator',
        ],
    ]
]

The Zend Skeleton Application has this preconfigured.


The view helper

You will usually want to translate messages in view templates. That can be easily done by using the translate() view helper, which internally consumes the translator service.

Inside a view partial, just call it by passing the message you want it to translate and optionally the text domain and locale.

<label><?php echo $this->translate('Hello') ?></label>
<label><?php echo $this->translate('Hello', null, 'en_US') ?></label>

And that's it, you are now ready to use Zend\I18n.