Drupal 8 multilingual tidbits 8: transliteration

Gábor Hojtsy 1 min read

Up to date as of October 16th, 2015.

We went through most of the great new base language features in Drupal 8, but one thing that was not yet covered is built-in transliteration support. Yes, you read that right. Drupal 8 includes a powerful backend system for transliteration and it even uses it for machine name generation on the frontend.

Here are a few examples on how Drupal prefills the machine name for a content type for a Hungarian, Czech and Marathi type respectively:

Image
Image
Image

You don't even need to have language module enabled to use the transliteration services. However language specific transliteration patterns are supported, and the overrides are alterable. See https://drupal.org/node/1842748 for documentation on how to tap into this new API.

While this concludes the list of new base language features, that is only one of the language related modules in core. The tidbits are far from over! Excited about Drupal 8 yet?

Issues to work on

Comments 7

matrixlord (not verified) Tue, 07/22/2014 - 12:25 Permalink

Very good! Is there an accessible API function that you can transliterate text on demand like in the transliteration module? Thanks!

<?php
use Drupal\Core\Transliteration\PHPTransliteration;
$transliterator = new PHPTransliteration();
$transliterated = $transliterator->transliterate('Árvíztűrő tükörfúrógép', 'hu');
?>

Hi it seems like the constructur needs the second argument so the code


$transliterator = new PHPTransliteration();

don't work.

Right, it may have worked in 2014 when I posted about it :) Looks like now there is a transliteration class in Core which does require the module handler and then there is a transliteration class in Component that does not. The difference is support for an alter hook. You need to pass in the module handler to the Core one. So that would become:

<?php
use Drupal\Core\Transliteration\PHPTransliteration;
$transliterator = new PHPTransliteration(NULL, \Drupal::moduleHandler());
$transliterated = $transliterator->transliterate('Árvíztűrő tükörfúrógép', 'hu');
?>

In a global scope at least.

Hennie (not verified) Wed, 05/03/2017 - 22:50 Permalink

Thanks for this article. We're busy with a Hungarian site and I need to replace the accents in URLs, how can I do this? With D7 you would use pathauto and transliteration but with D8 I do not know. Any suggestions?

Anish Sheela (not verified) Fri, 02/02/2018 - 06:05 Permalink

Transliteration for Indian languages needs improvement though. I will try to contribute that.