ZF2验证器消息i18n

时间:2015-03-25 16:13:15

标签: php validation localization zend-framework2

我本地化产品,我是PHP的新手,ZF2。我有.PO / .MO文件,有英文和日文翻译。如何配置ZF2以在所有library\Zend\Validator\*文件中使用这些.mo转换而不是默认消息,例如 protected $messageTemplates = array( self::NOT_GREATER => "The input is not greater than '%min%'", self::NOT_GREATER_INCLUSIVE => "The input is not greater or equal than '%min%'" );

我已经阅读了这些link,但它对我没有帮助。 我有自定义Translator,它扩展了MvcTranslator,配置如下: ```

),
'service_manager' => array(
    'aliases'   => array(
        'translator' => 'MvcTranslator',
    ),
    'factories' => array(
        // ...
        'MvcTranslator' => function ($services) {
            $config     = $services->get('config');
            $config     = isset($config['translator']) ? $config['translator'] : array();
            $translator = \Application\I18n\Translator::factory($config);

            $translator->setEscaper(new \Application\Escaper\Escaper);

            // add event listener for context fallback on missing translations
            $translator->enableEventManager();
            $translator->getEventManager()->attach(
                $translator::EVENT_MISSING_TRANSLATION,
                array($translator, 'handleMissingTranslation')
            );

            // establish default locale settings
            $translator->setLocale($translator->getLocale() ?: 'en_US');
            $translator->setFallbackLocale($translator->getFallbackLocale() ?: 'en_US');

            // try to guess locale from browser language header (using intl if available)
            if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])
                && (!isset($config['detect_locale']) || $config['detect_locale'] !== false)
            ) {
                $locale = extension_loaded('intl')
                    ? \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE'])
                    : str_replace('-', '_', current(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'])));

                $translator->setLocale(strlen($locale) ? $locale : $translator->getLocale());
            }

            return $translator;
        },
    ),
),
'translator' => array(
    'locale'                    => 'en_US',
    'detect_locale'             => true,
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo'
        ),
    ),
),
```

我以为我应该放Zend\Validator\AbstractValidator::setDefaultTranslator($translator); 在某处......但没有任何改变。我应该在验证器中写一些代码还是什么?

0 个答案:

没有答案