Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add italian rules#276

Merged
greg0ire merged 1 commit into
doctrine:2.1.xfrom
f-liva:2.0.x
Jun 27, 2025
Merged

Add italian rules#276
greg0ire merged 1 commit into
doctrine:2.1.xfrom
f-liva:2.0.x

Conversation

@f-liva
Copy link
Copy Markdown

@f-liva f-liva commented Jun 26, 2025

No description provided.

@f-liva
Copy link
Copy Markdown
Author

f-liva commented Jun 26, 2025

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

@greg0ire
Copy link
Copy Markdown
Member

Hi! Why do you keep opening new PRs? Can't you force-push?

@greg0ire
Copy link
Copy Markdown
Member

Regarding the CS issues, have you tried running vendor/bin/phpcbf?

@f-liva
Copy link
Copy Markdown
Author

f-liva commented Jun 26, 2025

Hi! Why do you keep opening new PRs? Can't you force-push?

I thought force push didn't update the PR, so I'll leave only this one open.

Regarding the CS issues, have you tried running vendor/bin/phpcbf?

No, now I’m launching it and trying.

@f-liva
Copy link
Copy Markdown
Author

f-liva commented Jun 26, 2025

Ok, I ran phpcbf and force pushed.

@greg0ire greg0ire changed the base branch from 2.0.x to 2.1.x June 26, 2025 08:14
@greg0ire greg0ire changed the base branch from 2.1.x to 2.0.x June 26, 2025 08:14
@greg0ire greg0ire changed the base branch from 2.0.x to 2.1.x June 26, 2025 08:24
Comment thread src/InflectorFactory.php Outdated
@f-liva f-liva force-pushed the 2.0.x branch 2 times, most recently from 1c20e35 to 4c1b91d Compare June 26, 2025 08:31
Comment thread src/InflectorFactory.php Outdated
@f-liva
Copy link
Copy Markdown
Author

f-liva commented Jun 26, 2025

@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.
I was thinking of creating a MultiWordInflector that extends and uses the standard WordInflector to inflect all the words of the input string. It could be something like this:

<?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.

@greg0ire
Copy link
Copy Markdown
Member

greg0ire commented Jun 26, 2025

Have you considered decorating the factory?

class MultiWordLanguageInflectorFactory implements LanguageInflectorFactory
{
   final public function build(): Inflector
    {
        return new MultiWordInflectorDecorator($this->decorated->build());
    }
…
}

@f-liva
Copy link
Copy Markdown
Author

f-liva commented Jun 26, 2025

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?
Can you take a look at the implementation here: https://github.com/f-liva/doctrine-inflector/tree/feature/multiword-inflector

@greg0ire
Copy link
Copy Markdown
Member

greg0ire commented Jun 26, 2025

Open a draft PR, it will be more convenient to review/leave comments.

@greg0ire
Copy link
Copy Markdown
Member

Yes, but this would force me to recreate the same behavior as GenericLanguageInflectorFactory, which I believe is not very DRY.

Would it? You could proxy methods to the decorated object…

Copy link
Copy Markdown
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤌

@greg0ire greg0ire added this to the 2.1.0 milestone Jun 27, 2025
@greg0ire greg0ire merged commit 02ab81d into doctrine:2.1.x Jun 27, 2025
13 checks passed
@greg0ire
Copy link
Copy Markdown
Member

Thanks @f-liva !

@f-liva
Copy link
Copy Markdown
Author

f-liva commented Jun 27, 2025

Perfect, thanks to you. I will soon open a new PR for the pluralization of multiple words.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants