3 namespace Drupal\Tests\locale\Functional;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Component\Utility\SafeMarkup;
10 * Tests parsing js files for translatable strings.
14 class LocaleJavascriptTranslationTest extends BrowserTestBase {
21 public static $modules = ['locale', 'locale_test'];
23 public function testFileParsing() {
25 // This test is for ensuring that the regular expression in
26 // _locale_parse_js_file() finds translatable source strings in all valid
27 // JavaScript syntax regardless of the coding style used, especially with
28 // respect to optional whitespace, line breaks, etc.
29 // - We test locale_test.es6.js, because that is the one that contains a
30 // variety of whitespace styles.
31 // - We also test the transpiled locale_test.js as an extra double-check
32 // that JavaScript transpilation doesn't change what
33 // _locale_parse_js_file() finds.
34 $files[] = __DIR__ . '/../../locale_test.es6.js';
35 $files[] = __DIR__ . '/../../locale_test.js';
37 foreach ($files as $filename) {
38 // Parse the file to look for source strings.
39 _locale_parse_js_file($filename);
41 // Get all of the source strings that were found.
42 $strings = $this->container
43 ->get('locale.storage')
45 'type' => 'javascript',
50 foreach ($strings as $string) {
51 $source_strings[$string->source] = $string->context;
54 $etx = LOCALE_PLURAL_DELIMITER;
55 // List of all strings that should be in the file.
57 'Standard Call t' => '',
58 'Whitespace Call t' => '',
60 'Single Quote t' => '',
61 "Single Quote \\'Escaped\\' t" => '',
62 'Single Quote Concat strings t' => '',
64 'Double Quote t' => '',
65 "Double Quote \\\"Escaped\\\" t" => '',
66 'Double Quote Concat strings t' => '',
68 'Context !key Args t' => 'Context string',
70 'Context Unquoted t' => 'Context string unquoted',
71 'Context Single Quoted t' => 'Context string single quoted',
72 'Context Double Quoted t' => 'Context string double quoted',
74 "Standard Call plural{$etx}Standard Call @count plural" => '',
75 "Whitespace Call plural{$etx}Whitespace Call @count plural" => '',
77 "Single Quote plural{$etx}Single Quote @count plural" => '',
78 "Single Quote \\'Escaped\\' plural{$etx}Single Quote \\'Escaped\\' @count plural" => '',
80 "Double Quote plural{$etx}Double Quote @count plural" => '',
81 "Double Quote \\\"Escaped\\\" plural{$etx}Double Quote \\\"Escaped\\\" @count plural" => '',
83 "Context !key Args plural{$etx}Context !key Args @count plural" => 'Context string',
85 "Context Unquoted plural{$etx}Context Unquoted @count plural" => 'Context string unquoted',
86 "Context Single Quoted plural{$etx}Context Single Quoted @count plural" => 'Context string single quoted',
87 "Context Double Quoted plural{$etx}Context Double Quoted @count plural" => 'Context string double quoted',
90 // Assert that all strings were found properly.
91 foreach ($test_strings as $str => $context) {
92 $args = ['%source' => $str, '%context' => $context];
94 // Make sure that the string was found in the file.
95 $this->assertTrue(isset($source_strings[$str]), SafeMarkup::format('Found source string: %source', $args));
97 // Make sure that the proper context was matched.
98 $message = $context ? SafeMarkup::format('Context for %source is %context', $args) : SafeMarkup::format('Context for %source is blank', $args);
99 $this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, $message);
102 $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.');
107 * Assert translations JS is added before drupal.js, because it depends on it.
109 public function testLocaleTranslationJsDependencies() {
110 // User to add and remove language.
111 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface']);
113 // Add custom language.
114 $this->drupalLogin($admin_user);
115 // Code for the language.
117 // The English name for the language.
118 $name = $this->randomMachineName(16);
119 // The domain prefix.
122 'predefined_langcode' => 'custom',
123 'langcode' => $langcode,
125 'direction' => LanguageInterface::DIRECTION_LTR,
127 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
130 $edit = ["prefix[$langcode]" => $prefix];
131 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
133 // This forces locale.admin.js string sources to be imported, which contains
134 // the next translation.
135 $this->drupalGet($prefix . '/admin/config/regional/translate');
137 // Translate a string in locale.admin.js to our new language.
138 $strings = \Drupal::service('locale.storage')
140 'source' => 'Show description',
141 'type' => 'javascript',
142 'name' => 'core/modules/locale/locale.admin.js',
144 $string = $strings[0];
146 $this->drupalPostForm(NULL, ['string' => 'Show description'], t('Filter'));
147 $edit = ['strings[' . $string->lid . '][translations][0]' => $this->randomString(16)];
148 $this->drupalPostForm(NULL, $edit, t('Save translations'));
150 // Calculate the filename of the JS including the translations.
151 $js_translation_files = \Drupal::state()->get('locale.translation.javascript');
152 $js_filename = $prefix . '_' . $js_translation_files[$prefix] . '.js';
154 $content = $this->getSession()->getPage()->getContent();
155 // Assert translations JS is included before drupal.js.
156 $this->assertTrue(strpos($content, $js_filename) < strpos($content, 'core/misc/drupal.js'), 'Translations are included before Drupal.t.');