- Translate plural, translations in phparray

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

- Translate plural, translations in phparray

MadCat34
Hi all,

I have a problem using translatePlural helper (translation in php array)
My translator is well defined, no problem with other translations in website

Doing this in a view:

$this->translatePlural('day', 'days', $numberDay);

In my translation file  (named fr_FR.php)

return array(
    ...
    'day' => 'jour',
    'days' => 'jours'
)

In French, 'day' should be 'jour' and 'days' should be 'jours'.

But i get:
'day' translated by 'j'
'days' translated by 'o'


Is there a special syntax to translate plural using phparray translator ?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: - Translate plural, translations in phparray

Bas Kamer
Hi

Well there are two thing you are doing incorrect.

The array for plurals should contain an indexed based array, with as many translation the plural rules dictates for the language. A default rule is 'nplurals=2; plural=n != 1;' meaning there are two plural forms for a language. The first (index based 0) if the number given is not 1. So for 0 use index 1, for 1 use index 0, for 2 use index 1, etc… The array should contain these indexes.

Here are more complex rules for other languages http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html

I see that for french the rule is 'nplurals=2; plural=(n > 1);'  index position 0 (jour) for zero and one days, index position 1 (jours) for more than one day.

Perhaps now you see why you get j and o as translations.

The second thing you should be doing is use placeholders for the actual number given. Since in every language the position of the number relative to the translation could be different. I found it useful to use anything that can be fed into sprintf. Doing it this way also prevent regular translations to be overwritten by the plural forms, since the keys (or messages) MUST be unique within one textdomain...

return array(
    '' => array(
        'plural_forms' => 'nplurals=2; plural=n!=1;'
    ),
    'day => 'jour', // regular non plural translation
    '%s day' => array(
        '%s jour',
        '%s jours',
    ),
);


usage is than with the placeholders
$translator->translatePlural('%s day', %s days', $n);
$translator->translate('day');


The translatePlural method will use the first argument as the message (or lookup key) to be translated. It will return it when no plural translation has been found and the plural rules dictates an index position of 0. The second argument is only used when no plural translation has been found and if the plural rule dictates the second plural form is appropriate. As far as I know, these plural fallbacks can only work correctly if the plural rule defines maximum of two possible plural forms - as there are no more fallback arguments.

Hope this explains a bit


On 17 sep. 2013, at 17:18, MadCat <[hidden email]> wrote:

> Hi all,
>
> I have a problem using translatePlural helper (translation in php array)
> My translator is well defined, no problem with other translations in website
>
> Doing this in a view:
>
> $this->translatePlural('day', 'days', $numberDay);
>
> In my translation file  (named fr_FR.php)
>
> return array(
>    ...
>    'day' => 'jour',
>    'days' => 'jours'
> )
>
> In French, 'day' should be 'jour' and 'days' should be 'jours'.
>
> But i get:
> 'day' translated by 'j'
> 'days' translated by 'o'
>
>
> Is there a special syntax to translate plural using phparray translator ?
>
> Thanks


--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: - Translate plural, translations in phparray

MadCat34
Thanks for your explanation.
It's clearer now :-)

Documentation was not clear on this point...

your link for pluralforms is now in my favorite link (would be very usefull
later, i think)


2013/10/4 Bas Kamer <[hidden email]>

> Hi
>
> Well there are two thing you are doing incorrect.
>
> The array for plurals should contain an indexed based array, with as many
> translation the plural rules dictates for the language. A default rule is
> 'nplurals=2; plural=n != 1;' meaning there are two plural forms for a
> language. The first (index based 0) if the number given is not 1. So for 0
> use index 1, for 1 use index 0, for 2 use index 1, etc… The array should
> contain these indexes.
>
> Here are more complex rules for other languages
> http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html
>
> I see that for french the rule is 'nplurals=2; plural=(n > 1);'  index
> position 0 (jour) for zero and one days, index position 1 (jours) for more
> than one day.
>
> Perhaps now you see why you get j and o as translations.
>
> The second thing you should be doing is use placeholders for the actual
> number given. Since in every language the position of the number relative
> to the translation could be different. I found it useful to use anything
> that can be fed into sprintf. Doing it this way also prevent regular
> translations to be overwritten by the plural forms, since the keys (or
> messages) MUST be unique within one textdomain...
>
> return array(
>     '' => array(
>         'plural_forms' => 'nplurals=2; plural=n!=1;'
>     ),
>     'day => 'jour', // regular non plural translation
>     '%s day' => array(
>         '%s jour',
>         '%s jours',
>     ),
> );
>
>
> usage is than with the placeholders
> $translator->translatePlural('%s day', %s days', $n);
> $translator->translate('day');
>
>
> The translatePlural method will use the first argument as the message (or
> lookup key) to be translated. It will return it when no plural translation
> has been found and the plural rules dictates an index position of 0. The
> second argument is only used when no plural translation has been found and
> if the plural rule dictates the second plural form is appropriate. As far
> as I know, these plural fallbacks can only work correctly if the plural
> rule defines maximum of two possible plural forms - as there are no more
> fallback arguments.
>
> Hope this explains a bit
>
>
> On 17 sep. 2013, at 17:18, MadCat <[hidden email]> wrote:
>
> > Hi all,
> >
> > I have a problem using translatePlural helper (translation in php array)
> > My translator is well defined, no problem with other translations in
> website
> >
> > Doing this in a view:
> >
> > $this->translatePlural('day', 'days', $numberDay);
> >
> > In my translation file  (named fr_FR.php)
> >
> > return array(
> >    ...
> >    'day' => 'jour',
> >    'days' => 'jours'
> > )
> >
> > In French, 'day' should be 'jour' and 'days' should be 'jours'.
> >
> > But i get:
> > 'day' translated by 'j'
> > 'days' translated by 'o'
> >
> >
> > Is there a special syntax to translate plural using phparray translator ?
> >
> > Thanks
>
>