How to use zend i18n module in mix php/other framework?

474 Views Asked by At

I want to use ZF2 i18n module in my custom php project. My composer.json file as bellow

{
    "require": {
        "zendframework/zend-i18n": "2.3.*@dev"
    }
}

My index.php file is

include_once './vendor/autoload.php';

use Zend\I18n\Translator\Translator;

$translator = new Translator();
$translator->setLocale('fr_FR');
echo $translator->translate('All rights reserved.');

My question how to set base dir for language where I store en_US.po, fr_FR.po file for translation and when I set $translator->setLocale('fr_FR'); then french version of 'All rights reserved.' text show.

1

There are 1 best solutions below

2
lluisaznar On

You need to add a pattern to the translator in order to get it working (you've got two languages supported). Using a pattern allows you to add more locales without changing your code. Use the addTranslationPattern function. The function definition is:

//$type -> format loader
//$baseDir -> the dir where you place your locale files
//$pattern -> sprintf expression (the locale is parsed through it)
//$textDomain -> category specified for the file. By default is 'default'
public function addTranslationFilePattern($type, $baseDir, $pattern, $textDomain = 'default')

Use it like:

$translator = new Translator();
$translator->addTranslationFilePattern('gettext', '<your_base_dir>', '%s.mo');

Check the reference for more information.