Skip to content

[Translation][FrameworkBundle] Fix resource loading order inconsistency reported in #23034 #23057

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

Closed
wants to merge 5 commits into from

Conversation

mpdude
Copy link
Contributor

@mpdude mpdude commented Jun 3, 2017

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #23034
License MIT
Doc PR

Fixes the bug reported in #23034:

When mixing addResource() calls and providing the resource_files option, the order in which resources are loaded depends on the kernel.debug setting and whether a cache is used.

In particular, when several loaders provide translations for the same message, the one that "wins" may change between development and production mode.

*
* @var array
*/
private $otherResources = array();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using this array to collect addResource() calls and actually execute them after the resource_files is ugly, but we don't have access to the private \Symfony\Component\Translation\Translator::$resources field so that we could "unshift" the resource_files in front of it in loadResources.

@mpdude
Copy link
Contributor Author

mpdude commented Jun 3, 2017

Is loading the resources immediately in the constructor (here) really necessary (why)? Commenting it out does at least not break tests.

@mpdude mpdude changed the title [Translator] Fix resource loading order inconsistency reported in #23034 [Translation] Fix resource loading order inconsistency reported in #23034 Jun 3, 2017
@mpdude mpdude changed the title [Translation] Fix resource loading order inconsistency reported in #23034 [Translation][FrameworkBundle] Fix resource loading order inconsistency reported in #23034 Jun 3, 2017
@nicolas-grekas nicolas-grekas added this to the 2.8 milestone Jun 4, 2017
@mpdude
Copy link
Contributor Author

mpdude commented Jun 8, 2017

@fabot / @nicolas-grekas does this look sensible? If you basically agree, I probably should rebase it onto 2.7.

@@ -38,6 +38,14 @@ class Translator extends BaseTranslator implements WarmableInterface
private $resourceLocales;

/**
* Holds parameters from addResource() calls so we can defer the acutal
Copy link
Member

@nicolas-grekas nicolas-grekas Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: actual
let's rebase on 2.7 if it applies there

@mpdude mpdude changed the base branch from 2.8 to 2.7 June 8, 2017 08:26
@mpdude
Copy link
Contributor Author

mpdude commented Jun 8, 2017

Rebased onto 2.7; no modifications were necessary

@fabpot
Copy link
Member

fabpot commented Jun 14, 2017

Thank you @mpdude.

@fabpot fabpot closed this Jun 14, 2017
fabpot added a commit that referenced this pull request Jun 14, 2017
…inconsistency reported in #23034 (mpdude)

This PR was squashed before being merged into the 2.7 branch (closes #23057).

Discussion
----------

[Translation][FrameworkBundle] Fix resource loading order inconsistency reported in #23034

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23034
| License       | MIT
| Doc PR        |

Fixes the bug reported in #23034:

When mixing `addResource()` calls and providing the `resource_files` option, the order in which resources are loaded depends on the `kernel.debug` setting and whether a cache is used.

In particular, when several loaders provide translations for the same message, the one that "wins" may change between development and production mode.

Commits
-------

2a9e65d [Translation][FrameworkBundle] Fix resource loading order inconsistency reported in #23034
@mpdude mpdude deleted the fix-23034 branch June 14, 2017 20:38
@jenkoian
Copy link
Contributor

My app has a custom loader to load translations from a REST service. This change broke it.

It broke because I was injecting the translator into the loader and calling addResource from within it, thus calling addResource after the initialize method was called.

Now I suspect this is my error, injecting the translator into a loader certainly doesn't sound the right thing to do.

So I was just after confirmation that this is indeed my issue and any tips on the best way to load resources outside of the loader would be greatly received too. I initially thought a compiler pass, but I don't think it's as simple as that? i.e. I couldn't call addMethodCall('addResource') for instance because I think that only works for an already constructed object.

@jenkoian
Copy link
Contributor

Note: I have now successfully used a compiler pass to work around this. Have opened a PR though to highlight the BC break, may decide it's not a use case we need to support but it's there for deciders anyway 👍

@stof
Copy link
Member

stof commented Jan 11, 2018

Creating a circular reference between the translator and the loader to have the loader reconfiguring the translator regarding what resources should be loaded by the loader is indeed a use case we never intended to support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants