[FrameworkBundle] Allow dots in translation domains

This commit is contained in:
Jan Schädlich 2019-05-08 09:16:31 +02:00
parent 8e5b6573ea
commit 4b593b08d1
6 changed files with 31 additions and 3 deletions

View File

@ -1170,14 +1170,15 @@ class FrameworkExtension extends Extension
->followLinks()
->files()
->filter(function (\SplFileInfo $file) {
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
return 2 <= substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
})
->in($dirs)
->sortByName()
;
foreach ($finder as $file) {
list(, $locale) = explode('.', $file->getBasename(), 3);
$fileNameParts = explode('.', basename($file));
$locale = $fileNameParts[\count($fileNameParts) - 2];
if (!isset($files[$locale])) {
$files[$locale] = [];
}

View File

@ -0,0 +1,3 @@
domain:
with:
dots: It works!

View File

@ -817,6 +817,11 @@ abstract class FrameworkExtensionTest extends TestCase
$files,
'->registerTranslatorConfiguration() finds translation resources in default path'
);
$this->assertContains(
strtr(__DIR__.'/Fixtures/translations/domain.with.dots.en.yml', '/', \DIRECTORY_SEPARATOR),
$files,
'->registerTranslatorConfiguration() finds translation resources with dots in domain'
);
$calls = $container->getDefinition('translator.default')->getMethodCalls();
$this->assertEquals(['fr'], $calls[1][1][0]);

View File

@ -373,6 +373,21 @@ class TranslatorTest extends TestCase
$this->assertEquals('répertoire', $translator->trans('folder'));
}
public function testLoadingTranslationFilesWithDotsInMessageDomain()
{
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$resourceFiles = [
'en' => [
__DIR__.'/../Fixtures/Resources/translations/domain.with.dots.en.yml',
],
];
$translator = $this->getTranslator($loader, ['cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles], 'yml');
$translator->setLocale('en');
$translator->setFallbackLocales(['fr']);
$this->assertEquals('It works!', $translator->trans('message', [], 'domain.with.dots'));
}
private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $loaderFomat = 'loader', $defaultLocale = 'en')
{
if (null === $defaultLocale) {

View File

@ -165,7 +165,10 @@ class Translator extends BaseTranslator implements WarmableInterface
foreach ($filesByLocale as $locale => $files) {
foreach ($files as $key => $file) {
// filename is domain.locale.format
list($domain, $locale, $format) = explode('.', basename($file), 3);
$fileNameParts = explode('.', basename($file));
$format = array_pop($fileNameParts);
$locale = array_pop($fileNameParts);
$domain = implode('.', $fileNameParts);
$this->addResource($format, $file, $locale, $domain);
}
}