View source
<?php
namespace Drupal\Tests\locale\Functional;
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Component\Render\FormattableMarkup;
class LocaleJavascriptTranslationTest extends BrowserTestBase {
protected static $modules = [
'locale',
'locale_test',
];
protected $defaultTheme = 'stark';
public function testFileParsing() {
$files[] = __DIR__ . '/../../locale_test.es6.js';
$files[] = __DIR__ . '/../../locale_test.js';
foreach ($files as $filename) {
_locale_parse_js_file($filename);
$strings = $this->container
->get('locale.storage')
->getStrings([
'type' => 'javascript',
'name' => $filename,
]);
$source_strings = [];
foreach ($strings as $string) {
$source_strings[$string->source] = $string->context;
}
$etx = PoItem::DELIMITER;
$test_strings = [
'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',
"No count argument plural - singular{$etx}No count argument plural - plural" => '',
];
foreach ($test_strings as $str => $context) {
$args = [
'%source' => $str,
'%context' => $context,
];
$this
->assertTrue(isset($source_strings[$str]), new FormattableMarkup('Found source string: %source', $args));
$this
->assertArrayHasKey($str, $source_strings);
$this
->assertSame($context, $source_strings[$str]);
}
$this
->assertSameSize($test_strings, $source_strings, 'Found correct number of source strings.');
}
}
public function testLocaleTranslationJsDependencies() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
'translate interface',
]);
$this
->drupalLogin($admin_user);
$langcode = 'es';
$name = $this
->randomMachineName(16);
$prefix = $langcode;
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add custom language');
$edit = [
"prefix[{$langcode}]" => $prefix,
];
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet($prefix . '/admin/config/regional/translate');
$strings = \Drupal::service('locale.storage')
->getStrings([
'source' => 'Show description',
'type' => 'javascript',
'name' => 'core/modules/locale/locale.admin.js',
]);
$string = $strings[0];
$this
->submitForm([
'string' => 'Show description',
], 'Filter');
$edit = [
'strings[' . $string->lid . '][translations][0]' => 'Mostrar descripcion',
];
$this
->submitForm($edit, 'Save translations');
$js_translation_files = \Drupal::state()
->get('locale.translation.javascript');
$js_filename = $prefix . '_' . $js_translation_files[$prefix] . '.js';
$content = $this
->getSession()
->getPage()
->getContent();
$this
->assertSession()
->responseContains('core/misc/drupal.js');
$this
->assertSession()
->responseContains($js_filename);
$this
->assertLessThan(strpos($content, 'core/misc/drupal.js'), strpos($content, $js_filename));
}
}