function LocaleTranslationUiTest::testJavaScriptTranslation
Same name in other branches
- 9 core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testJavaScriptTranslation()
- 8.9.x core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testJavaScriptTranslation()
- 10 core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testJavaScriptTranslation()
Tests the rebuilding of JavaScript translation files on deletion.
File
-
core/
modules/ locale/ tests/ src/ Functional/ LocaleTranslationUiTest.php, line 255
Class
- LocaleTranslationUiTest
- Tests the validation of translation strings and search results.
Namespace
Drupal\Tests\locale\FunctionalCode
public function testJavaScriptTranslation() : void {
$user = $this->drupalCreateUser([
'translate interface',
'administer languages',
'access administration pages',
]);
$this->drupalLogin($user);
$config = $this->config('locale.settings');
$langcode = 'xx';
// The English name for the language. This will be translated.
$name = $this->randomMachineName(16);
// Add custom language.
$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');
$this->container
->get('language_manager')
->reset();
// Build the JavaScript translation file.
// Retrieve the source string of the first string available in the
// {locales_source} table and translate it.
$query = Database::getConnection()->select('locales_source', 's');
$query->addJoin('INNER', 'locales_location', 'l', '[s].[lid] = [l].[lid]');
$source = $query->fields('s', [
'source',
])
->condition('l.type', 'javascript')
->range(0, 1)
->execute()
->fetchField();
$search = [
'string' => $source,
'langcode' => $langcode,
'translation' => 'all',
];
$this->drupalGet('admin/config/regional/translate');
$this->submitForm($search, 'Filter');
$textarea = $this->assertSession()
->elementExists('xpath', '//textarea');
$lid = $textarea->getAttribute('name');
$edit = [
$lid => $this->randomMachineName(),
];
$this->drupalGet('admin/config/regional/translate');
$this->submitForm($edit, 'Save translations');
// Trigger JavaScript translation parsing and building.
_locale_rebuild_js($langcode);
$locale_javascripts = \Drupal::state()->get('locale.translation.javascript', []);
$js_file = 'public://' . $config->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
$this->assertFileExists($js_file);
// Test JavaScript translation rebuilding.
\Drupal::service('file_system')->delete($js_file);
$this->assertFileDoesNotExist($js_file);
_locale_rebuild_js($langcode);
$this->assertFileExists($js_file);
// Test if JavaScript translation contains a custom string override.
$string_override = $this->randomMachineName();
$settings = Settings::getAll();
$settings['locale_custom_strings_' . $langcode] = [
'' => [
$string_override => $string_override,
],
];
// Recreate the settings static.
new Settings($settings);
_locale_rebuild_js($langcode);
$locale_javascripts = \Drupal::state()->get('locale.translation.javascript', []);
$js_file = 'public://' . $config->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
$content = file_get_contents($js_file);
$this->assertStringContainsString('"' . $string_override . '":"' . $string_override . '"', $content);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.