diff --git a/core/modules/locale/locale.translation.js b/core/modules/locale/locale.translation.js new file mode 100644 index 0000000..6d611a0 --- /dev/null +++ b/core/modules/locale/locale.translation.js @@ -0,0 +1 @@ +// This file is a placeholder and dynamically replaced in locale_js_alter(). diff --git a/core/modules/locale/src/Tests/LocaleJavascriptTranslationTest.php b/core/modules/locale/src/Tests/LocaleJavascriptTranslationTest.php index 6d6917a..e0e17b8 100644 --- a/core/modules/locale/src/Tests/LocaleJavascriptTranslationTest.php +++ b/core/modules/locale/src/Tests/LocaleJavascriptTranslationTest.php @@ -7,6 +7,7 @@ namespace Drupal\locale\Tests; +use Drupal\Core\Language\LanguageInterface; use Drupal\simpletest\WebTestBase; use Drupal\Component\Utility\String; @@ -93,4 +94,68 @@ public function testFileParsing() { $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.'); } + + /** + * Ensure that the translations js is added after its dependencies. + */ + public function testLocaleTranslationJsDependencies() { + // User to add and remove language. + $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'translate interface')); + + // Add custom language. + $this->drupalLogin($admin_user); + // Code for the language. + $langcode = 'es'; + // The English name for the language. + $name = $this->randomMachineName(16); + // The domain prefix. + $prefix = $langcode; + $edit = array( + 'predefined_langcode' => 'custom', + 'langcode' => $langcode, + 'label' => $name, + 'direction' => LanguageInterface::DIRECTION_LTR, + ); + $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); + + // Set path prefix. + $edit = array("prefix[$langcode]" => $prefix); + $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); + + // This forces locale.admin.js string sources to be imported, which contains + // the next translation. + $this->drupalGet($prefix . '/admin/config/regional/translate'); + + // Translate a string in locale.admin.js to langcode. + $strings = \Drupal::service('locale.storage') + ->getStrings(array( + 'source' => 'Show description', + 'type' => 'javascript', + 'name' => 'core/modules/locale/locale.admin.js', + )); + $string = $strings[0]; + + // Translate a string. + $this->drupalPostForm(NULL, ['string' => 'Show description'], t('Filter')); + $edit = ['strings[' . $string->lid . '][translations][0]' => $this->randomString(16)]; + $this->drupalPostForm(NULL, $edit, t('Save translations')); + + // Loading the Translate page should refresh the translations js and import + // the sources. + $this->drupalGet($prefix . '/admin/config/regional/translate'); + + // We need the generated hash in the js filename. + $js_translation_files = \Drupal::state()->get('locale.translation.javascript'); + $js_filename = $prefix . '_' . $js_translation_files[$prefix] . '.js'; + + // Get the src of the script previous to drupal.js which contains the + // translations. + $xpath = '//script[contains(@src, "drupal.js")]/preceding-sibling::script[contains(@src, "' . $js_filename . '")]/@src'; + $elements = $this->xpath($xpath); + $filename = explode('?', basename(((string)($elements[0]['src']))))[0]; + + // If the script was there, translations are included successfully. + $this->assertEqual($filename, $js_filename); + } + }