Skip to content

Commit 50a8b99

Browse files
committed
itk_translation_extractor
1 parent 14731d3 commit 50a8b99

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/Command/TranslationExtractCommand.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function configure(): void
8383
new InputOption('dump-messages', null, InputOption::VALUE_NONE, 'Should the messages be dumped in the console'),
8484
new InputOption('force', null, InputOption::VALUE_NONE, 'Should the extract be done'),
8585
new InputOption('clean', null, InputOption::VALUE_NONE, 'Should clean not found messages'),
86-
// new InputOption('domain', null, InputOption::VALUE_REQUIRED, 'Specify the domain to extract'),
86+
new InputOption('domain', null, InputOption::VALUE_REQUIRED, 'Specify the domain to extract'),
8787
new InputOption('sort', null, InputOption::VALUE_REQUIRED, 'Return list of messages sorted alphabetically'),
8888
new InputOption('as-tree', null, InputOption::VALUE_REQUIRED, 'Dump the messages as a tree-like structure: The given value defines the level where to switch to inline YAML'),
8989

@@ -201,12 +201,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
201201
// $currentCatalogue = $this->loadCurrentMessages($input->getArgument('locale'), $transPaths);
202202

203203
$io->comment('Loading translated messages...');
204-
$currentCatalogue = $this->loadTranslatedMessages($extractedCatalogue);
204+
$currentCatalogue = true === $input->getOption('no-fill')
205+
? $extractedCatalogue
206+
: $this->loadTranslatedMessages($extractedCatalogue);
205207

206-
// if (null !== $domain = $input->getOption('domain')) {
207-
// $currentCatalogue = $this->filterCatalogue($currentCatalogue, $domain);
208-
// $extractedCatalogue = $this->filterCatalogue($extractedCatalogue, $domain);
209-
// }
208+
if (null !== $domain = $input->getOption('domain')) {
209+
if ('' === $domain) {
210+
$domain = PoItem::NO_CONTEXT;
211+
}
212+
$currentCatalogue = $this->filterCatalogue($currentCatalogue, $domain);
213+
$extractedCatalogue = $this->filterCatalogue($extractedCatalogue, $domain);
214+
}
210215

211216
// process catalogues
212217
$operation = $input->getOption('clean')
@@ -451,7 +456,6 @@ private function loadTranslatedMessages(MessageCatalogue $extractedCatalogue)
451456
if ($translation = ($translations[$source] ?? null)) {
452457
if ($string = $translation->getString()) {
453458
$currentCatalogue->set($source, $string, $domain);
454-
$currentCatalogue->setMetadata($source, ['plurals' => $translation->getPlurals()], $domain);
455459
}
456460
}
457461
}

src/Translation/TwigExtractor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public function __construct(
2222
parent::__construct($this->twig);
2323
}
2424

25+
public function setPrefix(string $prefix): void
26+
{
27+
$this->prefix = $prefix;
28+
}
29+
2530
protected function extractTemplate(string $template, MessageCatalogue $catalogue): void
2631
{
2732
$visitor = $this->twig->getExtension(ItkTranslationExtractorTwigExtension::class)->getTranslationNodeVisitor();
@@ -31,7 +36,7 @@ protected function extractTemplate(string $template, MessageCatalogue $catalogue
3136
foreach ($visitor->getMessages() as $message) {
3237
$id = trim($message[0]);
3338
// $translation = Helper::joinStrings(...array_map(static fn (string $string) => '', [...Helper::splitStrings($id)]));
34-
$translation = $this->prefix.trim($message[0]);
39+
$translation = PoItem::joinStrings(...array_map(fn (string $string) => $this->prefix.$string, PoItem::splitStrings($id)));
3540
$domain = $message[1] ?: PoItem::NO_CONTEXT;
3641
$catalogue->set($id, $translation, $domain);
3742
}

tests/Unit/Extractor/PoFileDumperTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,39 @@ public function testFormatCatalogDa(): void
5151
}
5252
}
5353

54+
public function testFormatCatalogDaWithPrefix(): void
55+
{
56+
$resource = __DIR__.'/resources/';
57+
$locale = 'da';
58+
59+
$extractor = new TwigExtractor($this->twig());
60+
$messages = new MessageCatalogue($locale);
61+
$extractor->setPrefix('__');
62+
$extractor->extract($resource, $messages);
63+
64+
$dumper = new PoFileDumper();
65+
$output = $dumper->formatCatalogue($messages, '', [
66+
'project_name' => 'testFormatCatalog',
67+
]);
68+
69+
$strings = [
70+
$this->block([
71+
'msgctxt "the context"',
72+
'msgid "t filter with options context"',
73+
'msgstr "__t filter with options context"',
74+
]),
75+
$this->block([
76+
'msgid "Hello star."',
77+
'msgid_plural "Hello @count stars."',
78+
'msgstr[0] "__Hello star."',
79+
'msgstr[1] "__Hello @count stars."',
80+
]),
81+
];
82+
foreach ($strings as $string) {
83+
$this->assertStringContainsString($string, $output);
84+
}
85+
}
86+
5487
public function testFormatCatalogPl(): void
5588
{
5689
$resource = __DIR__.'/resources/';

0 commit comments

Comments
 (0)