Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-9088: Helfi locale-import #152

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading