【问题标题】:ZF translation formsZF 翻译表格
【发布时间】:2013-09-05 17:22:50
【问题描述】:

我正在使用 ZF2,并且正在使用翻译器。 在我的应用程序的配置中,我已将翻译器添加到 service_manager 工厂,因此 ZF2 助手将使用它。

这是我的翻译器配置的样子:

'translator' => array(
    'locale' => 'nl_NL',
    'translation_file_patterns' => array(
        array(
            'type' => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern' => '%s.mo',
        ),
    ),
),

在我的应用程序的 module.php 文件中,我的 onBootstrap 方法中有以下代码:

/**
 * Magic to determine the right locale to be used.
 * 
 * Check cookie
 * Check GET parameter
 * Check HOST (application can be used from several domain names)
 */ 

$translator = $serviceManager->get('translator');

// We have to change the current locale if $locale is not null.
if (!is_null($locale)) {          
    $translator->setLocale($locale);

    // The translate helper of the view has some problems with what we're doing.
    // So we'll manually add the translator with the correct locale to the view helper.
    $serviceManager->get('viewhelpermanager')->get('translate')->setTranslator($translator);
}

如您所见,由于 onBootstrap 方法中的语言环境修改,我已经遇到了一些问题。

现在有两件事可以帮助我: - 帮助我找到一种方法将正确的翻译器重新注入到表单助手中; - 帮助我找到 ZF2 喜欢或应该这样做的方法(我的搜索没有找到解决方案)。

希望大家能帮帮我!

【问题讨论】:

    标签: php zend-framework2 zend-form zend-form-element zend-translate


    【解决方案1】:

    表单助手的工作方式应该相同。

    $serviceManager->get('ViewHelperManager')->get('form')->setTranslator($translator);
    

    编辑

    并使用MvcTranslator 服务而不是translator

    if (!is_null($locale)) {
        $translator = $serviceManager->get('MvcTranslator');
        $translator->setLocale($locale);
        // ...
    }
    

    如果你这样做,你甚至不需要setTranslator() 调用。

    【讨论】:

    • 您应该尝试使用MvcTranslator 服务而不是translator
    • 如果我这样做:$serviceManager->get('MvcTranslator')->setLocale($locale); $serviceManager->get('translator')->setLocale($locale);然后它起作用了……这是你的意思吗?因为现在我必须做 $serviceManager->get('MvcTranslator')->setCache($cache); $serviceManager->get('translator')->setCache() 也是如此。
    • 根本不要使用translator 服务。仅使用MvcTranslator。我将完整代码添加到原始答案中。
    • 不幸的是,这并不能解决问题.. if (!is_null($locale)) { $translator = $serviceManager->get('MvcTranslator'); $translator->setLocale($locale); } 仍然给出一个未翻译的形式。
    【解决方案2】:

    使用MvcTranslator 代替translator

    $translator = $serviceManager->get('MvcTranslator');
    $translator->setLocale($locale);
    $serviceManager->get('ViewHelperManager')
                   ->get('translate')->setTranslator($translator);
    

    【讨论】:

      猜你喜欢
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多