Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to use trans_choice() to simply format a string to either "comment" or "comments" depending on the number of them. It should be fairly straightforward, like this.

In my view:

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

In the posts localisation file:

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

However, every single one returns just "comment". And if I go into tinker:

>>> 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 it looks to me as if I've followed the documentation perfectly.

Edit: The problem seems to lie somewhere in Symfony\Component\Translation\MessageSelector, but I haven't yet figured the cause out.

like image 545
Joel Hinz Avatar asked Dec 03 '22 16:12

Joel Hinz


1 Answers

Finally found the answer. Apparently, if the locale isn't available in Symfony's PluralizationRules class, the translator defaults to the first pick - that is, always index zero. By changing the locale name (I didn't even realise it was misspelled...), I got it working.

like image 81
Joel Hinz Avatar answered Dec 25 '22 19:12

Joel Hinz