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

Sluggable and accents #173

Closed
CharlyPoppins opened this issue Jan 5, 2015 · 10 comments
Closed

Sluggable and accents #173

CharlyPoppins opened this issue Jan 5, 2015 · 10 comments

Comments

@CharlyPoppins
Copy link

Hello,

sluggable is not handling accents. Is that normal or did I miss something in the settings ?

@CharlyPoppins
Copy link
Author

There is a problem with iconv.
$sluggableText = "Châteauneuf du Pape"; $usableValues = iconv('UTF-8', 'ASCII//TRANSLIT', $sluggableText); echo $usableValues; // Ch?teauneuf du Pape

@CharlyPoppins
Copy link
Author

Saw this on https://php.net/manual/fr/function.iconv.php

Please note that iconv('UTF-8', 'ASCII//TRANSLIT', ...) doesn't work properly when locale category LC_CTYPE is set to C or POSIX. You must choose another locale otherwise all non-ASCII characters will be replaced with question marks. This is at least true with glibc 2.5.

Example:

https://github.com/KnpLabs/DoctrineBehaviors/blob/master/src/Model/Sluggable/SluggableMethods.php
So I set setlocale(LC_CTYPE, "fr_FR"); in public function generateSlug() { ... }

Is that the right thing to do ?

@docteurklein
Copy link
Contributor

Unfortunatly, I never had to run into those problems, and I have no idea how transliteration works :)

@markitosgv
Copy link

accents like áéóèàñß not been converted i think is possible to modify generateSlug function with this

public function generateSlug()
{
    if ( $this->getRegenerateSlugOnUpdate() || empty( $this->slug ) ) {
        $fields = $this->getSluggableFields();
        $usableValues = [];

        foreach ($fields as $field) {
            // Too bad empty is a language construct...otherwise we could use the return value in a write context :)
            $val = $this->{$field};
            if ( !empty( $val ) ) {
                $usableValues[] = $val;
            }
        }

        if ( count($usableValues) < 1 ) {
            throw new \UnexpectedValueException('Sluggable expects to have at least one usable (non-empty) field from the following: [ ' . implode($fields, ',') .' ]');
        }

        // generate the slug itself
        $sluggableText = implode($usableValues, ' ');
        $sluggableText = strtr(utf8_decode($sluggableText), utf8_decode('ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'),
            'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy');
        $urlized = strtolower( trim( preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', iconv('UTF-8', 'ASCII//TRANSLIT', $sluggableText) ), $this->getSlugDelimiter() ) );
        $urlized = preg_replace("/[\/_|+ -]+/", $this->getSlugDelimiter(), $urlized);

        $this->slug = $urlized;
    }
}

What do you think about insert this conversion?

$sluggableText = strtr(utf8_decode($sluggableText), utf8_decode('ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'),
            'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy');

Words like 'Café', 'Niño', 'ße' become 'cafe', 'nino', 'be'

@kuczek
Copy link
Contributor

kuczek commented Feb 25, 2015

@markitosgv It does not supports polish characters ążśźęćńół ĄŻŚŹĘĆŃÓŁ

@kuczek
Copy link
Contributor

kuczek commented Feb 25, 2015

And look at #162 it solve that problem.

@MAXakaWIZARD
Copy link
Contributor

@CharlyPoppins I've added your examples as test cases in #162

@CharlyPoppins
Copy link
Author

Thanks, not used to git yet. So that means I need to wait an "admin" to merge your pull request #162 and then I can do a composer update of that git : "knplabs/doctrine-behaviors": "dev-master".
Am I right ?

@MAXakaWIZARD
Copy link
Contributor

@CharlyPoppins exactly

@CharlyPoppins
Copy link
Author

Ok perfect :) Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants