Skip to content

Commit e70b709

Browse files
committed
Implemented filtering duplicated default translations.
1 parent 0496170 commit e70b709

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed

src/Parser/TranslationParser.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use BluePsyduck\FactorioTranslator\Exception\NoSupportedLoaderException;
88
use BluePsyduck\FactorioTranslator\Translator;
9+
use FactorioItemBrowser\Common\Constant\Defaults;
910
use FactorioItemBrowser\Export\Entity\Dump\Dump;
1011
use FactorioItemBrowser\Export\Output\Console;
1112
use FactorioItemBrowser\Export\Service\ModFileService;
@@ -70,5 +71,17 @@ public function translate(
7071
$translations->set($locale, $value);
7172
}
7273
}
74+
75+
$this->filterDuplicates($translations);
76+
}
77+
78+
protected function filterDuplicates(DictionaryInterface $translations): void
79+
{
80+
$defaultTranslation = $translations->get(Defaults::LOCALE);
81+
foreach ($translations as $locale => $translation) {
82+
if ($locale !== Defaults::LOCALE && $translation === $defaultTranslation) {
83+
$translations->set($locale, '');
84+
}
85+
}
7386
}
7487
}

test/src/Parser/TranslationParserTest.php

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
use FactorioItemBrowser\Export\Service\ModFileService;
1414
use FactorioItemBrowser\Export\Parser\TranslationParser;
1515
use FactorioItemBrowser\ExportData\Collection\DictionaryInterface;
16+
use FactorioItemBrowser\ExportData\Collection\TranslationDictionary;
1617
use FactorioItemBrowser\ExportData\ExportData;
1718
use PHPUnit\Framework\MockObject\MockObject;
1819
use PHPUnit\Framework\TestCase;
20+
use ReflectionException;
1921

2022
/**
2123
* The PHPUnit test of the TranslationParser class.
@@ -42,13 +44,21 @@ protected function setUp(): void
4244
$this->translator = $this->createMock(Translator::class);
4345
}
4446

45-
private function createInstance(): TranslationParser
47+
/**
48+
* @param array<string> $mockedMethods
49+
* @return TranslationParser&MockObject
50+
*/
51+
private function createInstance(array $mockedMethods = []): TranslationParser
4652
{
47-
return new TranslationParser(
48-
$this->console,
49-
$this->modFileManager,
50-
$this->translator,
51-
);
53+
return $this->getMockBuilder(TranslationParser::class)
54+
->disableProxyingToOriginalMethods()
55+
->onlyMethods($mockedMethods)
56+
->setConstructorArgs([
57+
$this->console,
58+
$this->modFileManager,
59+
$this->translator,
60+
])
61+
->getMock();
5262
}
5363

5464
/**
@@ -139,7 +149,66 @@ public function testTranslate(): void
139149
'',
140150
);
141151

142-
$instance = $this->createInstance();
152+
$instance = $this->createInstance(['filterDuplicates']);
153+
$instance->expects($this->once())
154+
->method('filterDuplicates')
155+
->with($this->identicalTo($translations));
156+
143157
$instance->translate($translations, $localisedString, $fallbackLocalisedString);
144158
}
159+
160+
/**
161+
* @return array<mixed>
162+
*/
163+
public function provideFilterDuplicates(): array
164+
{
165+
// Translations with duplication
166+
$translations1 = new TranslationDictionary();
167+
$translations1->set('en', 'abc');
168+
$translations1->set('de', 'abc');
169+
$translations1->set('fr', 'def');
170+
$translations1->set('ja', 'def');
171+
$expectedTranslations1 = new TranslationDictionary();
172+
$expectedTranslations1->set('en', 'abc');
173+
$expectedTranslations1->set('fr', 'def');
174+
$expectedTranslations1->set('ja', 'def');
175+
176+
// Translations without duplication
177+
$translations2 = new TranslationDictionary();
178+
$translations2->set('en', 'abc');
179+
$translations2->set('de', 'def');
180+
$translations2->set('fr', 'ghi');
181+
$expectedTranslations2 = new TranslationDictionary();
182+
$expectedTranslations2->set('en', 'abc');
183+
$expectedTranslations2->set('de', 'def');
184+
$expectedTranslations2->set('fr', 'ghi');
185+
186+
// Translations without English will never filter
187+
$translations3 = new TranslationDictionary();
188+
$translations3->set('de', 'abc');
189+
$translations3->set('fr', 'abc');
190+
$translations3->set('ja', 'ghi');
191+
$expectedTranslations3 = new TranslationDictionary();
192+
$expectedTranslations3->set('de', 'abc');
193+
$expectedTranslations3->set('fr', 'abc');
194+
$expectedTranslations3->set('ja', 'ghi');
195+
196+
return [
197+
[$translations1, $expectedTranslations1],
198+
];
199+
}
200+
201+
/**
202+
* @throws ReflectionException
203+
* @dataProvider provideFilterDuplicates
204+
*/
205+
public function testFilterDuplicates(
206+
DictionaryInterface $translations,
207+
DictionaryInterface $expectedTranslations
208+
): void {
209+
$instance = $this->createInstance();
210+
$this->invokeMethod($instance, 'filterDuplicates', $translations);
211+
212+
$this->assertEquals($expectedTranslations, $translations);
213+
}
145214
}

0 commit comments

Comments
 (0)