Skip to content

Commit 7fdbeb2

Browse files
committed
Made empty translations fuzzy
1 parent 8ca7618 commit 7fdbeb2

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/Command/TranslationExtractCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
248248
'xliff_version' => $xliffVersion,
249249
'as_tree' => $input->getOption('as-tree'),
250250
'inline' => $input->getOption('as-tree') ?? 0,
251+
'empty_prefix' => $input->getOption('prefix'),
251252
];
252253

253254
$this->writer->write($operationResult, $format, $dumperOptions);

src/Translation/Dumper/PoFileDumper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function formatCatalogue(
3939
$header->setProjectName($projectName);
4040
}
4141

42+
$emptyPrefix = $options['empty_prefix'] ?? null;
43+
4244
$uri = tempnam(sys_get_temp_dir(), 'po_');
4345
$writer = new PoStreamWriter();
4446
$writer->setURI($uri);
@@ -47,6 +49,9 @@ public function formatCatalogue(
4749
foreach ($messages->getDomains() as $domain) {
4850
foreach ($messages->all($domain) as $source => $translation) {
4951
$item = new PoItem();
52+
if (empty($translation) || ($emptyPrefix && str_starts_with($translation, $emptyPrefix))) {
53+
$item->setFuzzy();
54+
}
5055
$item->setContext(PoItem::formatContext($domain));
5156
$source = PoItem::splitStrings($source);
5257
$translation = PoItem::splitStrings($translation, $numberOfPlurals);

src/Translation/Dumper/PoItem.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ class PoItem extends \Drupal\Component\Gettext\PoItem
66
{
77
public const string NO_CONTEXT = '__no_context__';
88

9+
private bool $fuzzy = false;
10+
11+
public function setFuzzy(bool $fuzzy = true): self
12+
{
13+
$this->fuzzy = $fuzzy;
14+
15+
return $this;
16+
}
17+
18+
public function __toString()
19+
{
20+
$string = parent::__toString();
21+
22+
if ($this->fuzzy) {
23+
// https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/gettext.html#Fuzzy-Entries
24+
$string = '#, fuzzy'."\n".$string;
25+
}
26+
27+
return $string;
28+
}
29+
930
public static function joinStrings(array $strings, string $prefix = ''): string
1031
{
1132
return implode(self::DELIMITER, array_map(fn (string $string) => $prefix.$string, $strings));

tests/Unit/Extractor/PoFileDumperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testFormatCatalogDaWithPrefix(): void
6464
$dumper = new PoFileDumper();
6565
$output = $dumper->formatCatalogue($messages, '', [
6666
'project_name' => 'testFormatCatalog',
67+
'empty_prefix' => '__',
6768
]);
6869

6970
$strings = [
@@ -78,6 +79,13 @@ public function testFormatCatalogDaWithPrefix(): void
7879
'msgstr[0] "__Hello star."',
7980
'msgstr[1] "__Hello @count stars."',
8081
]),
82+
83+
$this->block([
84+
'#, fuzzy',
85+
'msgctxt "the context"',
86+
'msgid "t filter with options context"',
87+
'msgstr "__t filter with options context"',
88+
]),
8189
];
8290
foreach ($strings as $string) {
8391
$this->assertStringContainsString($string, $output);

0 commit comments

Comments
 (0)