You are here

public function DrushLanguageCliService::importTranslations in Drush Language Commands 8

Import a single .po file.

@todo Implement \Drupal\locale\Form\ImportForm::buildForm @todo This can be simplified once https://www.drupal.org/node/2631584 lands in Drupal core.

Parameters

\Symfony\Component\Console\Style\StyleInterface|\Drupal\drush_language\Drush8Io $io: The $io interface of the cli tool calling.

callable $t: The translation function akin to t().

array $poFiles: A list of paths .po files containing the translations.

array $options: The command options.

See also

\Drupal\locale\Form\ImportForm::submitForm

File

src/Service/DrushLanguageCliService.php, line 194

Class

DrushLanguageCliService
Class DrushLanguageCliService.

Namespace

Drupal\drush_language\Service

Code

public function importTranslations($io, callable $t, array $poFiles, array $options = [
  'langcode' => NULL,
  'replace-customized' => TRUE,
  'replace-not-customized' => TRUE,
  'set-customized' => TRUE,
  'autocreate-language' => TRUE,
]) {
  $this->moduleHandler
    ->loadInclude('locale', 'translation.inc');
  $this->moduleHandler
    ->loadInclude('locale', 'bulk.inc');
  $opt_langcode = $options['langcode'];
  $opt_set_customized = $options['set-customized'];
  $opt_replace_customized = $options['replace-customized'];
  $opt_replace_not_customized = $options['replace-not-customized'];
  $opt_autocreate_language = $options['autocreate-language'];
  if (!$poFiles) {
    if ($dir = Settings::get('custom_translations_directory')) {
      $poFiles = glob(DRUPAL_ROOT . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . '*.po');
    }
    else {
      $io
        ->success($t('Nothing to do, no file given and no custom translation directory set.'));
    }
  }
  $importer_options = array_merge(_locale_translation_default_update_options(), [
    'langcode' => $opt_langcode,
    'customized' => $opt_set_customized ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
    'overwrite_options' => [
      'customized' => (int) $opt_replace_customized,
      'not_customized' => (int) $opt_replace_not_customized,
    ],
  ]);

  // Import language files.
  $files = [];
  $langcodes_to_import = [];
  foreach ($poFiles as $file_path) {

    // Ensure we have the file intended for upload.
    if (file_exists($file_path)) {
      $file = locale_translate_file_create($file_path);

      // Extract project, version and language code from the file name
      // Supported:
      // - {project}-{version}.{langcode}.po, {prefix}.{langcode}.po
      // - {langcode}.po
      // Note: $options['langcode'] will override file langcode.
      $file = locale_translate_file_attach_properties($file, $importer_options);
      if ($file->langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
        if (!$opt_langcode) {
          $io
            ->error($t('Can not autodetect language of file @file', [
            '@file' => $file_path,
          ]));
          return;
        }
        $file->langcode = $opt_langcode;
        if (empty($file->version) && !empty($file->project) && !empty($file->langcode)) {
          $sources = locale_translation_get_status();
          $source = $sources[$file->project][$file->langcode];
          if (isset($source->version)) {
            $file->version = $source->version;
          }
        }
      }
      $langcodes_to_import[$file->langcode] = $file->langcode;
      $files[] = $file;
    }
    else {
      $io
        ->error($t('File to import at @filepath not found.', [
        '@filepath' => $file_path,
      ]));
    }
  }
  if ($opt_autocreate_language) {
    $languages = $this->languageManager
      ->getLanguages();
    foreach ($langcodes_to_import as $langcode_to_import) {
      if (!isset($languages[$langcode_to_import])) {
        try {

          /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $language_storage */
          $language_storage = $this->entityTypeManager
            ->getStorage('configurable_language');
          $language = $language_storage
            ->create([
            'langcode' => $opt_langcode,
          ]);
          $io
            ->success($t('The language @id (@label) has been created.', [
            '@id' => $language
              ->id(),
            '@label' => $language
              ->label(),
          ]));
        } catch (InvalidPluginDefinitionException $exception) {
          $io
            ->error($exception
            ->getMessage());
        }
      }
    }
  }
  $batch = locale_translate_batch_build($files, $importer_options);
  batch_set($batch);

  // Create or update all configuration translations for this language.
  if ($batch = locale_config_batch_update_components($importer_options, $langcodes_to_import)) {
    batch_set($batch);
  }
  drush_backend_batch_process();
  $io
    ->success($t('Import complete.'));
}