What is the recomended way / best practice to Poedit translate strings without a source keyword?
Asked Answered
H

3

1

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?

Herzberg answered 12/4, 2013 at 10:58 Comment(3)
U can access the translator pretty much everywhere ;) Simply inject the translator or the ServiceManager / ServiceLocator itself ;)Hydroxy
@Hydroxy Oh, it would be great! How can I access it then in an model/table class? E.g. wenn an Expression is used: $select->columns(array('title' => new Expression('IFNULL(sports.title, "' . 'unassigned')));Herzberg
I can translate the string in the view scrtip, e.g.: $this->translate($sport->title). But it will only be possible, if the key string has already been added to the .po file. That means, I need access to the Service Locator from my table class. Well, I can pass it as constructor argument, when I'm creating its object in the Module class, and use is directly in the Table class. But then I need every time to take care of it.Herzberg
P
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.

Persuader answered 10/8, 2014 at 21:40 Comment(0)
H
0

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.

Hydroxy answered 13/4, 2013 at 8:3 Comment(5)
Where the problem lies: First of all I have to use the translate() (or another registered Poedit keyword) somewhere in the code, in order to pass strings to Poedit automatically. Right? Now. Where can I do that? The label value gets defined in the Form class. That means, I cannot use the Translate view helper (= Poedit keyword) for it in the view. But I also cannot use it in the Form class. (OK, maybe I could theoretically somehow, but... 1. how? 2. view helper in a Form class?..) So I cannot understand, how to pass my labels to Poedit and translate them correctly.Herzberg
Well, i suppose you could always add 'label' => '%s' to PoEdit, that'd only require you to never have additional (CodeFormat) spaces in there? Couple of things are just better of done manually sometimes ^^Hydroxy
A pattern like 'label' => '%s' does unfortunately not work, since Poedit needs a function and parses then its argument(-s). Manually? OK, maybe. Or with the second workaround I described in the question.Herzberg
Yeps, the 2nd workaround is actually something that's becoming a little familiar in some open source projects where people add a language-strings file additionally.Hydroxy
What's you telling about is the first workaround. Anyway, do you have an example of a project, where this approach is being used?Herzberg
O
0

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.

Oxy answered 26/7, 2013 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.