function LocaleExportTest::testExportTranslation

Same name and namespace in other branches
  1. 9 core/modules/locale/tests/src/Functional/LocaleExportTest.php \Drupal\Tests\locale\Functional\LocaleExportTest::testExportTranslation()
  2. 8.9.x core/modules/locale/tests/src/Functional/LocaleExportTest.php \Drupal\Tests\locale\Functional\LocaleExportTest::testExportTranslation()
  3. 11.x core/modules/locale/tests/src/Functional/LocaleExportTest.php \Drupal\Tests\locale\Functional\LocaleExportTest::testExportTranslation()

Tests exportation of translations.

File

core/modules/locale/tests/src/Functional/LocaleExportTest.php, line 55

Class

LocaleExportTest
Tests the exportation of locale files.

Namespace

Drupal\Tests\locale\Functional

Code

public function testExportTranslation() : void {
  $file_system = \Drupal::service('file_system');
  // First import some known translations.
  // This will also automatically add the 'fr' language.
  $name = $file_system->tempnam('temporary://', "po_") . '.po';
  file_put_contents($name, $this->getPoFile());
  $this->drupalGet('admin/config/regional/translate/import');
  $this->submitForm([
    'langcode' => 'fr',
    'files[file]' => $name,
  ], 'Import');
  $file_system->unlink($name);
  // Get the French translations.
  $this->drupalGet('admin/config/regional/translate/export');
  $this->submitForm([
    'langcode' => 'fr',
  ], 'Export');
  // Ensure we have a translation file.
  $this->assertSession()
    ->pageTextContains('# French translation of Drupal');
  // Ensure our imported translations exist in the file.
  $this->assertSession()
    ->pageTextContains('msgstr "lundi"');
  // Import some more French translations which will be marked as customized.
  $name = $file_system->tempnam('temporary://', "po2_") . '.po';
  file_put_contents($name, $this->getCustomPoFile());
  $this->drupalGet('admin/config/regional/translate/import');
  $this->submitForm([
    'langcode' => 'fr',
    'files[file]' => $name,
    'customized' => 1,
  ], 'Import');
  $file_system->unlink($name);
  // Create string without translation in the locales_source table.
  $this->container
    ->get('locale.storage')
    ->createString()
    ->setString('February')
    ->save();
  // Export only customized French translations.
  $this->drupalGet('admin/config/regional/translate/export');
  $this->submitForm([
    'langcode' => 'fr',
    'content_options[not_customized]' => FALSE,
    'content_options[customized]' => TRUE,
    'content_options[not_translated]' => FALSE,
  ], 'Export');
  // Ensure we have a translation file.
  $this->assertSession()
    ->pageTextContains('# French translation of Drupal');
  // Ensure the customized translations exist in the file.
  $this->assertSession()
    ->pageTextContains('msgstr "janvier"');
  // Ensure no untranslated strings exist in the file.
  $this->assertSession()
    ->responseNotContains('msgid "February"');
  // Export only untranslated French translations.
  $this->drupalGet('admin/config/regional/translate/export');
  $this->submitForm([
    'langcode' => 'fr',
    'content_options[not_customized]' => FALSE,
    'content_options[customized]' => FALSE,
    'content_options[not_translated]' => TRUE,
  ], 'Export');
  // Ensure we have a translation file.
  $this->assertSession()
    ->pageTextContains('# French translation of Drupal');
  // Ensure no customized translations exist in the file.
  $this->assertSession()
    ->responseNotContains('msgstr "janvier"');
  // Ensure the untranslated strings exist in the file, and with right quotes.
  $this->assertSession()
    ->responseContains($this->getUntranslatedString());
}

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