Skip to content

Commit

Permalink
feature #4426 [String] Add SpanishInflector support for singular and …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
fabpot committed Nov 3, 2024
2 parents e062a17 + 290a923 commit 8d3bad4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
14 changes: 11 additions & 3 deletions extra/string-extra/StringExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
use Symfony\Component\String\Inflector\EnglishInflector;
use Symfony\Component\String\Inflector\FrenchInflector;
use Symfony\Component\String\Inflector\InflectorInterface;
use Symfony\Component\String\Inflector\SpanishInflector;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\String\UnicodeString;
use Twig\Error\RuntimeError;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

final class StringExtension extends AbstractExtension
{
private $slugger;
private $frenchInflector;
private $englishInflector;
private $spanishInflector;
private $frenchInflector;

public function __construct(?SluggerInterface $slugger = null)
{
Expand Down Expand Up @@ -79,10 +82,15 @@ public function singular(string $value, string $locale = 'en', bool $all = false
private function getInflector(string $locale): InflectorInterface
{
switch ($locale) {
case 'fr':
return $this->frenchInflector ?? $this->frenchInflector = new FrenchInflector();
case 'en':
return $this->englishInflector ?? $this->englishInflector = new EnglishInflector();
case 'es':
if (!class_exists(SpanishInflector::class)) {
throw new RuntimeError('SpanishInflector is not available.');
}
return $this->spanishInflector ?? $this->spanishInflector = new SpanishInflector();
case 'fr':
return $this->frenchInflector ?? $this->frenchInflector = new FrenchInflector();
default:
throw new \InvalidArgumentException(\sprintf('Locale "%s" is not supported.', $locale));
}
Expand Down
4 changes: 4 additions & 0 deletions extra/string-extra/Tests/Fixtures/plural.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{{ 'partition'|plural('fr', all=true)|join(',') }}
{{ 'person'|plural('fr') }}
{{ 'person'|plural('en', all=true)|join(',') }}
{{ 'avión'|plural('es') }}
{{ 'avión'|plural('es', all=true)|join(',') }}

--DATA--
return []
Expand All @@ -13,3 +15,5 @@ partitions
partitions
persons
persons,people
aviones
aviones
4 changes: 4 additions & 0 deletions extra/string-extra/Tests/Fixtures/singular.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
{{ 'persons'|singular('en', all=true)|join(',') }}
{{ 'people'|singular('en') }}
{{ 'people'|singular('en', all=true)|join(',') }}
{{ 'personas'|singular('es') }}
{{ 'personas'|singular('es', all=true)|join(',') }}

--DATA--
return []
Expand All @@ -17,3 +19,5 @@ person
person
person
person
persona
persona

0 comments on commit 8d3bad4

Please sign in to comment.