Skip to content

Commit 8d3bad4

Browse files
committed
feature #4426 [String] Add SpanishInflector support for singular and plural (dennistobar)
This PR was squashed before being merged into the 3.x branch. Discussion ---------- [String] Add SpanishInflector support for singular and plural This PR implement a new Inflector using Spanish (ISO Code = es) implemented in this PR symfony/symfony#58228. * I sort the list of supported Inflectors to follow some rule (alphabet?) * I just change invalid test from "it" to "qq" (qq doesn't exist as valid ISO code, but it is italian... don't be evil if anyone want to create the italian inflector 😅 ) Commits ------- 290a923 [String] Add SpanishInflector support for singular and plural
2 parents e062a17 + 290a923 commit 8d3bad4

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

extra/string-extra/StringExtension.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@
1515
use Symfony\Component\String\Inflector\EnglishInflector;
1616
use Symfony\Component\String\Inflector\FrenchInflector;
1717
use Symfony\Component\String\Inflector\InflectorInterface;
18+
use Symfony\Component\String\Inflector\SpanishInflector;
1819
use Symfony\Component\String\Slugger\AsciiSlugger;
1920
use Symfony\Component\String\Slugger\SluggerInterface;
2021
use Symfony\Component\String\UnicodeString;
22+
use Twig\Error\RuntimeError;
2123
use Twig\Extension\AbstractExtension;
2224
use Twig\TwigFilter;
2325

2426
final class StringExtension extends AbstractExtension
2527
{
2628
private $slugger;
27-
private $frenchInflector;
2829
private $englishInflector;
30+
private $spanishInflector;
31+
private $frenchInflector;
2932

3033
public function __construct(?SluggerInterface $slugger = null)
3134
{
@@ -79,10 +82,15 @@ public function singular(string $value, string $locale = 'en', bool $all = false
7982
private function getInflector(string $locale): InflectorInterface
8083
{
8184
switch ($locale) {
82-
case 'fr':
83-
return $this->frenchInflector ?? $this->frenchInflector = new FrenchInflector();
8485
case 'en':
8586
return $this->englishInflector ?? $this->englishInflector = new EnglishInflector();
87+
case 'es':
88+
if (!class_exists(SpanishInflector::class)) {
89+
throw new RuntimeError('SpanishInflector is not available.');
90+
}
91+
return $this->spanishInflector ?? $this->spanishInflector = new SpanishInflector();
92+
case 'fr':
93+
return $this->frenchInflector ?? $this->frenchInflector = new FrenchInflector();
8694
default:
8795
throw new \InvalidArgumentException(\sprintf('Locale "%s" is not supported.', $locale));
8896
}

extra/string-extra/Tests/Fixtures/plural.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
{{ 'partition'|plural('fr', all=true)|join(',') }}
66
{{ 'person'|plural('fr') }}
77
{{ 'person'|plural('en', all=true)|join(',') }}
8+
{{ 'avión'|plural('es') }}
9+
{{ 'avión'|plural('es', all=true)|join(',') }}
810

911
--DATA--
1012
return []
@@ -13,3 +15,5 @@ partitions
1315
partitions
1416
persons
1517
persons,people
18+
aviones
19+
aviones

extra/string-extra/Tests/Fixtures/singular.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
{{ 'persons'|singular('en', all=true)|join(',') }}
88
{{ 'people'|singular('en') }}
99
{{ 'people'|singular('en', all=true)|join(',') }}
10+
{{ 'personas'|singular('es') }}
11+
{{ 'personas'|singular('es', all=true)|join(',') }}
1012

1113
--DATA--
1214
return []
@@ -17,3 +19,5 @@ person
1719
person
1820
person
1921
person
22+
persona
23+
persona

0 commit comments

Comments
 (0)