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() {
24 $filename = __DIR__ . '/../../locale_test.js';
26 // Parse the file to look for source strings.
27 _locale_parse_js_file($filename);
29 // Get all of the source strings that were found.
30 $strings = $this->container
31 ->get('locale.storage')
33 'type' => 'javascript',
38 foreach ($strings as $string) {
39 $source_strings[$string->source] = $string->context;
42 $etx = LOCALE_PLURAL_DELIMITER;
43 // List of all strings that should be in the file.
45 'Standard Call t' => '',
46 'Whitespace Call t' => '',
48 'Single Quote t' => '',
49 "Single Quote \\'Escaped\\' t" => '',
50 'Single Quote Concat strings t' => '',
52 'Double Quote t' => '',
53 "Double Quote \\\"Escaped\\\" t" => '',
54 'Double Quote Concat strings t' => '',
56 'Context !key Args t' => 'Context string',
58 'Context Unquoted t' => 'Context string unquoted',
59 'Context Single Quoted t' => 'Context string single quoted',
60 'Context Double Quoted t' => 'Context string double quoted',
62 "Standard Call plural{$etx}Standard Call @count plural" => '',
63 "Whitespace Call plural{$etx}Whitespace Call @count plural" => '',
65 "Single Quote plural{$etx}Single Quote @count plural" => '',
66 "Single Quote \\'Escaped\\' plural{$etx}Single Quote \\'Escaped\\' @count plural" => '',
68 "Double Quote plural{$etx}Double Quote @count plural" => '',
69 "Double Quote \\\"Escaped\\\" plural{$etx}Double Quote \\\"Escaped\\\" @count plural" => '',
71 "Context !key Args plural{$etx}Context !key Args @count plural" => 'Context string',
73 "Context Unquoted plural{$etx}Context Unquoted @count plural" => 'Context string unquoted',
74 "Context Single Quoted plural{$etx}Context Single Quoted @count plural" => 'Context string single quoted',
75 "Context Double Quoted plural{$etx}Context Double Quoted @count plural" => 'Context string double quoted',
78 // Assert that all strings were found properly.
79 foreach ($test_strings as $str => $context) {
80 $args = ['%source' => $str, '%context' => $context];
82 // Make sure that the string was found in the file.
83 $this->assertTrue(isset($source_strings[$str]), SafeMarkup::format('Found source string: %source', $args));
85 // Make sure that the proper context was matched.
86 $message = $context ? SafeMarkup::format('Context for %source is %context', $args) : SafeMarkup::format('Context for %source is blank', $args);
87 $this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, $message);
90 $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.');
94 * Assert translations JS is added before drupal.js, because it depends on it.
96 public function testLocaleTranslationJsDependencies() {
97 // User to add and remove language.
98 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface']);
100 // Add custom language.
101 $this->drupalLogin($admin_user);
102 // Code for the language.
104 // The English name for the language.
105 $name = $this->randomMachineName(16);
106 // The domain prefix.
109 'predefined_langcode' => 'custom',
110 'langcode' => $langcode,
112 'direction' => LanguageInterface::DIRECTION_LTR,
114 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
117 $edit = ["prefix[$langcode]" => $prefix];
118 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
120 // This forces locale.admin.js string sources to be imported, which contains
121 // the next translation.
122 $this->drupalGet($prefix . '/admin/config/regional/translate');
124 // Translate a string in locale.admin.js to our new language.
125 $strings = \Drupal::service('locale.storage')
127 'source' => 'Show description',
128 'type' => 'javascript',
129 'name' => 'core/modules/locale/locale.admin.js',
131 $string = $strings[0];
133 $this->drupalPostForm(NULL, ['string' => 'Show description'], t('Filter'));
134 $edit = ['strings[' . $string->lid . '][translations][0]' => $this->randomString(16)];
135 $this->drupalPostForm(NULL, $edit, t('Save translations'));
137 // Calculate the filename of the JS including the translations.
138 $js_translation_files = \Drupal::state()->get('locale.translation.javascript');
139 $js_filename = $prefix . '_' . $js_translation_files[$prefix] . '.js';
141 $content = $this->getSession()->getPage()->getContent();
142 // Assert translations JS is included before drupal.js.
143 $this->assertTrue(strpos($content, $js_filename) < strpos($content, 'core/misc/drupal.js'), 'Translations are included before Drupal.t.');