vincent15000's avatar

Str::pluralStudly() not working in french

Hello,

https://laravel.com/docs/12.x/strings#method-str-plural-studly

https://laravel.com/docs/12.x/localization#pluralization-language

Perhaps I forgot some configuration ?

I have tried to change the language to fr in the app.php file, but it doesn't work better.

Well ... it works only if I add Pluralizer::useLanguage('french'); to the AppServiceProvider, but then I also have to change all tables' names. I'd like to activate the pluralizer only for the views.

Thanks for your help.

V

0 likes
3 replies
Glukinho's avatar

Did you set in AppServiceProvider's boot():

Pluralizer::useLanguage('french');

?

1 like
Glukinho's avatar
Level 31

I think you could make a macro

// AppSerivceProvider -> boot()

Str::macro('pluralWithLang', function (string $value, string $lang) {
	Pluralizer::useLanguage($lang);
	$result = Str::plural($value);
	Pluralizer::useLanguage('english');
	return $result;
});

And use it:

$plural = Str::pluralWithLang($somestring, 'french');
1 like
Glukinho's avatar

I think it is safe. Setting Pluralizer::useLanguage($lang) and back in one request doesn't affect other or subsequent requests, this setting is not stored anywhere and is used in current request only.

To affect other requests you should read from/write to some persistent storage: cache, database or a simple file.

1 like

Please or to participate in this conversation.