Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c33f69c

Browse files
committed
feature #31451 [FrameworkBundle] Allow dots in translation domains (jschaedl)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle] Allow dots in translation domains | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #31400 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | tbd. <!-- required for new features --> ### Description With this fix it is now possible to have `.` in translation domains like `app.security.en.yaml`. ### Todo - [x] add a test case Commits ------- 4b593b0 [FrameworkBundle] Allow dots in translation domains
2 parents 7f39f36 + 4b593b0 commit c33f69c

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
@@ -1167,14 +1167,15 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
11671167
->followLinks()
11681168
->files()
11691169
->filter(function (\SplFileInfo $file) {
1170-
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
1170+
return 2 <= substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
11711171
})
11721172
->in($dirs)
11731173
->sortByName()
11741174
;
11751175

11761176
foreach ($finder as $file) {
1177-
list(, $locale) = explode('.', $file->getBasename(), 3);
1177+
$fileNameParts = explode('.', basename($file));
1178+
$locale = $fileNameParts[\count($fileNameParts) - 2];
11781179
if (!isset($files[$locale])) {
11791180
$files[$locale] = [];
11801181
}
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)