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.
Looking at the Illuminate\Support\Pluralizer
class, the only option I see is to copy that class to your app and overload the original one using composer:
"autoload": {
"files": ["/custom/path/Pluralizer.php"]
}
The "correct way" would be to have some sort of config option to change this in Laravel, but as you see, this option does not exist right now. Be sure to also keep the namespace as it is in your copied version. Overloading the whole class with a custom version of yours might be the easiest solution for you right now.