You are here

function drush_language_translations_export_ng in Drush Language Commands 7

Exports .po file(s).

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

See also

\Drupal\locale\Form\ExportForm::submitForm

1 string reference to 'drush_language_translations_export_ng'
language_translations_drush_command in ./language_translations.drush.inc
Implements of hook_drush_command().

File

./language_translations.drush.inc, line 359
Drush language commands related to translations.

Code

function drush_language_translations_export_ng() {
  $args = func_get_args();
  if (count($args) < 2) {
    return drush_set_error(dt('Usage: drush language-export <langcode> <path_to/file.po>'));
  }

  // Get arguments and options.
  $langcode = array_shift($args);
  $file_path_arg = array_shift($args);
  $textgroup = drush_get_option('group', 'default');
  $force = drush_get_option('force', TRUE);

  // Ensure the langcode match an existing language.
  $language = _drush_language_translations_get_language($langcode);
  if ($language == NULL) {
    return drush_set_error(dt('drush language-export: no such language'));
  }

  // Validate export statuses.
  $export_statuses_allowed = [
    // internal-value => input-value
    'customized' => 'customized',
    'not_customized' => 'not-customized',
    'not_translated' => 'not-translated',
  ];
  $export_statuses_input = drush_get_option_list('status', 'customized');
  $export_statuses_input = array_values($export_statuses_input);
  if ($export_statuses_input == [
    'all',
  ]) {
    $export_statuses_input = $export_statuses_allowed;
  }
  $export_statuses_unknown = array_diff($export_statuses_input, $export_statuses_allowed);
  if ($export_statuses_unknown) {
    $t_args = [
      '@options' => implode(', ', $export_statuses_unknown),
    ];
    return drush_set_error(dt('drush language-export: Unknown status options: @options', $t_args));
  }
  $export_statuses_filtered = array_intersect($export_statuses_allowed, $export_statuses_input);
  $export_statuses = array_fill_keys(array_keys($export_statuses_filtered), TRUE);

  // Relative path should be relative to cwd(), rather than Drupal root-dir.
  if (drush_is_absolute_path($file_path_arg)) {
    $file_path = $file_path_arg;
  }
  else {
    $file_path = drush_get_context('DRUSH_DRUPAL_ROOT') . DIRECTORY_SEPARATOR . $file_path_arg;
  }

  // Check if file_path exists and is writable.
  $dir = dirname($file_path);
  if (!file_prepare_directory($dir)) {
    file_prepare_directory($dir, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
  }
  $reader = new PoDatabaseReader();
  if (!method_exists($reader, 'setTextgroup')) {
    return drush_set_error(dt('Patch for l10n_update module is needed from https://www.drupal.org/node/2869244.'));
  }
  $language_name = '';
  if ($language != NULL) {
    $reader
      ->setLangcode($language->language);
    $reader
      ->setOptions($export_statuses);
    $reader
      ->setTextgroup($textgroup);
    $languages = l10n_update_translatable_language_list();
    $language_name = isset($languages[$language->language]) ? $languages[$language->language]->name : '';
  }
  $item = $reader
    ->readItem();
  if ($item || $force) {
    $header = $reader
      ->getHeader();
    $header
      ->setProjectName(variable_get('site_name'));
    $header
      ->setLanguageName($language_name);
    $writer = new PoStreamWriter();
    $writer
      ->setUri($file_path);
    $writer
      ->setHeader($header);
    $writer
      ->open();
    if ($item) {
      $writer
        ->writeItem($item);
    }
    $writer
      ->writeItems($reader);
    $writer
      ->close();
    drush_log('Export complete.', 'success');
  }
  else {
    drush_set_error(dt('Nothing to export.'));
  }
}