Search code examples
phpzend-framework2translatepo

zf2 transalator print msgid not translated text


I created the file it_IT.mo and it_IT.po that I placed in the folder

module/application/language/mydomain/ 

in file module.php I entered

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    $eventManager->attach('dispatch.error', array($this,'onDispatchError'), 100);

    $translator = $e->getApplication()->getServiceManager()->get('translator');
    $translator->addTranslationFile(
        'phpArray',
        'vendor/zendframework/zendframework/resources/languages/it/Zend_Validate.php',
        'default',
        'it_IT'
    );
    $translator->addTranslationFilePattern('Gettext',"module/Application/language/mydomain/","mydomain");
    AbstractValidator::setDefaultTranslator($translator);
}

in my view

<?php echo $this->translate("operation_allowed_false","ha","it_IT"); die();?>

operation_allowed_false is the msgid of the file

print the key and not the translated text

this is my file

msgid ""
msgstr ""
"Project-Id-Version: ha\n"
"POT-Creation-Date: 2014-09-17 13:09+0100\n"
"PO-Revision-Date: 2014-09-17 13:14+0100\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: it_IT\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.6.9\n"
"X-Poedit-Basepath: C:\\Users\\DEVELOPMENT\\xamp\\htdocs\\ha\\doc\\phpstring"
"\\contenuti\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-SearchPath-0: C:\\Users\\DEVELOPMENT\\xamp\\htdocs\\ha\\doc"
"\\phpstring\\contenuti\n"

#: C:\Users\DEVELOPMENT\xamp\htdocs\ha\doc\phpstring\contenuti/base.php:9
msgid "operation_allowed_false"
msgstr "Operazione non consentita."

Solution

  • In Module.php you register the gettext translationFilePattern with wrong a pattern. The pattern string must contain a "%s" which gets replaced by the locale string.

    Example:

    $translator->addTranslationFilePattern('gettext',"module/Application/language/mydomain","%s.mo");
    

    So if your locale is it_IT, the translator will load the file module/Application/language/mydomain/it_IT.mo


    In your view you want to translate from domain ha (second param):

    <?php echo $this->translate("operation_allowed_false","ha","it_IT"); die();?>
    

    But you did not register your translationFilePattern with a text domain. Set this parameter to null to use the default domain:

    <?php echo $this->translate("operation_allowed_false" null,"it_IT"); die();?>
    

    I would also recommend to move the translator configuration to your module.config.php:

    'translator' => array(
        'locale' => 'it_IT',
        'translation_files' => array(
            array(
                'type' => 'phpArray',
                'filename' => '/vendor/zendframework/zendframework/resources/languages/it/Zend_Validate.php',
                'locale' => 'it_IT',
            ),
        ),
        'translation_file_patterns' => array(
            array(
                'type' => 'gettext',
                'base_dir' => __DIR__ . '/../language/mydomain',
                'pattern' => '%s.mo',
            ),
        ),
    ),
    

    Everything untested