Chris@0: container Chris@0: ->get('locale.storage') Chris@0: ->getStrings([ Chris@0: 'type' => 'javascript', Chris@0: 'name' => $filename, Chris@0: ]); Chris@0: Chris@0: $source_strings = []; Chris@0: foreach ($strings as $string) { Chris@0: $source_strings[$string->source] = $string->context; Chris@0: } Chris@0: Chris@18: $etx = PoItem::DELIMITER; Chris@0: // List of all strings that should be in the file. Chris@0: $test_strings = [ Chris@0: 'Standard Call t' => '', Chris@0: 'Whitespace Call t' => '', Chris@0: Chris@0: 'Single Quote t' => '', Chris@0: "Single Quote \\'Escaped\\' t" => '', Chris@0: 'Single Quote Concat strings t' => '', Chris@0: Chris@0: 'Double Quote t' => '', Chris@0: "Double Quote \\\"Escaped\\\" t" => '', Chris@0: 'Double Quote Concat strings t' => '', Chris@0: Chris@0: 'Context !key Args t' => 'Context string', Chris@0: Chris@0: 'Context Unquoted t' => 'Context string unquoted', Chris@0: 'Context Single Quoted t' => 'Context string single quoted', Chris@0: 'Context Double Quoted t' => 'Context string double quoted', Chris@0: Chris@0: "Standard Call plural{$etx}Standard Call @count plural" => '', Chris@0: "Whitespace Call plural{$etx}Whitespace Call @count plural" => '', Chris@0: Chris@0: "Single Quote plural{$etx}Single Quote @count plural" => '', Chris@0: "Single Quote \\'Escaped\\' plural{$etx}Single Quote \\'Escaped\\' @count plural" => '', Chris@0: Chris@0: "Double Quote plural{$etx}Double Quote @count plural" => '', Chris@0: "Double Quote \\\"Escaped\\\" plural{$etx}Double Quote \\\"Escaped\\\" @count plural" => '', Chris@0: Chris@0: "Context !key Args plural{$etx}Context !key Args @count plural" => 'Context string', Chris@0: Chris@0: "Context Unquoted plural{$etx}Context Unquoted @count plural" => 'Context string unquoted', Chris@0: "Context Single Quoted plural{$etx}Context Single Quoted @count plural" => 'Context string single quoted', Chris@0: "Context Double Quoted plural{$etx}Context Double Quoted @count plural" => 'Context string double quoted', Chris@18: Chris@18: "No count argument plural - singular{$etx}No count argument plural - plural" => '', Chris@0: ]; Chris@0: Chris@0: // Assert that all strings were found properly. Chris@0: foreach ($test_strings as $str => $context) { Chris@0: $args = ['%source' => $str, '%context' => $context]; Chris@0: Chris@0: // Make sure that the string was found in the file. Chris@17: $this->assertTrue(isset($source_strings[$str]), new FormattableMarkup('Found source string: %source', $args)); Chris@0: Chris@0: // Make sure that the proper context was matched. Chris@17: $message = $context ? new FormattableMarkup('Context for %source is %context', $args) : new FormattableMarkup('Context for %source is blank', $args); Chris@0: $this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, $message); Chris@0: } Chris@0: Chris@0: $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert translations JS is added before drupal.js, because it depends on it. Chris@0: */ Chris@0: public function testLocaleTranslationJsDependencies() { Chris@0: // User to add and remove language. Chris@0: $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface']); Chris@0: Chris@0: // Add custom language. Chris@0: $this->drupalLogin($admin_user); Chris@0: // Code for the language. Chris@0: $langcode = 'es'; Chris@0: // The English name for the language. Chris@0: $name = $this->randomMachineName(16); Chris@0: // The domain prefix. Chris@0: $prefix = $langcode; Chris@0: $edit = [ Chris@0: 'predefined_langcode' => 'custom', Chris@0: 'langcode' => $langcode, Chris@0: 'label' => $name, Chris@0: 'direction' => LanguageInterface::DIRECTION_LTR, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); Chris@0: Chris@0: // Set path prefix. Chris@0: $edit = ["prefix[$langcode]" => $prefix]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); Chris@0: Chris@0: // This forces locale.admin.js string sources to be imported, which contains Chris@0: // the next translation. Chris@0: $this->drupalGet($prefix . '/admin/config/regional/translate'); Chris@0: Chris@0: // Translate a string in locale.admin.js to our new language. Chris@0: $strings = \Drupal::service('locale.storage') Chris@0: ->getStrings([ Chris@0: 'source' => 'Show description', Chris@0: 'type' => 'javascript', Chris@0: 'name' => 'core/modules/locale/locale.admin.js', Chris@0: ]); Chris@0: $string = $strings[0]; Chris@0: Chris@0: $this->drupalPostForm(NULL, ['string' => 'Show description'], t('Filter')); Chris@0: $edit = ['strings[' . $string->lid . '][translations][0]' => $this->randomString(16)]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save translations')); Chris@0: Chris@0: // Calculate the filename of the JS including the translations. Chris@0: $js_translation_files = \Drupal::state()->get('locale.translation.javascript'); Chris@0: $js_filename = $prefix . '_' . $js_translation_files[$prefix] . '.js'; Chris@0: Chris@0: $content = $this->getSession()->getPage()->getContent(); Chris@0: // Assert translations JS is included before drupal.js. Chris@0: $this->assertTrue(strpos($content, $js_filename) < strpos($content, 'core/misc/drupal.js'), 'Translations are included before Drupal.t.'); Chris@0: } Chris@0: Chris@0: }