Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Translation] Message domains with dot not working #31400

Closed
mfrieling opened this issue May 6, 2019 · 1 comment
Closed

[Translation] Message domains with dot not working #31400

mfrieling opened this issue May 6, 2019 · 1 comment

Comments

@mfrieling
Copy link

Symfony version(s) affected: 4.2.x

Description
Translation message domains containing a dot (.) are not loaded, neither with nor without caching.

How to reproduce
Create a translation in any controller or twig template using a message domain like app.security:

// ...

class SecurityController extends AbstractController {
  /**
   * @Route("/login", name="app_login")
   */
  public function login(AuthenticationUtils $authenticationUtils, TranslatorInterface $translator): Response {
    // ...
    $test = $translator->trans('login.page.title', [], 'app.security', 'en');
    // ...
  }
}

Extracting translations with php bin/console translation:update --dump-messages --force en will correctly create the file translations/app.security.en.xlf with the login.page.title translation. But even a rm -rf var/cache && php bin/console clear:cache will never load this translation file.

After spending hours with searching in the Symfony docs, Google and Stack Overflow I was sure that translation is configured correctly and must work. So I removed the message domain and the translations/messages.en.xlf got loaded. Then I tried another message domain security and the seucrity.en.xlf got loaded as well. This lead me to the conclusion that there must be a bug with message domains containing a dot only, as further tests with dashed worked fine.

Possible Solution
I could not locate where the bug happens because that might be in generated code and I don't know where this code is generated (some compiler passes possibly). So please, either fix this bug or if there is a reason why message domains must not contain dots document this clearly everywhere where message domains are mentioned (or link to Translation Component - Using Message Domains where it will be documented) including the why, because in my opinion the concept of Keyword Messages should not only be able to apply to message keys but message domains as well.

And if ti will not be supported, of course change the message extractor to report an error when it finds a message domain containing a dot instead of correctly generating the translation files.

@jschaedl
Copy link
Contributor

jschaedl commented May 8, 2019

I created a small sample project to reproduce this issue: https://github.com/jschaedl/symfony-issue-31400

The problem is that the FrameworkBundles Extension class loads the translation resources only if the filename contains two .. See:

return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());

I don't see a reason why there is such a restriction. I think we can easily fix this and allow domain names with ..

We need to adjust this line:

return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());

and the Translator::addResource(...)method here:

list($domain, $locale, $format) = explode('.', basename($file), 3);

WDYT?

@fabpot fabpot closed this as completed Jun 11, 2019
fabpot added a commit that referenced this issue Jun 11, 2019
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants