function LocaleTranslationUiTest::testStringTranslation
Same name in other branches
- 9 core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testStringTranslation()
- 10 core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testStringTranslation()
- 11.x core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testStringTranslation()
Adds a language and tests string translation by users with the appropriate permissions.
File
-
core/
modules/ locale/ tests/ src/ Functional/ LocaleTranslationUiTest.php, line 48
Class
- LocaleTranslationUiTest
- Adds a new locale and translates its name. Checks the validation of translation strings and search results.
Namespace
Drupal\Tests\locale\FunctionalCode
public function testStringTranslation() {
// User to add and remove language.
$admin_user = $this->drupalCreateUser([
'administer languages',
'access administration pages',
]);
// User to translate and delete string.
$translate_user = $this->drupalCreateUser([
'translate interface',
'access administration pages',
]);
// Code for the language.
$langcode = 'xx';
// The English name for the language. This will be translated.
$name = 'cucurbitaceae';
// This will be the translation of $name.
$translation = $this->randomMachineName(16);
$translation_to_en = $this->randomMachineName(16);
// Add custom language.
$this->drupalLogin($admin_user);
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
];
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
// Add string.
t($name, [], [
'langcode' => $langcode,
])->render();
// Reset locale cache.
$this->container
->get('string_translation')
->reset();
$this->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
$this->assertText(t($name), 'Test language added.');
$this->drupalLogout();
// Search for the name and translate it.
$this->drupalLogin($translate_user);
$search = [
'string' => $name,
'langcode' => $langcode,
'translation' => 'untranslated',
];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertText($name, 'Search found the string as untranslated.');
// No t() here, it's surely not translated yet.
$this->assertText($name, 'name found on edit screen.');
$this->assertNoOption('edit-langcode', 'en', 'No way to translate the string to English.');
$this->drupalLogout();
$this->drupalLogin($admin_user);
$this->drupalPostForm('admin/config/regional/language/edit/en', [
'locale_translate_english' => TRUE,
], t('Save language'));
$this->drupalLogout();
$this->drupalLogin($translate_user);
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertText($name, 'Search found the string as untranslated.');
// Assume this is the only result, given the random name.
$textarea = current($this->xpath('//textarea'));
$lid = $textarea->getAttribute('name');
$edit = [
$lid => $translation,
];
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$this->assertText(t('The strings have been saved.'), 'The strings have been saved.');
$url_bits = explode('?', $this->getUrl());
$this->assertEqual($url_bits[0], Url::fromRoute('locale.translate_page', [], [
'absolute' => TRUE,
])->toString(), 'Correct page redirection.');
$search = [
'string' => $name,
'langcode' => $langcode,
'translation' => 'translated',
];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertRaw($translation, 'Non-English translation properly saved.');
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'untranslated',
];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this->xpath('//textarea'));
$lid = $textarea->getAttribute('name');
$edit = [
$lid => $translation_to_en,
];
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'translated',
];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertRaw($translation_to_en, 'English translation properly saved.');
$this->assertTrue($name != $translation && t($name, [], [
'langcode' => $langcode,
]) == $translation, 't() works for non-English.');
// Refresh the locale() cache to get fresh data from t() below. We are in
// the same HTTP request and therefore t() is not refreshed by saving the
// translation above.
$this->container
->get('string_translation')
->reset();
// Now we should get the proper fresh translation from t().
$this->assertTrue($name != $translation_to_en && t($name, [], [
'langcode' => 'en',
]) == $translation_to_en, 't() works for English.');
$this->assertTrue(t($name, [], [
'langcode' => LanguageInterface::LANGCODE_SYSTEM,
]) == $name, 't() works for LanguageInterface::LANGCODE_SYSTEM.');
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'untranslated',
];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertText(t('No strings available.'), 'String is translated.');
// Test invalidation of 'rendered' cache tag after string translation.
$this->drupalLogout();
$this->drupalGet('xx/user/login');
$this->assertText('Enter the password that accompanies your username.');
$this->drupalLogin($translate_user);
$search = [
'string' => 'accompanies your username',
'langcode' => $langcode,
'translation' => 'untranslated',
];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this->xpath('//textarea'));
$lid = $textarea->getAttribute('name');
$edit = [
$lid => 'Please enter your Llama username.',
];
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$this->drupalLogout();
$this->drupalGet('xx/user/login');
$this->assertText('Please enter your Llama username.');
// Delete the language.
$this->drupalLogin($admin_user);
$path = 'admin/config/regional/language/delete/' . $langcode;
// This a confirm form, we do not need any fields changed.
$this->drupalPostForm($path, [], t('Delete'));
// We need raw here because %language and %langcode will add HTML.
$t_args = [
'%language' => $name,
'%langcode' => $langcode,
];
$this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The test language has been removed.');
// Reload to remove $name.
$this->drupalGet($path);
// Verify that language is no longer found.
$this->assertSession()
->statusCodeEquals(404);
$this->drupalLogout();
// Delete the string.
$this->drupalLogin($translate_user);
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'translated',
];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
// Assume this is the only result, given the random name.
$textarea = current($this->xpath('//textarea'));
$lid = $textarea->getAttribute('name');
$edit = [
$lid => '',
];
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$this->assertRaw($name, 'The strings have been saved.');
$this->drupalLogin($translate_user);
$search = [
'string' => $name,
'langcode' => 'en',
'translation' => 'untranslated',
];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertNoText(t('No strings available.'), 'The translation has been removed');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.