zf2 transalator打印msgid未翻译的文本

时间:2014-09-17 11:37:55

标签: php zend-framework2 translate po

我创建了放在文件夹

中的文件it_IT.mo和it_IT.po
module/application/language/mydomain/ 

在文件 module.php 我输入了

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);
}

在我的视图

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

operation_allowed_false是文件的msgid

打印密钥而不是翻译文本

这是我的档案

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."

1 个答案:

答案 0 :(得分:1)

Module.php 中,您使用错误的模式注册gettext translationFilePattern。模式字符串必须包含&#34;%s&#34;它被区域设置字符串替换。

示例:

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

因此,如果你的语言环境是it_IT,翻译器将加载文件 module / Application / language / mydomain / it_IT.mo


视图中,您想要从域 ha (第二个参数)进行翻译:

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

但是您没有在文本域中注册translationFilePattern。将此参数设置为null以使用默认域:

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

我还建议将翻译器配置移到 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',
        ),
    ),
),

未经测试的一切