Why does Laravel's trans_choice () always show a singular case?

I am trying to use trans_choice()

to simply format the string for "comments" or "comments" depending on how many there are. It should be pretty simple like this.

In my opinion:

{{ trans_choice('posts.num comments', $post->comments->count()) }}

      

In the message localization file:

return [
    'num comments' => 'comment|comments',
];

      

However, each of them only returns a "comment". And if I go messing around:

>>> trans_choice('posts.num comments', 1);
=> "comment"
>>> trans_choice('posts.num comments', 2);
=> "comment"
>>> trans_choice('posts.num comments', 4);
=> "comment"

      

I'm sure I'm missing something obvious, but I feel like I'm following the documentation very well.

Edit: The problem seems to lie somewhere Symfony\Component\Translation\MessageSelector

, but I haven't figured out the reason yet.

+3


source to share


2 answers


Finally found the answer. Apparently, if the locale is not available in the Symfony PluralizationRules class, then by default the translator selects the first choice, that is, always index zero. By changing the locale name (I didn't even realize it was spelled wrong) I got it working.



+8


source


If you are Brazilian, this is probably your answer!

I had the same problem and found out that it was related to my locale (and lang folder). Instead of "br", we must use "xbr" so that Symphony finds it in PluralizationRules (vendor / symphony / translation / PluralizationRules).



This file has an array with all available languages, so it can be checked.

PS I am using Laravel 5.3

+1


source







All Articles