8000 [FrameworkBundle] Allow dots in translation domains · symfony/symfony@4b593b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b593b0

Browse files
committed
[FrameworkBundle] Allow dots in translation domains
1 parent 8e5b657 commit 4b593b0

File tree

6 files changed

+31
-3
lines changed

6 files changed

+31
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,14 +1170,15 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
11701170
->followLinks()
11711171
->files()
11721172
->filter(function (\SplFileInfo $file) {
1173-
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
1173+
return 2 <= substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
11741174
})
11751175
->in($dirs)
11761176
->sortByName()
11771177
;
11781178

11791179
foreach ($finder as $file) {
1180-
list(, $locale) = explode('.', $file->getBasename(), 3);
1180+
$fileNameParts = explode('.', basename($file));
1181+
$locale = $fileNameParts[\count($fileNameParts) - 2];
11811182
if (!isset($files[$locale])) {
11821183
$files[$locale] = [];
11831184
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
domain:
2+
with:
3+
dots: It works!

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,11 @@ public function testTranslator()
817817
$files,
818818
'->registerTranslatorConfiguration() finds translation resources in default path'
819819
);
820+
$this->assertContains(
821+
strtr(__DIR__.'/Fixtures/translations/domain.with.dots.en.yml', '/', \DIRECTORY_SEPARATOR),
822+
$files,
823+
'->registerTranslatorConfiguration() finds translation resources with dots in domain'
824+
);
820825

821826
$calls = $container->getDefinition('translator.default')->getMethodCalls();
822827
$this->assertEquals(['fr'], $calls[1][1][0]);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
message: It works!

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,21 @@ public function testWarmup()
373373
$this->assertEquals('répertoire', $translator->trans('folder'));
374374
}
375375

376+
public function testLoadingTranslationFilesWithDotsInMessageDomain()
377+
{
378+
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
379+
$resourceFiles = [
380+
'en' => [
381+
__DIR__.'/../Fixtures/Resources/translations/domain.with.dots.en.yml',
382+
],
383+
];
384+
385+
$translator = $this->getTranslator($loader, ['cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles], 'yml');
386+
$translator->setLocale('en');
387+
$translator->setFallbackLocales(['fr']);
388+
$this->assertEquals('It works!', $translator->trans('message', [], 'domain.with.dots'));
389+
}
390+
376391
private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $loaderFomat = 'loader', $defaultLocale = 'en')
377392
{
378393
if (null === $defaultLocale) {

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ private function addResourceFiles()
165165
foreach ($filesByLocale as $locale => $files) {
166166
foreach ($files as $key => $file) {
167167
// filename is domain.locale.format
168-
list($domain, $locale, $format) = explode('.', basename($file), 3);
168+
$fileNameParts = explode('.', basename($file));
169+
$format = array_pop($fileNameParts);
170+
$locale = array_pop($fileNameParts);
171+
$domain = implode('.', $fileNameParts);
169172
$this->addResource($format, $file, $locale, $domain);
170173
}
171174
}

0 commit comments

Comments
 (0)
0