Skip to content

Commit c295f57

Browse files
committed
itk_translation_extractor
1 parent 14731d3 commit c295f57

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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)