function LocaleExportTest::testExportTranslation

Same name in other branches
  1. 9 core/modules/locale/tests/src/Functional/LocaleExportTest.php \Drupal\Tests\locale\Functional\LocaleExportTest::testExportTranslation()
  2. 10 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()

Test exportation of translations.

File

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

Class

LocaleExportTest
Tests the exportation of locale files.

Namespace

Drupal\Tests\locale\Functional

Code

public function testExportTranslation() {
    $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->drupalPostForm('admin/config/regional/translate/import', [
        'langcode' => 'fr',
        'files[file]' => $name,
    ], t('Import'));
    $file_system->unlink($name);
    // Get the French translations.
    $this->drupalPostForm('admin/config/regional/translate/export', [
        'langcode' => 'fr',
    ], t('Export'));
    // Ensure we have a translation file.
    $this->assertRaw('# French translation of Drupal', 'Exported French translation file.');
    // Ensure our imported translations exist in the file.
    $this->assertRaw('msgstr "lundi"', 'French translations present in exported file.');
    // 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->drupalPostForm('admin/config/regional/translate/import', [
        'langcode' => 'fr',
        'files[file]' => $name,
        'customized' => 1,
    ], t('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->drupalPostForm('admin/config/regional/translate/export', [
        'langcode' => 'fr',
        'content_options[not_customized]' => FALSE,
        'content_options[customized]' => TRUE,
        'content_options[not_translated]' => FALSE,
    ], t('Export'));
    // Ensure we have a translation file.
    $this->assertRaw('# French translation of Drupal', 'Exported French translation file with only customized strings.');
    // Ensure the customized translations exist in the file.
    $this->assertRaw('msgstr "janvier"', 'French custom translation present in exported file.');
    // Ensure no untranslated strings exist in the file.
    $this->assertNoRaw('msgid "February"', 'Untranslated string not present in exported file.');
    // Export only untranslated French translations.
    $this->drupalPostForm('admin/config/regional/translate/export', [
        'langcode' => 'fr',
        'content_options[not_customized]' => FALSE,
        'content_options[customized]' => FALSE,
        'content_options[not_translated]' => TRUE,
    ], t('Export'));
    // Ensure we have a translation file.
    $this->assertRaw('# French translation of Drupal', 'Exported French translation file with only untranslated strings.');
    // Ensure no customized translations exist in the file.
    $this->assertNoRaw('msgstr "janvier"', 'French custom translation not present in exported file.');
    // Ensure the untranslated strings exist in the file, and with right quotes.
    $this->assertRaw($this->getUntranslatedString(), 'Empty string present in exported file.');
}

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