What is the recomended way / best practice to Poed

2019-08-03 06:20发布

问题:

I'm developing a Zend Framework 2 application an having a problem with translations. Actually in the view scripts a can use the view helper Translate. Since I defined "translate" as a source keyword in Poedit ([Poedit menu] -> Catalogue -> Properties... -> Source keywords) the strings are identified by the tool and added to the tranlation list.

But there are also some strings at other places, where I cannot use the/a view helper, e.g. in form classes or in the navigation. How should this be managed?

Some ideas:

  1. Create a file with a list of such strings. Example: We create files navigation.i18n, forms.i18n etc. (or just one file), define there all strings we need in the common Poedit syntax we added to the Source keywords list (e.g. with translate: translate('my label foo'), translate('my label bar') etc.), and finally add i18n as a source path ([Poedit menu] -> Catalogue -> Properties... -> Source paths). We also can use an extension, that is already defined as a 'Source paths'.

  2. A class, that provides one (static) method translate(...) without any functionality. Example: Instead of 'label' => 'foo'we use 'label' => \MyNamespace\Util\Translator::translate('foo')

I think, the second appoach is cleaner, and I like more. I don't need to write my key sring twice and to hold in the head, what is already translated / updated. But maybe there are better ideas?

回答1:

Just add keyword _ to Poedit ([Poedit menu] -> Catalogue -> Properties... -> Source keywords). Then instead of a label use function _ with this label. For example change

        'options' => array(
            'label' => 'Username',
        ),

to

        'options' => array(
            'label' => _('Username'),
        ),

and in Poedit update Catalog From Sources. That's it -- now you have your label in Poedit.



回答2:

I still do not really understand where your problem lies :S From everything i can tell, you're trying to translate the output of a Model/Entity. This is a typical concern of the View!

If the main concern is "How to get all those Model-Output into PoEdit?", then all i can tell you is: either manually or to implement DB-Translation...

Injecting the ServiceManager into basically everything following this syntax of getServiceConfig()

return array( 'factories' => array(
    'my-service' => function ($sm) {
        $class = new ServiceClass();
        $class->setServiceManager($sm);
        return $class;
    }
));

In the above example $class can pretty much be everything. A Model/Entity, a TableGateway or whatever you desire.



回答3:

If you build your form in a different way, like so:

$name = new \Zend\Form\Element('name');
$name->setLabel('Your name');
$name->setAttributes(array(
    'type'  => 'text'
));
$this->add($name);

( http://framework.zend.com/manual/2.0/en/modules/zend.form.quick-start.html )

Then you can register "setLabel" as keyword in poedit, and it will pick up the label-text for translation.