Skip to main content

Translating Plural/Singular Forms

Premium
3 min read

Have you ever needed counting specific items, with translations?

A typical code would be:

<div class="">
@if($messagesCount === 0)
<span>You have no new messages</span>
@elseif($messagesCount === 1)
<span>You have 1 new message</span>
@else
<span>You have {{ $messagesCount }} new messages</span>
@endif
</div>

To avoid these if conditions, you can use the Laravel localization pluralization feature.


Using trans_choice() Helper

Laravel comes with a really great helper function called trans_choice(). This function allows us to use a string that contains multiple translations, and it will choose the correct one based on the number we pass to it...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 41 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.