Skip to content

Commit

Permalink
Add locale:import:all - imports multiple custom translation po files (#…
Browse files Browse the repository at this point in the history
…5569)

* Add locale:import:all - imports multiple custom translation po files

Supersedes #4251

* Fix feedbacks

* Fix feedbacks

* Apply suggestions from code review

Co-authored-by: Erik Stielstra <info@erikstielstra.nl>

---------

Co-authored-by: Sergey-Orlov <sergey.kam.orlov@gmail.com>
Co-authored-by: Erik Stielstra <info@erikstielstra.nl>
  • Loading branch information
3 people committed Aug 23, 2023
1 parent 42efcdb commit 6ab5d13
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/Commands/core/LocaleCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class LocaleCommands extends DrushCommands
const UPDATE = 'locale:update';
const EXPORT = 'locale:export';
const IMPORT = 'locale:import';
const IMPORT_ALL = 'locale:import-all';

protected function getLanguageManager(): LanguageManagerInterface
{
Expand Down Expand Up @@ -208,6 +209,80 @@ public function exportValidate(CommandData $commandData): void
}
}

/**
* Imports multiple translation files from the defined directory.
*
* @throws \Exception
*/
#[CLI\Command(name: self::IMPORT_ALL, aliases: ['locale-import-all', 'locale:import:all'])]
#[CLI\Argument(name: 'directory', description: 'The path to directory with translation files to import.')]
#[CLI\Option(name: 'type', description: 'String types to include, defaults to <info>not-customized</info>. Recognized values: <info>not-customized</info>, <info>customized</info>', suggestedValues: ['not-customized', 'customized'])]
#[CLI\Option(name: 'override', description: 'Whether and how imported strings will override existing translations. Defaults to the Import behavior configured in the admin interface. Recognized values: <info>none</info>, <info>customized</info>, <info>not-customized</info>, <info>all</info>', suggestedValues: ['none', 'not-customized', 'customized', 'all'])]
#[CLI\Usage(name: 'drush locale:import-all /var/www/translations', description: 'Import all translations from the defined directory (non-recursively). Supported filename patterns are: {project}-{version}.{langcode}.po, {prefix}.{langcode}.po or {langcode}.po.')]
#[CLI\Usage(name: 'drush locale:import-all /var/www/translations/custom --types=customized --override=all', description: 'Import all custom translations from the defined directory (non-recursively) and override any existing translation. Supported filename patterns are: {project}-{version}.{langcode}.po, {prefix}.{langcode}.po or {langcode}.po.')]
#[CLI\ValidateModulesEnabled(modules: ['locale'])]
public function importAll($directory, $options = ['type' => self::REQ, 'override' => self::REQ])
{
if (!is_dir($directory)) {
throw new \Exception('The defined directory does not exist.');
}

// Look for .po files in defined directory
$poFiles = glob($directory . DIRECTORY_SEPARATOR . '*.po');
if (empty($poFiles)) {
throw new \Exception('Translation files not found in the defined directory.');
}

$this->getModuleHandler()->loadInclude('locale', 'translation.inc');
$this->getModuleHandler()->loadInclude('locale', 'bulk.inc');

$translationOptions = _locale_translation_default_update_options();
$translationOptions['customized'] = $this->convertCustomizedType($options['type']);
$override = $this->convertOverrideOption($options['override']);
if ($override) {
$translationOptions['overwrite_options'] = $override;
}
$langcodes_to_import = [];
$files = [];
foreach ($poFiles as $file) {
// Ensure we have the file intended for upload.
if (!file_exists($file)) {
$this->logger->warning(dt('Can not read file @file.', ['@file' => $file]));
continue;
}
$poFile = (object) [
'filename' => basename($file),
'uri' => $file,
];
$poFile = locale_translate_file_attach_properties($poFile, $translationOptions);
if ($poFile->langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
$this->logger->warning(dt('Can not autodetect language of file @file. Supported filename patterns are: {project}-{version}.{langcode}.po, {prefix}.{langcode}.po or {langcode}.po.', [
'@file' => $file,
]));
continue;
}
if (!$this->getLanguageManager()->getLanguage($poFile->langcode)) {
$this->logger->warning(dt('Language @language does not exist for file @file', [
'@language' => $poFile->langcode,
'@file' => $file,
]));
continue;
}
// Import translation file if language exists.
$langcodes_to_import[$poFile->langcode] = $poFile->langcode;
$files[$poFile->uri] = $poFile;
}

// Set a batch to download and import translations.
$batch = locale_translate_batch_build($files, $translationOptions);
batch_set($batch);
if ($batch = locale_config_batch_update_components($translationOptions, $langcodes_to_import)) {
batch_set($batch);
}

drush_backend_batch_process();
}

/**
* Converts input of translation type.
*
Expand Down

0 comments on commit 6ab5d13

Please sign in to comment.