diff --git a/core/lib/Drupal/Component/Transliteration/PHPTransliteration.php b/core/lib/Drupal/Component/Transliteration/PHPTransliteration.php index 7162325..a495ee8 100644 --- a/core/lib/Drupal/Component/Transliteration/PHPTransliteration.php +++ b/core/lib/Drupal/Component/Transliteration/PHPTransliteration.php @@ -2,7 +2,7 @@ /** * @file - * Definition of \Drupal\Component\Transliteration\PhpTransliteration. + * Definition of \Drupal\Component\Transliteration\PHPTransliteration. * * Some parts of this code were derived from the MediaWiki project's UtfNormal * class, Copyright © 2004 Brion Vibber , @@ -15,20 +15,16 @@ * Implements transliteration without using the PECL extensions. * * Transliterations are done character-by-character, by looking up non-US-ASCII - * characters in a transliteration database. The database comes from two types - * of files, both of which are searched for in the - * PHPTransliteration::$dataDirectory directory. First, language-specific - * overrides are searched (see PHPTranslation::readLanguageOverrides() for - * details of these files). If there is no language-specific override for a - * character, the generic transliteration character tables are searched (see - * PHPTranslation::readGenericData() for details of these files). If looking up - * the character in the generic table results in a NULL value, or an illegal - * character is encountered, then a substitute character is returned. + * characters in a transliteration database. * - * This class is the registered transliteration class returned from - * drupal_container()->get('transliteration') by default. - * - * @ingroup transliteration + * The database comes from two types of files, both of which are searched for in + * the PHPTransliteration::$dataDirectory directory. First, language-specific + * overrides are searched (see PHPTransliteration::readLanguageOverrides()). If + * there is no language-specific override for a character, the generic + * transliteration character tables are searched (see + * PHPTransliteration::readGenericData()). If looking up the character in the + * generic table results in a NULL value, or an illegal character is + * encountered, then a substitute character is returned. */ class PHPTransliteration implements TransliterationInterface { @@ -68,13 +64,6 @@ class PHPTransliteration implements TransliterationInterface { protected $genericMap = array(); /** - * Returns this PHPTransliteration object (for the Drupal Container). - */ - public function get() { - return $this; - } - - /** * Constructs a transliteration object. * * @param string $data_directory @@ -83,7 +72,6 @@ public function get() { * file resides. */ public function __construct($data_directory = NULL) { - // Set up data directory and tail bytes table. $this->dataDirectory = (isset($data_directory)) ? $data_directory : __DIR__ . '/data'; } @@ -184,9 +172,7 @@ protected function replace($code, $langcode, $unknown_character) { * PHPTransliteration::$dataDirectory. These files should set up an array * variable $overrides with an element whose key is $langcode and whose value * is an array whose keys are character codes, and whose values are their - * transliterations in this language. The resulting $overrides array is - * altered by invoking hook_transliteration_overrides_alter() to let modules - * add additional overrides. + * transliterations in this language. * * @param $langcode * Code for the language to read. @@ -199,14 +185,11 @@ protected function readLanguageOverrides($langcode) { // Read in this file, which should set up a variable called $overrides, // which will be local to this function. if (is_file($file)) { - include($file); + include $file; } if (!isset($overrides) || !is_array($overrides)) { $overrides = array($langcode => array()); } - - // Let modules alter the list, and save it. - drupal_alter('transliteration_overrides', $overrides, $langcode); $this->languageOverrides[$langcode] = $overrides[$langcode]; } @@ -229,7 +212,7 @@ protected function readGenericData($bank) { // Read in this file, which should set up a variable called $base, which // will be local to this function. if (is_file($file)) { - include($file); + include $file; } if (!isset($base) || !is_array($base)) { $base = array(); diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index 48684e5..0c2f655 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -129,7 +129,8 @@ public function build(ContainerBuilder $container) { ->setFactoryClass('Drupal\Core\ExceptionController') ->setFactoryMethod('getExceptionListener'); - $container->register('transliteration', 'Drupal\Component\Transliteration\PHPTransliteration'); + $container + ->register('transliteration', 'Drupal\Core\Transliteration\PHPTransliteration'); // Add Serializer with arguments to be replaced in the compiler pass. $container->register('serializer', 'Symfony\Component\Serializer\Serializer') diff --git a/core/lib/Drupal/Core/Transliteration/PHPTransliteration.php b/core/lib/Drupal/Core/Transliteration/PHPTransliteration.php new file mode 100644 index 0000000..951ce1e --- /dev/null +++ b/core/lib/Drupal/Core/Transliteration/PHPTransliteration.php @@ -0,0 +1,32 @@ +languageOverrides[$langcode], $langcode); + } + +} diff --git a/core/modules/system/language.api.php b/core/modules/system/language.api.php index 767d932..eb8bc3c 100644 --- a/core/modules/system/language.api.php +++ b/core/modules/system/language.api.php @@ -228,11 +228,10 @@ function hook_language_fallback_candidates_alter(array &$fallback_candidates) { */ /** - * Provide language overrides for transliteration. + * Provide language-specific overrides for transliteration. * * @param array $overrides - * Associative array of language overrides. The outermost key is the language - * code, and the corresponding value is an array whose keys are integer + * Associative array of language-specific overrides whose keys are integer * Unicode character codes, and whose values are the transliterations of those * characters in the given language, to override default transliterations. * @param string $langcode @@ -244,7 +243,7 @@ function hook_transliteration_overrides_alter(&$overrides, $langcode) { // Provide special overrides for German for a custom site. if ($langcode == 'de') { // The core-provided transliteration of Ä is Ae, but we want just A. - $overrides['de'][0xC4] = 'A'; + $overrides[0xC4] = 'A'; } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Transliteration/TransliterationTest.php b/core/modules/system/lib/Drupal/system/Tests/Transliteration/TransliterationTest.php index dcc3709..d717d8b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Transliteration/TransliterationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Transliteration/TransliterationTest.php @@ -7,15 +7,13 @@ namespace Drupal\system\Tests\Transliteration; -use Drupal\Component\Transliteration\PHPTransliteration; -use Drupal\simpletest\WebTestBase; +use Drupal\Core\Transliteration\PHPTransliteration; +use Drupal\simpletest\DrupalUnitTestBase; /** - * Tests the transliteration class. - * - * We need this to be a WebTestBase class because it uses drupal_container(). + * Tests Transliteration component functionality. */ -class TransliterationTest extends WebTestBase { +class TransliterationTest extends DrupalUnitTestBase { /** * Modules to enable. * @@ -26,7 +24,7 @@ class TransliterationTest extends WebTestBase { public static function getInfo() { return array( 'name' => 'Transliteration functionality', - 'description' => 'Tests the transliteration component', + 'description' => 'Tests Transliteration component functionality.', 'group' => 'Transliteration', ); } @@ -76,16 +74,26 @@ public function testPHPTransliteration() { // Test each case both with a new instance of the transliteration class, // and with one that builds as it goes. - $common_transliterator = drupal_container()->get('transliteration'); + $transliterator_service = $this->container->get('transliteration'); foreach($cases as $case) { - list($langcode, $before, $after) = $case; - $transliterator = new PHPTransliteration(); - $actual = $transliterator->transliterate($before, $langcode); - $this->assertEqual($after, $actual, format_string('@before is correctly transliterated to @after in new class (@actual) in language @langcode', array('@before' => $before, '@langcode' => $langcode, '@after' => $after, '@actual' => $actual))); + list($langcode, $original, $expected) = $case; + $transliterator_class = new PHPTransliteration(); + $actual = $transliterator_class->transliterate($original, $langcode); + $this->assertIdentical($actual, $expected, format_string('@original transliteration to @actual is identical to @expected for language @langcode in new class instance.', array( + '@original' => $original, + '@langcode' => $langcode, + '@expected' => $expected, + '@actual' => $actual, + ))); - $actual = $common_transliterator->transliterate($before, $langcode); - $this->assertEqual($after, $actual, format_string('@before is correctly transliterated to @after in previously-used class (@actual) in language @langcode', array('@before' => $before, '@langcode' => $langcode, '@after' => $after, '@actual' => $actual))); + $actual = $transliterator_service->transliterate($original, $langcode); + $this->assertIdentical($actual, $expected, format_string('@original transliteration to @actual is identical to @expected for language @langcode in service instance.', array( + '@original' => $original, + '@langcode' => $langcode, + '@expected' => $expected, + '@actual' => $actual, + ))); } } } diff --git a/core/modules/system/tests/modules/transliterate_test/transliterate_test.info b/core/modules/system/tests/modules/transliterate_test/transliterate_test.info index 8325b2f..c6193f6 100644 --- a/core/modules/system/tests/modules/transliterate_test/transliterate_test.info +++ b/core/modules/system/tests/modules/transliterate_test/transliterate_test.info @@ -1,5 +1,5 @@ -name = "Transliteration test module" -description = "Tests the transliteration hook" +name = "Transliteration test" +description = "Helper module for Transliteration system tests." package = Testing version = VERSION core = 8.x diff --git a/core/modules/system/tests/modules/transliterate_test/transliterate_test.module b/core/modules/system/tests/modules/transliterate_test/transliterate_test.module index 59e1149..ee71069 100644 --- a/core/modules/system/tests/modules/transliterate_test/transliterate_test.module +++ b/core/modules/system/tests/modules/transliterate_test/transliterate_test.module @@ -2,22 +2,15 @@ /** * @file - * Test module for Transliteration hook. - * - * @see hook_transliteration_overrides_alter() + * Test module for Transliteration system. */ /** * Implements hook_transliteration_overrides_alter(). - * - * Provides overrides for a fake language with language code 'zz'. */ function transliterate_test_transliteration_overrides_alter(&$overrides, $langcode) { if ($langcode == 'zz') { // The default transliteration of Ä is A, but change it to Z for testing. - $overrides['zz'][0xC4] = 'Z'; - } - else { - $overrides['zz'][0xC4] = 'W'; + $overrides[0xC4] = 'Z'; } }