Skip to content

Commit

Permalink
add error if 2 base locales are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Jun 1, 2024
1 parent 585c093 commit 725e7af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Components/Configuration/Services/LocalesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function loadLocales(SimpleXMLElement $rootLocales, string $configFilenam

$localeName = (string)$nodeLocale['name'];
$iniSection = (string)$nodeLocale['iniSection'];
$isMain = isset($nodeLocale['base']) && $this->getBool((string)$nodeLocale['base']);
$isBase = isset($nodeLocale['base']) && $this->getBool((string)$nodeLocale['base']);

$localeFile = $this->placholderProcessor->buildRealFilename(
$localeName,
Expand All @@ -60,7 +60,16 @@ public function loadLocales(SimpleXMLElement $rootLocales, string $configFilenam
$configFilename
);

$foundLocales[] = new Locale($localeName, $isMain, $localeFile, $iniSection);
$foundLocales[] = new Locale($localeName, $isBase, $localeFile, $iniSection);
}

# check if we have 2 base locales, and throw an exception if so
$baseLocales = array_filter($foundLocales, function (Locale $locale): bool {
return $locale->isBase();
});

if (count($baseLocales) > 1) {
throw new ConfigurationException('Only 1 locale can be defined as the base locale within a translation-set');
}

return $foundLocales;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,41 @@ public function testBaseLocaleAttributeIsLoaded(): void
<locales basePath="./snippets">
<locale name="en">' . $this->existingLocaleFile . '</locale>
<locale name="de" base="true">' . $this->existingLocaleFile . '</locale>
<locale name="fr" base="false">' . $this->existingLocaleFile . '</locale>
</locales>
');

$locales = $this->loader->loadLocales($xmlNode, 'test.xml');

$this->assertCount(2, $locales);
$this->assertCount(3, $locales);

$this->assertEquals('en', $locales[0]->getName());
$this->assertEquals(false, $locales[0]->isBase());

$this->assertEquals('de', $locales[1]->getName());
$this->assertEquals(true, $locales[1]->isBase());

$this->assertEquals('fr', $locales[2]->getName());
$this->assertEquals(false, $locales[2]->isBase());
}

/**
* @throws ConfigurationException
* @return void
*/
public function testBaseLocaleAttributeCannotBeDefinedTwice(): void
{
$this->expectException(ConfigurationException::class);
$this->expectExceptionMessage('Only 1 locale can be defined as the base locale within a translation-set');

$xmlNode = $this->loadXml('
<locales basePath="./snippets">
<locale name="en" base="true">' . $this->existingLocaleFile . '</locale>
<locale name="de" base="true">' . $this->existingLocaleFile . '</locale>
</locales>
');

$this->loader->loadLocales($xmlNode, 'test.xml');
}

/**
Expand Down

0 comments on commit 725e7af

Please sign in to comment.