Issue #2323259 by Sutharsan, vijaycs85, Gábor Hojtsy: Fixed Local translation file detection is not semantic version compatible.
parent
18aa4abddc
commit
53b72740fa
|
@ -60,8 +60,8 @@ class FileTranslation extends StaticTranslation {
|
||||||
* Finds installer translations either for a specific or all languages.
|
* Finds installer translations either for a specific or all languages.
|
||||||
*
|
*
|
||||||
* Filenames must match the pattern:
|
* Filenames must match the pattern:
|
||||||
* - 'drupal-[number].*.[langcode].po
|
* - 'drupal-[version].[langcode].po (if langcode is provided)
|
||||||
* - 'drupal-[number].*.*.po
|
* - 'drupal-[version].*.po (if no langcode is provided)
|
||||||
*
|
*
|
||||||
* @param string $langcode
|
* @param string $langcode
|
||||||
* (optional) The language code corresponding to the language for which we
|
* (optional) The language code corresponding to the language for which we
|
||||||
|
@ -75,10 +75,27 @@ class FileTranslation extends StaticTranslation {
|
||||||
* @see file_scan_directory()
|
* @see file_scan_directory()
|
||||||
*/
|
*/
|
||||||
public function findTranslationFiles($langcode = NULL) {
|
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;
|
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.
|
* Reads the given Gettext PO files into a data structure.
|
||||||
*
|
*
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Definition of Drupal\system\Tests\Installer\InstallTranslationFilePatternTest.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Drupal\system\Tests\Installer;
|
||||||
|
|
||||||
|
use Drupal\Core\StringTranslation\Translator\FileTranslation;
|
||||||
|
use Drupal\Tests\UnitTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for installer language support.
|
||||||
|
*
|
||||||
|
* @group Installer
|
||||||
|
*/
|
||||||
|
class InstallTranslationFilePatternTest extends UnitTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Drupal\Core\StringTranslation\Translator\FileTranslation
|
||||||
|
*/
|
||||||
|
protected $fileTranslation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \ReflectionMethod
|
||||||
|
*/
|
||||||
|
protected $filePatternMethod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setup() {
|
||||||
|
parent::setUp();
|
||||||
|
$this->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'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -27,17 +27,16 @@ class InstallerLanguageDirectionTest extends InstallerTestBase {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected function 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.ar.po', "msgid \"\"\nmsgstr \"\"\nmsgid \"Save and continue\"\nmsgstr \"Save and continue Arabic\"");
|
||||||
|
|
||||||
parent::setUpLanguage();
|
parent::setUpLanguage();
|
||||||
// After selecting a different language than English, all following screens
|
// After selecting a different language than English, all following screens
|
||||||
// should be translated already.
|
// 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');
|
$elements = $this->xpath('//input[@type="submit"]/@value');
|
||||||
$string = (string) current($elements);
|
$this->assertEqual((string) current($elements), 'Save and continue Arabic');
|
||||||
$this->assertNotEqual($string, 'Save and continue');
|
$this->translations['Save and continue'] = 'Save and continue Arabic';
|
||||||
$this->translations['Save and continue'] = $string;
|
|
||||||
|
|
||||||
// Verify that language direction is right-to-left.
|
// Verify that language direction is right-to-left.
|
||||||
$direction = (string) current($this->xpath('/html/@dir'));
|
$direction = (string) current($this->xpath('/html/@dir'));
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* Definition of Drupal\system\Tests\Installer\InstallerLanguageTest.
|
* Contains Drupal\system\Tests\Installer\InstallerLanguageTest.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Drupal\system\Tests\Installer;
|
namespace Drupal\system\Tests\Installer;
|
||||||
|
|
||||||
use Drupal\simpletest\WebTestBase;
|
use Drupal\simpletest\KernelTestBase;
|
||||||
use Drupal\Core\StringTranslation\Translator\FileTranslation;
|
use Drupal\Core\StringTranslation\Translator\FileTranslation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,7 +15,7 @@ use Drupal\Core\StringTranslation\Translator\FileTranslation;
|
||||||
*
|
*
|
||||||
* @group Installer
|
* @group Installer
|
||||||
*/
|
*/
|
||||||
class InstallerLanguageTest extends WebTestBase {
|
class InstallerLanguageTest extends KernelTestBase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that the installer can find translation files.
|
* 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
|
// Different translation files would be found depending on which language
|
||||||
// we are looking for.
|
// we are looking for.
|
||||||
$expected_translation_files = array(
|
$expected_translation_files = array(
|
||||||
NULL => array('drupal-8.0.hu.po', 'drupal-8.0.de.po'),
|
NULL => array('drupal-8.0.0-beta2.hu.po', 'drupal-8.0.0.de.po'),
|
||||||
'de' => array('drupal-8.0.de.po'),
|
'de' => array('drupal-8.0.0.de.po'),
|
||||||
'hu' => array('drupal-8.0.hu.po'),
|
'hu' => array('drupal-8.0.0-beta2.hu.po'),
|
||||||
'it' => array(),
|
'it' => array(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -28,17 +28,16 @@ class InstallerTranslationTest extends InstallerTestBase {
|
||||||
* Overrides InstallerTest::setUpLanguage().
|
* Overrides InstallerTest::setUpLanguage().
|
||||||
*/
|
*/
|
||||||
protected function 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();
|
parent::setUpLanguage();
|
||||||
// After selecting a different language than English, all following screens
|
// After selecting a different language than English, all following screens
|
||||||
// should be translated already.
|
// 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');
|
$elements = $this->xpath('//input[@type="submit"]/@value');
|
||||||
$string = (string) current($elements);
|
$this->assertEqual((string) current($elements), 'Save and continue German');
|
||||||
$this->assertNotEqual($string, 'Save and continue');
|
$this->translations['Save and continue'] = 'Save and continue German';
|
||||||
$this->translations['Save and continue'] = $string;
|
|
||||||
|
|
||||||
// Check the language direction.
|
// Check the language direction.
|
||||||
$direction = (string) current($this->xpath('/html/@dir'));
|
$direction = (string) current($this->xpath('/html/@dir'));
|
||||||
|
|
Loading…
Reference in New Issue