diff --git a/composer.json b/composer.json
index fdfe71b..9869f76 100644
--- a/composer.json
+++ b/composer.json
@@ -15,7 +15,6 @@
"composer/installers": "~1.5",
"drupal-composer/drupal-scaffold": "^2.5.2",
"drupal/config_devel": "~1.2",
- "drupal/console": "~1.0",
"drupal/devel": "~1.2",
"drupal/drupal-extension": "~4.0",
"drush/drush": "~9.0@stable",
diff --git a/oe_multilingual.services.yml b/oe_multilingual.services.yml
index 58b2830..a70a6d0 100644
--- a/oe_multilingual.services.yml
+++ b/oe_multilingual.services.yml
@@ -10,4 +10,4 @@ services:
arguments: ['@language_manager', '@path.matcher', '@oe_multilingual.helper']
oe_multilingual.local_translations_batcher:
class: Drupal\oe_multilingual\LocalTranslationsBatcher
- arguments: ['@module_handler', '@theme_handler', '@language_manager', '@extension.list.module', '@extension.list.theme', '@extension.list.profile']
+ arguments: ['@module_handler', '@theme_handler', '@language_manager', '@extension.list.module', '@extension.list.theme', '@extension.list.profile', '@file_system']
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index da835c5..db99eb8 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -6,7 +6,7 @@
-
+
diff --git a/src/Commands/MultilingualCommands.php b/src/Commands/MultilingualCommands.php
index 78852e6..5c23a66 100644
--- a/src/Commands/MultilingualCommands.php
+++ b/src/Commands/MultilingualCommands.php
@@ -44,6 +44,7 @@ public function __construct(LocalTranslationsBatcher $localTranslationsBatcher)
*/
public function importLocalTranslations(array $options = ['langcodes' => self::OPT]): void {
$langcodes = $options['langcodes'] ? $options['langcodes'] : [];
+
$this->localTranslationsBatcher->createBatch($langcodes);
$batch =& batch_get();
if (!$batch) {
@@ -51,6 +52,14 @@ public function importLocalTranslations(array $options = ['langcodes' => self::O
}
drush_backend_batch_process();
+ // Update config translations.
+ if ($batch = locale_config_batch_update_components([])) {
+ $this->logger()->notice('Importing configuration translations...');
+ batch_set($batch);
+ drush_backend_batch_process();
+ $this->logger()->notice('Done.');
+ }
+
}
}
diff --git a/src/LocalTranslationsBatcher.php b/src/LocalTranslationsBatcher.php
index 18e0983..44e7a13 100644
--- a/src/LocalTranslationsBatcher.php
+++ b/src/LocalTranslationsBatcher.php
@@ -4,12 +4,16 @@
namespace Drupal\oe_multilingual;
+use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ProfileExtensionList;
use Drupal\Core\Extension\ThemeExtensionList;
use Drupal\Core\Extension\ThemeHandlerInterface;
+use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Language\LanguageManagerInterface;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\locale\Gettext;
/**
* Creates the batches for importing the local translations.
@@ -19,6 +23,9 @@
*/
class LocalTranslationsBatcher {
+ use StringTranslationTrait;
+ use DependencySerializationTrait;
+
/**
* The module handler.
*
@@ -61,6 +68,13 @@ class LocalTranslationsBatcher {
*/
protected $profileExtensionList;
+ /**
+ * The file system service.
+ *
+ * @var \Drupal\Core\File\FileSystemInterface
+ */
+ protected $fileSystem;
+
/**
* LocalTranslationsBatcher constructor.
*
@@ -76,14 +90,17 @@ class LocalTranslationsBatcher {
* The theme extensions list.
* @param \Drupal\Core\Extension\ProfileExtensionList $profileExtensionList
* The profile extensions list.
+ * @param \Drupal\Core\File\FileSystemInterface $fileSystem
+ * The file system service.
*/
- public function __construct(ModuleHandlerInterface $moduleHandler, ThemeHandlerInterface $themeHandler, LanguageManagerInterface $languageManager, ModuleExtensionList $moduleExtensionList, ThemeExtensionList $themeExtensionList, ProfileExtensionList $profileExtensionList) {
+ public function __construct(ModuleHandlerInterface $moduleHandler, ThemeHandlerInterface $themeHandler, LanguageManagerInterface $languageManager, ModuleExtensionList $moduleExtensionList, ThemeExtensionList $themeExtensionList, ProfileExtensionList $profileExtensionList, FileSystemInterface $fileSystem) {
$this->moduleHandler = $moduleHandler;
$this->themeHandler = $themeHandler;
$this->languageManager = $languageManager;
$this->moduleExtensionList = $moduleExtensionList;
$this->themeExtensionList = $themeExtensionList;
$this->profileExtensionList = $profileExtensionList;
+ $this->fileSystem = $fileSystem;
}
/**
@@ -95,10 +112,8 @@ public function __construct(ModuleHandlerInterface $moduleHandler, ThemeHandlerI
* @see \Drupal\locale\Form\TranslationStatusForm::submitForm()
*/
public function createBatch(array $langcodes = []): void {
- $this->moduleHandler->loadInclude('locale', 'fetch.inc');
- $this->moduleHandler->loadInclude('locale', 'bulk.inc');
- $this->moduleHandler->loadInclude('locale', 'translation.inc');
$this->moduleHandler->loadInclude('locale', 'inc', 'locale.compare');
+ $this->moduleHandler->loadInclude('locale', 'inc', 'locale.bulk');
if (!$langcodes) {
$languages = $this->languageManager->getLanguages();
@@ -113,18 +128,29 @@ public function createBatch(array $langcodes = []): void {
}
$extensions = $this->getExtensionsToTranslate();
- if (!$extensions) {
+ if (!$extensions || !$langcodes) {
return;
}
- locale_translation_flush_projects();
- locale_translation_check_projects_local($extensions, $langcodes);
- $options = _locale_translation_default_update_options();
- $batch = locale_translation_batch_fetch_build($extensions, $langcodes, $options);
- batch_set($batch);
- if ($batch = locale_config_batch_update_components($options, $langcodes)) {
- batch_set($batch);
+ // Build operations for local translation batch import.
+ $operations = [];
+ foreach ($extensions as $extension) {
+ $operations[] = [
+ [$this, 'importProjectPoFiles'],
+ [$extension, $langcodes],
+ ];
}
+
+ $batch = [
+ 'operations' => $operations,
+ 'title' => $this->t('Importing translations.'),
+ 'progress_message' => '',
+ 'error_message' => $this->t('Error importing translation files'),
+ 'file' => drupal_get_path('module', 'locale') . '/locale.batch.inc',
+ ];
+
+ batch_set($batch);
+
}
/**
@@ -151,7 +177,30 @@ protected function getExtensionsToTranslate(): array {
$extensions[] = $name;
}
- return $extensions;
+ return array_intersect_key(locale_translation_project_list(), array_combine($extensions, $extensions));
+ }
+
+ /**
+ * Implements callback_batch_operation().
+ *
+ * Import po files of the project for allowed languages.
+ */
+ public function importProjectPoFiles($extension, $langcodes, &$context): void {
+ $files = file_scan_directory(
+ $this->fileSystem->dirname($extension['info']['interface translation server pattern']),
+ '/.*-(' . implode('|', $langcodes) . ')\.po/'
+ );
+ foreach ($files as $file) {
+ preg_match('/(' . implode('|', $langcodes) . ')$/', $file->name, $matches);
+ $file->langcode = $matches[0];
+ Gettext::fileToDatabase($file, [
+ 'overwrite_options' => [
+ 'not_customized' => TRUE,
+ ],
+ ]);
+
+ $context['message'] = $this->t('Imported translation for %project (%langcode).', ['%project' => $extension['name'], '%langcode' => $file->langcode]);
+ }
}
}