Change record status: 
Project: 
Introduced in branch: 
8.x
Introduced in version: 
8.x
Description: 

The basic functionality of the contributed Transliteration module has been added to Drupal Core.

Added classes/interfaces:
\Drupal\Component\Transliteration\TransliterationInterface
\Drupal\Component\Transliteration\PHPTransliteration
\Drupal\Core\Transliteration\PHPTransliteration

The core/component transliteration classes do basic character-by-character transliteration using a database of generic character transliterations and language-specific overrides, which is OK for basic uses such as creating legal file names and machine names, but not good for transliterating prose (which would require consideration of context, capitalization, etc.).

To use this class from the Drupal container:

// Use the current default interface language.
$langcode = Drupal::languageManager()->getLanguage()->langcode;
// Instantiate the transliteration class.
$trans = Drupal::service('transliteration');
// Use this to transliterate some text.
$transformed = $trans->transliterate($string, $langcode);
// Limit the result to $max characters, making sure to stop on an input
// character boundary
$transformed = $trans->transliterate($string, $langcode, $max);

Or you can instantiate the class directly:

use Drupal\Component\Transliteration\PhpTransliteration;

$trans = new PhpTransliteration();
$transformed = $trans->transliterate($string, $langcode);
// Or:
$transformed = $trans->transliterate($string, $langcode, $max);

There is also a new hook that modules can implement to provide language-specific overrides of individual characters' transliterations: hook_transliteration_overrides_alter(). This hook is only invoked from the Core class, not the Component class (components are Drupalism-independent).

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

flocondetoile’s picture

For instantiate the class directly, you have to use Drupal\Component

use Drupal\Component\Transliteration\PhpTransliteration;

$langcode = Drupal::languageManager()->getLanguage()->langcode;
$trans = new PHPTransliteration();
$transformed = $trans->transliterate($string, $langcode);
// Or:
$transformed = $trans->transliterate($string, $langcode, $max);
// For generate clean url
$clean_url =  preg_replace('/\-+/', '-', strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '', str_replace(' ', '-', $transformed))));