Add italian rules#276
Conversation
|
Hi, I can't understand what is wrong with the Coding Standard check; I get these errors, but the files are identical to the versions in the other languages. https://github.com/f-liva/doctrine-inflector/actions/runs/15895357977 |
|
Hi! Why do you keep opening new PRs? Can't you force-push? |
|
Regarding the CS issues, have you tried running |
I thought force push didn't update the PR, so I'll leave only this one open.
No, now I’m launching it and trying. |
|
Ok, I ran phpcbf and force pushed. |
1c20e35 to
4c1b91d
Compare
|
@greg0ire I would also like to understand how to implement an inflector for compound expressions, because in Italian the combinations of words change between singular and plural. Let me explain better: words like "libro giallo," which in English translate to "yellow book" and in the plural become "yellow books" (where only the last word is pluralized), in Italian the singular is "libro giallo" while the plural becomes "libri gialli," so both words must be pluralized. <?php
declare(strict_types=1);
namespace Doctrine\Inflector\Rules\Italian;
use Doctrine\Inflector\WordInflector;
class MultiWordInflectorDecorator implements WordInflector
{
private WordInflector $wordInflector;
public function __construct(WordInflector $wordInflector)
{
$this->wordInflector = $wordInflector;
}
public function inflect(string $word): string
{
// If it's a single word without spaces or hyphens, use the original inflector
if (strpos($word, ' ') === false && strpos($word, '-') === false) {
return $this->wordInflector->inflect($word);
}
// Split the phrase into words and process each one
$words = preg_split('/([ -])/', $word, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$result = [];
foreach ($words as $i => $part) {
if ($part === ' ' || $part === '-') {
$result[] = $part;
continue;
}
// Process each word individually
$result[] = $this->wordInflector->inflect($part);
}
return implode('', $result);
}
}The problem is that to do this I would need to use RulesetInflector, but since GenericLanguageInflectorFactory::build is a final method, I cannot modify the RulesetInflector class used. Do you have any suggestions on how I could integrate this change in a new PR? This existing PR instead adds support for correct pluralization of individual words which, following the previous example, would transform "libro giallo" into "libro gialli", a result that is partially pluralized. |
|
Have you considered decorating the factory? class MultiWordLanguageInflectorFactory implements LanguageInflectorFactory
{
final public function build(): Inflector
{
return new MultiWordInflectorDecorator($this->decorated->build());
}
…
} |
Yes, but this would force me to recreate the same behavior as GenericLanguageInflectorFactory, which I believe is not very DRY. I decided to transform the RulesetInflector class into an abstract class based on GenericRulesetInflector, which is the original class. This way, I was able to create Italian/RulesetInflector by extending GenericRulesetInflector, allowing me to reuse and keep the original inflect method logic intact. Subsequently, I modified GenericLanguageInflectorFactory to allow specifying which RulesetInflector to use, and in Italian/InflectorFactory I specify it via the createRulesetInflector method. Finally, I updated RulesetInflectorTest so that in mocks the GenericRulesetInflector class is instantiated instead of the abstract RulesetInflector class. All tests passed, and now Italian can handle pluralization of multiple words. What do you think? |
|
Open a draft PR, it will be more convenient to review/leave comments. |
Would it? You could proxy methods to the decorated object… |
|
Thanks @f-liva ! |
|
Perfect, thanks to you. I will soon open a new PR for the pluralization of multiple words. |
No description provided.