Skip to content

Commit

Permalink
feature #31451 [FrameworkBundle] Allow dots in translation domains (j…
Browse files Browse the repository at this point in the history
…schaedl)

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
  • Loading branch information
fabpot committed Jun 11, 2019
2 parents 7f39f36 + 4b593b0 commit c33f69c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
Expand Up @@ -1167,14 +1167,15 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
->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] = [];
}
Expand Down
@@ -0,0 +1,3 @@
domain:
with:
dots: It works!
Expand Up @@ -817,6 +817,11 @@ public function testTranslator()
$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]);
Expand Down
@@ -0,0 +1 @@
message: It works!
Expand Up @@ -373,6 +373,21 @@ public function testWarmup()
$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) {
Expand Down
Expand Up @@ -165,7 +165,10 @@ private function addResourceFiles()
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);
}
}
Expand Down

0 comments on commit c33f69c

Please sign in to comment.