Skip to content

Commit

Permalink
Merge pull request #82 from openeuropa/OPENEUROPA-2147-3
Browse files Browse the repository at this point in the history
OPENEUROPA-2147: Implementation of third approach with local import translations.
  • Loading branch information
ademarco authored Aug 26, 2019
2 parents e1a2796 + 7969771 commit 504e558
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion oe_multilingual.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<env name="SIMPLETEST_IGNORE_DIRECTORIES" value="${drupal.root}"/>
<env name="SIMPLETEST_BASE_URL" value="${drupal.base_url}"/>
<env name="SIMPLETEST_DB" value="mysql://${drupal.database.user}:${drupal.database.password}@${drupal.database.host}:${drupal.database.port}/${drupal.database.name}"/>
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["${selenium.browser}", null, "${selenium.host}/wd/hub"]'/>
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["${selenium.browser}", null, "${selenium.host}:${selenium.port}/wd/hub"]'/>
</php>
<testsuites>
<testsuite>
Expand Down
9 changes: 9 additions & 0 deletions src/Commands/MultilingualCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,22 @@ 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) {
return;
}

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.');
}

}

}
75 changes: 62 additions & 13 deletions src/LocalTranslationsBatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,6 +23,9 @@
*/
class LocalTranslationsBatcher {

use StringTranslationTrait;
use DependencySerializationTrait;

/**
* The module handler.
*
Expand Down Expand Up @@ -61,6 +68,13 @@ class LocalTranslationsBatcher {
*/
protected $profileExtensionList;

/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;

/**
* LocalTranslationsBatcher constructor.
*
Expand All @@ -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;
}

/**
Expand All @@ -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();
Expand All @@ -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);

}

/**
Expand All @@ -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]);
}
}

}

0 comments on commit 504e558

Please sign in to comment.