zf2 transalator打印msgid未翻译的文本 [英] zf2 transalator print msgid not translated text
问题描述
我创建了文件i_IT.mo和it_IT.po,并将其放置在文件夹中
I created the file it_IT.mo and it_IT.po that I placed in the folder
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
operation_allowed_false is the msgid of the file
打印密钥而不是翻译的文本
print the key and not the translated text
这是我的文件
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."
推荐答案
在 Module.php 中,您使用错误的模式注册了gettext translationFilePattern.模式字符串必须包含一个%s",该字符串将被语言环境字符串替换.
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.
示例:
$translator->addTranslationFilePattern('gettext',"module/Application/language/mydomain","%s.mo");
因此,如果您的语言环境是it_IT,那么翻译器将加载文件 module/Application/language/mydomain/it_IT.mo
So if your locale is it_IT, the translator will load the file module/Application/language/mydomain/it_IT.mo
您要在视图中从域 ha (第二个参数)进行翻译:
In your view you want to translate from domain ha (second param):
<?php echo $this->translate("operation_allowed_false","ha","it_IT"); die();?>
但是您没有在文本域中注册translationFilePattern.将此参数设置为null
以使用默认域:
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();?>
我还建议将翻译器配置移至您的 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
这篇关于zf2 transalator打印msgid未翻译的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!