Change Laravel Pluralizer Rules
I have a project in Laravel which has model names, fields and messages in Spanish, I am currently using laravel-shift/blueprint to generate all the base code of the application from a yaml file, at the moment of Laravel infer the pluralization of the names for the migrations, controllers, variables etc, it makes them treating the words as English words, for example:
+--------------+----------------------------+--------------------------+
| Word | English Plural (incorrect) | Spanish Plural (correct) |
+==============+============================+==========================+
| Animal | Animals | Animales |
+--------------+----------------------------+--------------------------+
| Organizacion | Organizacions | Organizaciones |
+--------------+----------------------------+--------------------------+
| Estacion | Estacions | Estaciones |
+--------------+----------------------------+--------------------------+
| Regulador | Reguladors | Reguladores |
+--------------+----------------------------+--------------------------+
Looking at the framework I realized that in the file vendor/laravel/framework/src/Illuminate/Support/Pluralizer.php uses the Doctrine Inflector rules for the English language to create the inflector instance that Laravel uses:
use Doctrine\Inflector\Rules\English;
public static function inflector()
{
static $inflector;
if (is_null($inflector)) {
$inflector = new Inflector(
new CachedWordInflector(new RulesetInflector(
English\Rules::getSingularRuleset()
)),
new CachedWordInflector(new RulesetInflector(
English\Rules::getPluralRuleset()
))
);
}
return $inflector;
}
I edited the file to use the Spanish rules:
use Doctrine\Inflector\Rules\Spanish;
public static function inflector()
{
static $inflector;
if (is_null($inflector)) {
$inflector = new Inflector(
new CachedWordInflector(new RulesetInflector(
Spanish\Rules::getSingularRuleset()
)),
new CachedWordInflector(new RulesetInflector(
Spanish\Rules::getPluralRuleset()
))
);
}
return $inflector;
}
So the pluralization works as I expected for the Spanish words.
Is there any way to tell laravel to use the Spanish rules without having to change code inside the vendor folder?
P.S.
Previously I used a service provider that extended the array of irregular words in the rules for plural in inflector:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Doctrine\Common\Inflector\Inflector;
class SpanishPluralizerServiceProvider extends ServiceProvider
{
public function register()
{
Inflector::rules('plural', [
'irregular' => [
"organizacion" => "organizaciones",
"proovedor" => "proovedores",
"ubicacion" => "ubicaciones",
"estacion" => "estaciones",
"operador" => "operadores",
"sucursal" => "sucursales",
"entidad" => "entidades",
"ciudad" => "ciudades",
"legal" => "legales",
"pais" => "paises"
]
]);
}
public function boot()
{
//
}
}
but this doesn't seem to work anymore in Laravel 7 and Laravel 8
Thank you in advance for your time and help.
Please or to participate in this conversation.