Search code examples
cakephpdiacriticsslugcakephp-3.x

How to make Text::slug() convert german umlauts properly?


I am using CakePHP 3.6, and when I am using words with german umlauts like:

Text::slug('Grundstücke')

I will get:

Grundstucke (where ü = u)

but that's not correct, I should get:

Grundstuecke (where ü = ue)

Is there an option to set so that umlauts are being converted the way I want them to?


Solution

  • Change your transliterator

    The Text::slug() uses internally transliterator_transliterate (see php doc).

    So you need to change the default transliterator that is being used.

    After some research I found one that will work for you.

    At the end of your bootstrap.php file add:

    \Cake\Utility\Text::setTransliteratorId( 'de-ASCII; Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove ');
    

    Then your text will be converted as you expect.

    Notes

    Resources I've used to find this answer: