Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Configurable pluralizer language and uncountables #41941

Merged
merged 4 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 33 additions & 45 deletions src/Illuminate/Support/Pluralizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,31 @@
class Pluralizer
{
/**
* Uncountable word forms.
* The cached inflector instance.
*
* @var static
*/
protected static $inflector;

/**
* The language that should be used by the inflector.
*
* @var string
*/
protected static $language = 'english';

/**
* Uncountable non-nouns word forms.
*
* Contains words supported by Doctrine/Inflector/Rules/English/Uninflected.php
*
* @var string[]
*/
public static $uncountable = [
'audio',
'bison',
'cattle',
'chassis',
'compensation',
'coreopsis',
'data',
'deer',
'education',
'emoji',
'equipment',
'evidence',
'feedback',
'firmware',
'fish',
'furniture',
'gold',
'hardware',
'information',
'jedi',
'kin',
'knowledge',
'love',
'metadata',
'money',
'moose',
'news',
'nutrition',
'offspring',
'plankton',
'pokemon',
'police',
'rain',
'recommended',
'related',
'rice',
'series',
'sheep',
'software',
'species',
'swine',
'traffic',
'wheat',
];

/**
Expand Down Expand Up @@ -130,12 +107,23 @@ protected static function matchCase($value, $comparison)
*/
public static function inflector()
{
static $inflector;

if (is_null($inflector)) {
$inflector = InflectorFactory::createForLanguage('english')->build();
if (is_null(static::$inflector)) {
static::$inflector = InflectorFactory::createForLanguage(static::$language)->build();
}

return $inflector;
return static::$inflector;
}

/**
* Specify the language that should be used by the inflector.
*
* @param string $language
* @return void
*/
public static function useLanguage(string $language)
{
static::$language = $language;

static::$inflector = null;
}
}
105 changes: 105 additions & 0 deletions tests/Integration/Support/PluralizerPortugueseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Illuminate\Tests\Integration\Support;

use Illuminate\Support\Pluralizer;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase;

class PluralizerPortugueseTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

Pluralizer::useLanguage('portuguese');
}

public function tearDown(): void
{
parent::tearDown();

Pluralizer::useLanguage('english');
}

public function testBasicSingular()
{
$this->assertSame('herói', Str::singular('heróis'));
$this->assertSame('irmão', Str::singular('irmãos'));
$this->assertSame('chafariz', Str::singular('chafarizes'));
$this->assertSame('colher', Str::singular('colheres'));
$this->assertSame('modelo', Str::singular('modelos'));
$this->assertSame('venda', Str::singular('vendas'));
$this->assertSame('usuário', Str::singular('usuários'));
$this->assertSame('comissão', Str::singular('comissões'));
}

public function testIrregulars()
{
$this->assertSame('males', Str::plural('mal'));
$this->assertSame('lápis', Str::singular('lápis'));
}

public function testBasicPlural()
{
$this->assertSame('fênix', Str::plural('fênix'));
$this->assertSame('palavras', Str::plural('palavra'));
$this->assertSame('modelos', Str::plural('modelo'));
$this->assertSame('vendas', Str::plural('venda'));
$this->assertSame('usuários', Str::plural('usuário'));
$this->assertSame('comissões', Str::plural('comissão'));
}

public function testCaseSensitiveSingularUsage()
{
$this->assertSame('Criança', Str::singular('Crianças'));
$this->assertSame('CIDADÃO', Str::singular('CIDADÃOS'));
}

public function testCaseSensitiveSingularPlural()
{
$this->assertSame('Crianças', Str::plural('Criança'));
$this->assertSame('CIDADÃOS', Str::plural('CIDADÃO'));
$this->assertSame('Testes', Str::plural('Teste'));
}

public function testPluralAppliedForStringEndingWithNumericCharacter()
{
$this->assertSame('Usuário1s', Str::plural('Usuário1'));
$this->assertSame('Usuário2s', Str::plural('Usuário2'));
$this->assertSame('Usuário3s', Str::plural('Usuário3'));
}

public function testPluralSupportsArrays()
{
$this->assertSame('usuários', Str::plural('usuário', []));
$this->assertSame('usuário', Str::plural('usuário', ['um']));
$this->assertSame('usuários', Str::plural('usuário', ['um', 'dois']));
}

public function testPluralSupportsCollections()
{
$this->assertSame('usuários', Str::plural('usuário', collect()));
$this->assertSame('usuário', Str::plural('usuário', collect(['um'])));
$this->assertSame('usuários', Str::plural('usuário', collect(['um', 'dois'])));
}

public function testPluralStudlySupportsArrays()
{
$this->assertPluralStudly('AlgumUsuários', 'AlgumUsuário', []);
$this->assertPluralStudly('AlgumUsuário', 'AlgumUsuário', ['um']);
$this->assertPluralStudly('AlgumUsuários', 'AlgumUsuário', ['um', 'dois']);
}

public function testPluralStudlySupportsCollections()
{
$this->assertPluralStudly('AlgumUsuários', 'AlgumUsuário', collect());
$this->assertPluralStudly('AlgumUsuário', 'AlgumUsuário', collect(['um']));
$this->assertPluralStudly('AlgumUsuários', 'AlgumUsuário', collect(['um', 'dois']));
}

private function assertPluralStudly($expected, $value, $count = 2)
{
$this->assertSame($expected, Str::pluralStudly($value, $count));
}
}