View source
<?php
namespace Drupal\locale\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\Component\Utility\SafeMarkup;
class LocaleJavascriptTranslationTest extends WebTestBase {
public static $modules = array(
'locale',
'locale_test',
);
public function testFileParsing() {
$filename = drupal_get_path('module', 'locale') . '/tests/locale_test.js';
_locale_parse_js_file($filename);
$strings = $this->container
->get('locale.storage')
->getStrings(array(
'type' => 'javascript',
'name' => $filename,
));
$source_strings = array();
foreach ($strings as $string) {
$source_strings[$string->source] = $string->context;
}
$etx = LOCALE_PLURAL_DELIMITER;
$test_strings = array(
'Standard Call t' => '',
'Whitespace Call t' => '',
'Single Quote t' => '',
"Single Quote \\'Escaped\\' t" => '',
'Single Quote Concat strings t' => '',
'Double Quote t' => '',
"Double Quote \\\"Escaped\\\" t" => '',
'Double Quote Concat strings t' => '',
'Context !key Args t' => 'Context string',
'Context Unquoted t' => 'Context string unquoted',
'Context Single Quoted t' => 'Context string single quoted',
'Context Double Quoted t' => 'Context string double quoted',
"Standard Call plural{$etx}Standard Call @count plural" => '',
"Whitespace Call plural{$etx}Whitespace Call @count plural" => '',
"Single Quote plural{$etx}Single Quote @count plural" => '',
"Single Quote \\'Escaped\\' plural{$etx}Single Quote \\'Escaped\\' @count plural" => '',
"Double Quote plural{$etx}Double Quote @count plural" => '',
"Double Quote \\\"Escaped\\\" plural{$etx}Double Quote \\\"Escaped\\\" @count plural" => '',
"Context !key Args plural{$etx}Context !key Args @count plural" => 'Context string',
"Context Unquoted plural{$etx}Context Unquoted @count plural" => 'Context string unquoted',
"Context Single Quoted plural{$etx}Context Single Quoted @count plural" => 'Context string single quoted',
"Context Double Quoted plural{$etx}Context Double Quoted @count plural" => 'Context string double quoted',
);
foreach ($test_strings as $str => $context) {
$args = array(
'%source' => $str,
'%context' => $context,
);
$this
->assertTrue(isset($source_strings[$str]), SafeMarkup::format('Found source string: %source', $args));
$message = $context ? SafeMarkup::format('Context for %source is %context', $args) : SafeMarkup::format('Context for %source is blank', $args);
$this
->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, $message);
}
$this
->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.');
}
public function testLocaleTranslationJsDependencies() {
$admin_user = $this
->drupalCreateUser(array(
'administer languages',
'access administration pages',
'translate interface',
));
$this
->drupalLogin($admin_user);
$langcode = 'es';
$name = $this
->randomMachineName(16);
$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'));
$edit = array(
"prefix[{$langcode}]" => $prefix,
);
$this
->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
$this
->drupalGet($prefix . '/admin/config/regional/translate');
$strings = \Drupal::service('locale.storage')
->getStrings(array(
'source' => 'Show description',
'type' => 'javascript',
'name' => 'core/modules/locale/locale.admin.js',
));
$string = $strings[0];
$this
->drupalPostForm(NULL, [
'string' => 'Show description',
], t('Filter'));
$edit = [
'strings[' . $string->lid . '][translations][0]' => $this
->randomString(16),
];
$this
->drupalPostForm(NULL, $edit, t('Save translations'));
$js_translation_files = \Drupal::state()
->get('locale.translation.javascript');
$js_filename = $prefix . '_' . $js_translation_files[$prefix] . '.js';
$this
->assertTrue(strpos($this->content, $js_filename) < strpos($this->content, 'core/misc/drupal.js'), 'Translations are included before Drupal.t.');
}
}