From 53b72740fa3d3870c62b470e98e66357e5ece0cf Mon Sep 17 00:00:00 2001 From: webchick Date: Mon, 25 Aug 2014 22:33:10 -0700 Subject: [PATCH] =?UTF-8?q?Issue=20#2323259=20by=20Sutharsan,=20vijaycs85,?= =?UTF-8?q?=20G=C3=A1bor=20Hojtsy:=20Fixed=20Local=20translation=20file=20?= =?UTF-8?q?detection=20is=20not=20semantic=20version=20compatible.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Translator/FileTranslation.php | 23 +++++- ...pal-8.0.de.po => drupal-8.0.0-beta2.hu.po} | 0 .../{drupal-8.0.hu.po => drupal-8.0.0.de.po} | 0 .../InstallTranslationFilePatternTest.php | 81 +++++++++++++++++++ .../InstallerLanguageDirectionTest.php | 13 ++- .../Tests/Installer/InstallerLanguageTest.php | 12 +-- .../Installer/InstallerTranslationTest.php | 13 ++- 7 files changed, 119 insertions(+), 23 deletions(-) rename core/modules/simpletest/files/translations/{drupal-8.0.de.po => drupal-8.0.0-beta2.hu.po} (100%) rename core/modules/simpletest/files/translations/{drupal-8.0.hu.po => drupal-8.0.0.de.po} (100%) create mode 100644 core/modules/system/src/Tests/Installer/InstallTranslationFilePatternTest.php diff --git a/core/lib/Drupal/Core/StringTranslation/Translator/FileTranslation.php b/core/lib/Drupal/Core/StringTranslation/Translator/FileTranslation.php index ecd5a61634e4..5a633aeae741 100644 --- a/core/lib/Drupal/Core/StringTranslation/Translator/FileTranslation.php +++ b/core/lib/Drupal/Core/StringTranslation/Translator/FileTranslation.php @@ -60,8 +60,8 @@ class FileTranslation extends StaticTranslation { * Finds installer translations either for a specific or all languages. * * Filenames must match the pattern: - * - 'drupal-[number].*.[langcode].po - * - 'drupal-[number].*.*.po + * - 'drupal-[version].[langcode].po (if langcode is provided) + * - 'drupal-[version].*.po (if no langcode is provided) * * @param string $langcode * (optional) The language code corresponding to the language for which we @@ -75,10 +75,27 @@ class FileTranslation extends StaticTranslation { * @see file_scan_directory() */ public function findTranslationFiles($langcode = NULL) { - $files = file_scan_directory($this->directory, '!drupal-\d+\.[^\.]+\.' . (!empty($langcode) ? preg_quote($langcode, '!') : '[^\.]+') . '\.po$!', array('recurse' => FALSE)); + $files = file_scan_directory($this->directory, $this->getTranslationFilesPattern($langcode), array('recurse' => FALSE)); return $files; } + /** + * Provides translation file name pattern. + * + * @param string $langcode + * (optional) The language code corresponding to the language for which we + * want to find translation files. + * + * @return string + * String file pattern. + */ + protected function getTranslationFilesPattern($langcode = NULL) { + // The file name matches: drupal-[release version].[language code].po + // When provided the $langcode is use as language code. If not provided all + // language codes will match. + return '!drupal-[0-9a-z\.-]+\.' . (!empty($langcode) ? preg_quote($langcode, '!') : '[^\.]+') . '\.po$!'; + } + /** * Reads the given Gettext PO files into a data structure. * diff --git a/core/modules/simpletest/files/translations/drupal-8.0.de.po b/core/modules/simpletest/files/translations/drupal-8.0.0-beta2.hu.po similarity index 100% rename from core/modules/simpletest/files/translations/drupal-8.0.de.po rename to core/modules/simpletest/files/translations/drupal-8.0.0-beta2.hu.po diff --git a/core/modules/simpletest/files/translations/drupal-8.0.hu.po b/core/modules/simpletest/files/translations/drupal-8.0.0.de.po similarity index 100% rename from core/modules/simpletest/files/translations/drupal-8.0.hu.po rename to core/modules/simpletest/files/translations/drupal-8.0.0.de.po diff --git a/core/modules/system/src/Tests/Installer/InstallTranslationFilePatternTest.php b/core/modules/system/src/Tests/Installer/InstallTranslationFilePatternTest.php new file mode 100644 index 000000000000..51b3e477a84c --- /dev/null +++ b/core/modules/system/src/Tests/Installer/InstallTranslationFilePatternTest.php @@ -0,0 +1,81 @@ +fileTranslation = new FileTranslation('filename'); + $method = new \ReflectionMethod('\Drupal\Core\StringTranslation\Translator\FileTranslation', 'getTranslationFilesPattern'); + $method->setAccessible(true); + $this->filePatternMethod = $method; + } + + /** + * @dataProvider providerValidTranslationFiles + */ + public function testFilesPatternValid($langcode, $filename) { + $pattern = $this->filePatternMethod->invoke($this->fileTranslation, $langcode); + $this->assertNotEmpty(preg_match($pattern, $filename)); + } + + /** + * @return array + */ + public function providerValidTranslationFiles() { + return array( + array('hu', 'drupal-8.0.0-alpha1.hu.po'), + array('ta', 'drupal-8.10.10-beta12.ta.po'), + array('hi', 'drupal-8.0.0.hi.po'), + ); + } + + /** + * @dataProvider providerInvalidTranslationFiles + */ + public function testFilesPatternInvalid($langcode, $filename) { + $pattern = $this->filePatternMethod->invoke($this->fileTranslation, $langcode); + $this->assertEmpty(preg_match($pattern, $filename)); + } + + /** + * @return array + */ + public function providerInvalidTranslationFiles() { + return array( + array('hu', 'drupal-alpha1-*-hu.po'), + array('ta', 'drupal-beta12.ta'), + array('hi', 'drupal-hi.po'), + array('de', 'drupal-dummy-de.po'), + array('hu', 'drupal-10.0.1.alpha1-hu.po'), + ); + } + +} diff --git a/core/modules/system/src/Tests/Installer/InstallerLanguageDirectionTest.php b/core/modules/system/src/Tests/Installer/InstallerLanguageDirectionTest.php index 74b8be28d967..cd7bce5168cc 100644 --- a/core/modules/system/src/Tests/Installer/InstallerLanguageDirectionTest.php +++ b/core/modules/system/src/Tests/Installer/InstallerLanguageDirectionTest.php @@ -27,17 +27,16 @@ class InstallerLanguageDirectionTest extends InstallerTestBase { * {@inheritdoc} */ protected function setUpLanguage() { + // Place a custom local translation in the translations directory. + mkdir(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE); + file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.ar.po', "msgid \"\"\nmsgstr \"\"\nmsgid \"Save and continue\"\nmsgstr \"Save and continue Arabic\""); + parent::setUpLanguage(); // After selecting a different language than English, all following screens // should be translated already. - // @todo Instead of actually downloading random translations that cannot be - // asserted, write and supply a translation file. Until then, take - // over whichever string happens to be there, but ensure that the English - // string no longer appears. $elements = $this->xpath('//input[@type="submit"]/@value'); - $string = (string) current($elements); - $this->assertNotEqual($string, 'Save and continue'); - $this->translations['Save and continue'] = $string; + $this->assertEqual((string) current($elements), 'Save and continue Arabic'); + $this->translations['Save and continue'] = 'Save and continue Arabic'; // Verify that language direction is right-to-left. $direction = (string) current($this->xpath('/html/@dir')); diff --git a/core/modules/system/src/Tests/Installer/InstallerLanguageTest.php b/core/modules/system/src/Tests/Installer/InstallerLanguageTest.php index b32ef40b433a..a025b35226c4 100644 --- a/core/modules/system/src/Tests/Installer/InstallerLanguageTest.php +++ b/core/modules/system/src/Tests/Installer/InstallerLanguageTest.php @@ -2,12 +2,12 @@ /** * @file - * Definition of Drupal\system\Tests\Installer\InstallerLanguageTest. + * Contains Drupal\system\Tests\Installer\InstallerLanguageTest. */ namespace Drupal\system\Tests\Installer; -use Drupal\simpletest\WebTestBase; +use Drupal\simpletest\KernelTestBase; use Drupal\Core\StringTranslation\Translator\FileTranslation; /** @@ -15,7 +15,7 @@ use Drupal\Core\StringTranslation\Translator\FileTranslation; * * @group Installer */ -class InstallerLanguageTest extends WebTestBase { +class InstallerLanguageTest extends KernelTestBase { /** * Tests that the installer can find translation files. @@ -24,9 +24,9 @@ class InstallerLanguageTest extends WebTestBase { // Different translation files would be found depending on which language // we are looking for. $expected_translation_files = array( - NULL => array('drupal-8.0.hu.po', 'drupal-8.0.de.po'), - 'de' => array('drupal-8.0.de.po'), - 'hu' => array('drupal-8.0.hu.po'), + NULL => array('drupal-8.0.0-beta2.hu.po', 'drupal-8.0.0.de.po'), + 'de' => array('drupal-8.0.0.de.po'), + 'hu' => array('drupal-8.0.0-beta2.hu.po'), 'it' => array(), ); diff --git a/core/modules/system/src/Tests/Installer/InstallerTranslationTest.php b/core/modules/system/src/Tests/Installer/InstallerTranslationTest.php index 8ebf010db855..5a24f2771455 100644 --- a/core/modules/system/src/Tests/Installer/InstallerTranslationTest.php +++ b/core/modules/system/src/Tests/Installer/InstallerTranslationTest.php @@ -28,17 +28,16 @@ class InstallerTranslationTest extends InstallerTestBase { * Overrides InstallerTest::setUpLanguage(). */ protected function setUpLanguage() { + // Place a custom local translation in the translations directory. + mkdir(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE); + file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', "msgid \"\"\nmsgstr \"\"\nmsgid \"Save and continue\"\nmsgstr \"Save and continue German\""); + parent::setUpLanguage(); // After selecting a different language than English, all following screens // should be translated already. - // @todo Instead of actually downloading random translations that cannot be - // asserted, write and supply a German translation file. Until then, take - // over whichever string happens to be there, but ensure that the English - // string no longer appears. $elements = $this->xpath('//input[@type="submit"]/@value'); - $string = (string) current($elements); - $this->assertNotEqual($string, 'Save and continue'); - $this->translations['Save and continue'] = $string; + $this->assertEqual((string) current($elements), 'Save and continue German'); + $this->translations['Save and continue'] = 'Save and continue German'; // Check the language direction. $direction = (string) current($this->xpath('/html/@dir'));