1

I'm trying to translate results count to Lithuanian and there are some specific rules. I'll try to explain them:

  • All counts ending with zero (0, 10, 20 ... 1050, 1060...) and between 12 & 19 inclusive (12...19)
  • All counts ending with one, except ending with 11 (1, 21, 31 ... 1221, 1231... but not 11, 111, 211 ... 2311, 2411)
  • All other not listed above (2...9 inclusive and more than 21 to which above rules does not apply)

I tried something like this (using YAML), but even number 40 doesn't match the rules:

'%count% Results, ': '{0,*0}%count% rezultatų |{1,*1}%count% rezultatas |]1,10[%count% rezultatai |]10,20[%count% rezultatų '

Is it even possible to do something like that using YAML? With the above example I get:

An exception has been thrown during the rendering of a template ("Unable to choose a translation for "{0,*0}%count% rezultatų |{1,*1}%count% rezultatas |]1,10[%count% rezultatai |]10,20[%count% rezultatų " with locale "lt". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %count% apples").")

Karmalakas
  • 1,143
  • 2
  • 19
  • 39

2 Answers2

4

Symfony2's Translator only supports the ISO 31-11 notation. That format doesn't have the * wildchart you are using. What you are trying to do is not possible with Symfony2 at te moment.

However, you can extend Symfony's Translator and add this functionality. You can do that by overriding the Symfony\Component\Translation\MessageSelector class, adding the functionality and then change the service parameter translator.selector.class to your class name. For instance:

// src/Acme/TranslationExtraBundle/Translation/MessageSelector.php
namespace Acme\TranslationExtraBundle\Translation;

use Symfony\Component\Translation\MessageSelector as BaseMessageSelector;

class MessageSelector extends BaseMessageSelector
{
    public function choose($message, $number, $locale)
    {
        // ... your special logic

        return parent::choose($message, $number, $locale);
    }
}
parameters:
    translator.selector.class: Acme\TranslationExtraBundle\Translation\MessageSelector
Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • I think something is wrong with `translation.selector.class`, because it doesn't change anything - I can write there any text, but no error or something. _Edit:_ Noticed it should be `translator`, not `translation`. Checking again :) – Karmalakas Apr 05 '13 at 11:40
  • So I was trying to solve my problem by extending MessageSelector and found out that there are `Symfony\Component\Translation\PluralizationRules` and Lithuanian is already there. But thanks for the tip. – Karmalakas Apr 05 '13 at 13:13
  • 1
    @CRONUS great! But I hope you understand my post, using the service container is really usefull to extend/customize the Symfony2 framework for other things in the future. – Wouter J Apr 05 '13 at 16:24
3

So I was trying to solve my problem following Wouters answer and found out that there is Symfony\Component\Translation\PluralizationRules and Lithuanian is already there. All I had to do is remove intervals from my translation line and it works as expected now.

'%count% Results, ': '%count% rezultatas |%count% rezultatai |%count% rezultatų '
Karmalakas
  • 1,143
  • 2
  • 19
  • 39