function LocaleTranslationUiTest::testJavaScriptTranslation

Same name in other branches
  1. 9 core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testJavaScriptTranslation()
  2. 10 core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testJavaScriptTranslation()
  3. 11.x core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testJavaScriptTranslation()

Adds a language and checks that the JavaScript translation files are properly created and rebuilt on deletion.

File

core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php, line 227

Class

LocaleTranslationUiTest
Adds a new locale and translates its name. Checks the validation of translation strings and search results.

Namespace

Drupal\Tests\locale\Functional

Code

public function testJavaScriptTranslation() {
    $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->drupalPostForm('admin/config/regional/language/add', $edit, t('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->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $textarea = current($this->xpath('//textarea'));
    $lid = $textarea->getAttribute('name');
    $edit = [
        $lid => $this->randomMachineName(),
    ];
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('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->assertFileNotExists($js_file);
    _locale_rebuild_js($langcode);
    $this->assertFileExists($js_file);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.