使用Poedit

时间:2018-02-14 14:23:49

标签: forms zend-framework3 poedit

Zend 3会自动翻译表单标签。

  1. 如果使用数组规范创建表单,如何使用Poedit扫描可翻译的表单元素字符串?
  2. 如何将translator-> translate()功能添加到表单中?我在module.php onBootstrap方法中尝试了以下操作,但这不起作用:

    $sm = $e->getApplication()->getServiceManager(); 
    $vhm = $sm->get('ViewHelperManager'); 
    $translator = $sm->get('MvcTranslator');  
    $vhm->get('form')->setTranslator($translator);
    
  3. 我想像$ form-> translator-> translate()一样使用它,这样就可以用Poedit扫描代码,找到可翻译的labeles,占位符等。

2 个答案:

答案 0 :(得分:0)

如果您需要

,这里是TranslatorFactory
final class TranslatorFactory implements FactoryInterface
{
     public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
     {
           // get translator files' paths from config
           $paths      = $container->get('config')['settings']['localization-paths'] ?? [];
           $translator = new Translator();

           // add zend-i18n-resources files to translator
           $translator->addTranslationFilePattern(
              'phpArray',
               Resources::getBasePath(),
               Resources::getPatternForValidator()
          );

          // add your translation files to translator
          foreach ($paths as $path) {
               $translator->addTranslationFilePattern('phpArray', $path, '%s.php');
          }

          // todo: replace with user's preferred language
          $translator->setLocale('tr');

          return $translator;
      }
}

将您的工厂添加到服务经理

'service_manager' => [
    'factories' => [
        \Zend\I18n\Translator\TranslatorInterface::class => \MyModule\Factory\TranslatorFactory::class,
    ],
],

答案 1 :(得分:0)

不确定您是否仍在寻找解决方案,因此我会添加我的解决方案。

我在TranslatorAwareTrait课程中使用AbstractForm

use Zend\I18n\Translator\TranslatorAwareTrait;

abstract class AbstractForm extends \Zend\Form\Form implements
{
    use TranslatorAwareTrait;

    // Form stuff
}

然后,在*FormFactory执行以下操作:

use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\Factory\FactoryInterface;

class SomeFormFactory implements FactoryInterface
{
    /**
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param array|null $options
     * @return mixed|object|AbstractForm
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        // Obviously you'll have more/other requirements. Fulfil them here.
        $form = new SomeForm();
        $form->setTranslator(
            $container->get('translator')
        );

        return $form;
    }
}

用法示例:

use Zend\I18n\Translator\TranslatorAwareTrait;

abstract class AbstractForm extends \Zend\Form\Form implements 
{
    use TranslatorAwareTrait;

    public function init()
    {
        if (!$this->has('submit')) {
            $this->addSubmitButton();
        }
    }

    public function addSubmitButton($value = 'Save', array $classes = null)
    {
        $this->add([
            'name'       => 'submit',
            'type'       => Submit::class,
            'attributes' => [
                'value' => 
                    // Translate $value before passing to this function
                    $value === 'Save' ? $this->getTranslator()->translate('Save') : $value, 
                'class' => (!is_null($classes) ? join (' ', $classes) : 'btn btn-primary'),
            ],
        ]);
    }
}

另一方面,你可以......

如果您正在使用Poedit进行翻译,请在传递字符串之前进行翻译。

如果您的模块包含以下配置(在每个模块中!):

'translator' => [
    'translation_file_patterns' => [
        [
            'type' => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern' => '%s.mo',
        ],
    ],
],

您可以在此处看到使用gettext完成翻译。这是一个PHP模块,用于搜索以下代码字符串并翻译其内容:_('translatable string')

要查找的翻译文件以.mo扩展名结尾,可以在__DIR__ . '/../language'中找到。

因此,如果您确保启用了PHP gettext模块来使用它。

要将其与普通字符串一起使用,即使在配置Fieldset或表单时,您也可以使用以下字符串:

$this->add([
    'name' => 'street',
    'required' => true,
    'type' => Text::class,
    'options' => [
        'label' => _('Street'), // <<-- translated using gettext
    ],
]);
相关问题