getPoFile example

// Languages to test, with various number of plural forms.     $languages = [
      'vi' => ['plurals' => 1, 'expected' => [TRUE, FALSE, FALSE, FALSE]],
      'fr' => ['plurals' => 2, 'expected' => [TRUE, TRUE, FALSE, FALSE]],
      'sl' => ['plurals' => 4, 'expected' => [TRUE, TRUE, TRUE, TRUE]],
    ];

    foreach ($languages as $langcode => $data) {
      // Import a .po file to add a new language with a given number of plural forms       $name = \Drupal::service('file_system')->tempnam('temporary://', $langcode . '_') . '.po';
      file_put_contents($name$this->getPoFile($data['plurals']));
      $this->drupalGet('admin/config/regional/translate/import');
      $this->submitForm([
        'langcode' => $langcode,
        'files[file]' => $name,
      ], 'Import');

      // Change the config langcode of the 'files' view.       $config = \Drupal::service('config.factory')->getEditable('views.view.files');
      $config->set('langcode', $langcode);
      $config->save();

      
\Drupal::service('file_system')->copy(__DIR__ . '/../../../tests/test.xx.po', 'translations://', FileSystemInterface::EXISTS_REPLACE);
  }

  /** * Tests exportation of translations. */
  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->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');

    
    $this->config('locale.settings')
      ->set('translation.import_enabled', TRUE)
      ->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
      ->save();
  }

  /** * Tests import of standalone .po files. */
  public function testStandalonePoFile() {
    // Try importing a .po file.     $this->importPoFile($this->getPoFile()[
      'langcode' => 'fr',
    ]);
    $this->config('locale.settings');
    // The import should automatically create the corresponding language.     $this->assertSession()->pageTextContains("The language French has been created.");

    // The import should have created 8 strings.     $this->assertSession()->pageTextContains("One translation file imported. 8 translations were added, 0 translations were updated and 0 translations were removed.");

    // This import should have saved plural forms to have 2 variants.     $locale_plurals = \Drupal::service('locale.plural.formula')->getNumberOfPlurals('fr');
    
Home | Imprint | This part of the site doesn't use cookies.