'Pluralization is not working when using translation strings as keys in laravel localization
I added this in fr.json
{
"There is one apple|There are many apples": "Hay una manzana|Hay muchas manzanas"
}
In blade file:
{{__("There is one apple")}}
Then it shows
There is one apple
It should show Hay una manzana. Please help me to solve it.
Solution 1:[1]
The default language for your application is stored in the config/app.php configuration file's locale configuration option. You are free to modify this value to suit the needs of your application.
In your case it should be
'locale' => 'fr',
You may find more details in documentation https://laravel.com/docs/8.x/localization#configuring-the-locale
Solution 2:[2]
First make sure you set config/app.php, as
'locale' => 'fr'.
You may set that per request using Illuminate\Support\Facades\App::setLocal('fr');
within the routes(Route::get()) or maybe using a middleware.
After you set the locale, add this key:value pair to resources/lang/fr.json file:
"There is one apple|There are many apples" : "{1} Hay una manzana|[2,*] Hay muchas manzanas".
And instead of __() use trans_choice() method. And you also have to provide the number of apple/apples as the second argument. That's how it knows if it's going to be singular(1) or plural(2+). So this how you use it inside the blade file:
{{ trans_choice("There is one apple|There are many apples", 2) }}.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SiZE |
| Solution 2 | PMekuli |
