Skip to content

Commit 1693d99

Browse files
committed
[FrameworkBundle] Allow dots in translation domains
1 parent 371dfc7 commit 1693d99

File tree

7 files changed

+32
-4
lines changed

7 files changed

+32
-4
lines changed

phpunit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (!file_exists(__DIR__.'/vendor/symfony/phpunit-bridge/bin/simple-phpunit')) {
88
exit(1);
99
}
1010
if (\PHP_VERSION_ID >= 70000 && !getenv('SYMFONY_PHPUNIT_VERSION')) {
11-
putenv('SYMFONY_PHPUNIT_VERSION=6.5');
11+
putenv('SYMFONY_PHPUNIT_VERSION=6.5.11');
1212
}
1313
putenv('SYMFONY_PHPUNIT_DIR='.__DIR__.'/.phpunit');
1414
require __DIR__.'/vendor/symfony/phpunit-bridge/bin/simple-phpunit';

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,14 +1153,15 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
11531153
->followLinks()
11541154
->files()
11551155
->filter(function (\SplFileInfo $file) {
1156-
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
1156+
return 2 <= substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
11571157
})
11581158
->in($dirs)
11591159
->sortByName()
11601160
;
11611161

11621162
foreach ($finder as $file) {
1163-
list(, $locale) = explode('.', $file->getBasename(), 3);
1163+
$fileNameParts = explode('.', basename($file));
1164+
$locale = $fileNameParts[\count($fileNameParts) - 2];
11641165
if (!isset($files[$locale])) {
11651166
$files[$locale] = [];
11661167
}
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
@@ -816,6 +816,11 @@ public function testTranslator()
816816
$files,
817817
'->registerTranslatorConfiguration() finds translation resources in default path'
818818
);
819+
$this->assertContains(
820+
strtr(__DIR__.'/Fixtures/translations/domain.with.dots.en.yml', '/', \DIRECTORY_SEPARATOR),
821+
$files,
822+
'->registerTranslatorConfiguration() finds translation resources with dots in domain'
823+
);
819824

820825
$calls = $container->getDefinition('translator.default')->getMethodCalls();
821826
$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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy