Skip to content

Commit

Permalink
Merge pull request #152 from City-of-Helsinki/UHF-9088
Browse files Browse the repository at this point in the history
UHF-9088: Helfi locale-import
  • Loading branch information
khalima authored Mar 25, 2024
2 parents b7fde18 + 39b3d84 commit b31aadb
Showing 1 changed file with 6 additions and 50 deletions.
56 changes: 6 additions & 50 deletions src/Commands/LocaleCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

namespace Drupal\helfi_api_base\Commands;

use Drupal\Component\Gettext\PoStreamReader;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationManager;
use Drush\Commands\DrushCommands;

/**
* A Drush commandfile.
* A Drush command file.
*/
class LocaleCommands extends DrushCommands {

Expand Down Expand Up @@ -67,25 +65,6 @@ private function getTranslationFile(string $language, string $module) : ?object
return NULL;
}

/**
* Creates a PO stream reader instance.
*
* @param string $langcode
* The langcode.
* @param object $file
* The file.
*
* @return \Drupal\Component\Gettext\PoStreamReader
* The stream reader.
*/
private function createStreamReader(string $langcode, object $file) : PoStreamReader {
$reader = new PoStreamReader();
$reader->setLangcode($langcode);
$reader->setURI($file->uri);
$reader->open();
return $reader;
}

/**
* Pre-command hook to import english source strings.
*
Expand All @@ -106,37 +85,14 @@ public function import(string $module) {
continue;
}

// Expose source strings (to make them translatable).
$reader = $this->createStreamReader($language->getId(), $file);

while ($item = $reader->readItem()) {
$options = [
'langcode' => $language->getId(),
];

if ($context = $item->getContext()) {
$options['context'] = $context;
}
$sources = $item->getSource();

// We don't want to expose strings with plural form.
if ($item->isPlural()) {
continue;
}

if (!is_array($sources)) {
$sources = [$sources];
}
foreach ($sources as $source) {
$this->translationManager
// @codingStandardsIgnoreLine
->translateString(new TranslatableMarkup($source, [], $options));
}
}
$process = $this->processManager()->process([
'drush',
'locale:import',
'--type=customized',
// Import translations as not-customized translations.
// Let users override translations from UI translate interface.
'--type=not-customized',
// Override only not customized translations.
'--override=not-customized',
$language->getId(),
$file->uri,
]);
Expand Down

0 comments on commit b31aadb

Please sign in to comment.