更改 Laravel 复数规则

Change Laravel Pluralizer Rules

我在 Laravel 中有一个项目,其中包含西班牙语的模型名称、字段和消息,我目前正在使用 laravel-shift/blueprint 从 [=18= 生成应用程序的所有基本代码] 文件,在 Laravel 推断迁移、控制器、变量等名称的复数形式,这使得它们将单词视为英文单词,例如:

+--------------+----------------------------+--------------------------+
| Word         | English Plural (incorrect) | Spanish Plural (correct) |
+==============+============================+==========================+
| Animal       | Animals                    | Animales                 |
+--------------+----------------------------+--------------------------+
| Organizacion | Organizacions              | Organizaciones           |
+--------------+----------------------------+--------------------------+
| Estacion     | Estacions                  | Estaciones               |
+--------------+----------------------------+--------------------------+
| Regulador    | Reguladors                 | Reguladores              |
+--------------+----------------------------+--------------------------+

查看框架,我意识到在文件中 vendor/laravel/framework/src/Illuminate/Support/Pluralizer.php 使用英语的 Doctrine Inflector 规则来创建 Laravel 使用的 inflector 实例:

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;
    }

我编辑文件以使用西班牙语规则:

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;
    }

所以西班牙语单词的复数化效果和我预期的一样。

有什么方法可以告诉 laravel 使用西班牙语规则而无需更改 vendor 文件夹中的代码?

P.S.

之前我使用了一个服务提供商,它扩展了变形器复数规则中的不规则单词数组:

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()
  {
    //
  }
}

但这似乎不再适用于 Laravel 7 和 Laravel 8

提前感谢您的宝贵时间和帮助。

查看 Illuminate\Support\Pluralizer class,我看到的唯一选择是将 class 复制到您的应用并使用 composer 重载原始应用:

 "autoload": {
     "files": ["/custom/path/Pluralizer.php"]
  }

“正确的方法”是在 Laravel 中使用某种配置选项来更改此设置,但如您所见,此选项目前不存在。确保还保留命名空间,因为它在您复制的版本中。用您的自定义版本重载整个 class 可能是目前最简单的解决方案。