In zend framework 2 (ZF2), php arrays are the main method of configuring the application. Sometimes, there are strings in there which must be translated to other languages. However, the translator is not available yet at the time the config is merged, since it has to be configured first.
ZF2 offers the Zend\Config\Processor\Translator to do the job. However, there are some problems with that:
- You probably don't want to translate everything in your config, only specific strings. Running all config values through the translator may have unintended side-effects.
- Caching serveral merged configurations in different languages is another problem for this solution.
- Code scanning (e.g. with Poedit) does not work.
Here's how you can deal with all these problems: simply use an empty translator (no translations specified) in your config file. Here I translate the navigation of my application
$translator = new \Zend\I18n\Translator\Translator; return array( 'navigation' => array( 'default' => array( 'admin' => array( 'label' => $translator->translate('Control Panel'), 'controller' => 'admin', 'action' => 'index', 'route' => 'survey/default', ), ), ), );
Poedit will recognize the translate('') function if set up correctly and you'll be able to edit the translation files. When displaying the "translated" string, make sure to pass it through the real translator with loaded translations (gettext files from Poedit). Here I display the translated
'Control Panel' string in my breadcrumb partial viewscript:
$label = $this->translate($page->label); echo (sprintf('<li>%s</li>', $label));