-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.7][Translation] generate translation cache at warmup #13942
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
[2.7][Translation] generate translation cache at warmup #13942
Conversation
@@ -33,6 +33,7 @@ | |||
<parameter key="translation.loader.class">Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader</parameter> | |||
<parameter key="translation.extractor.class">Symfony\Component\Translation\Extractor\ChainExtractor</parameter> | |||
<parameter key="translation.writer.class">Symfony\Component\Translation\Writer\TranslationWriter</parameter> | |||
<parameter key="translation.warmer.class">Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer</parameter> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the class name in the attribute directly below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure to understand. You ask me to put my class name directly in my service declaration, that's right ?
If that's right, can you please explain me (for my personnal knowledge) why should I do it in this particular case ?
Thx,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Symfony used to use class parameters for a long time to make the class names configurable. Though there are better ways to modify services so that it was decided to not promote these parameters anymore and also don't introduce new ones (all services introduced with Symfony 2.6 don't declare them). The existing parameters have to be kept for BC, but they will be removed in Symfony 3.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thank you for your feedback
Thanks for your feedback @aitboudad, @xabbuh and @fabpot. I wait for the last feedback about |
if ($this->locale !== null) { | ||
$this->loadCatalogue($this->locale); | ||
} | ||
foreach ($this->fallbackLocales as $locale) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid this as it's already called in loadCatalogue,
see https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Translation/Translator.php#L344
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, that won't do the trick. If I try to load cz
catalogue and it's not a registered locale, I'll have a cz
catalogue with fallback translations (i.e fr
) computed in.
It won't generate catalogues for every fallback. Am I right ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, that sounds reasonable
@xavierleune Could you add some tests as well? |
@aitboudad actually I can't see what should be tested. If I want to check input / output, I don't have any output to test. I could test if catalogues are generated, but that would be a double test. Do you have any idea ? |
@xavierleune you can check if catalogues are generated when locale it's not registered |
050b8b4
to
f2c0226
Compare
@aitboudad I'm not sure that's very relevant |
*/ | ||
public function warmUp($cacheDir) | ||
{ | ||
if ($this->options['cache_dir'] !== null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should write null !== $this->options['cache_dir']
f2c0226
to
57cd942
Compare
@aitboudad thanks for your last feedback. For the records, the "Constructor." comment should be removed from RouterCacheWarmer also. |
@@ -152,5 +152,10 @@ | |||
<service id="translation.extractor" class="%translation.extractor.class%"/> | |||
|
|||
<service id="translation.writer" class="%translation.writer.class%"/> | |||
|
|||
<service id="translation.warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer" public="false"> | |||
<argument type="service" id="translator" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be translator.default
<argument type="service" id="translator.default" />
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why I should use translator.default. If I do that when a dev inject his own service which can be Warmable, he'll need to implement his own warmer but TranslationsCacheWarmer can do the job and is not specific to the class Translator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use the alias "translator", in dev mode this won't work as the translator return an instance of LoggingTranslator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LoggingTranslator implements TranslatorInterface but it does not implements WarmableInterface. So the cache file won't be generated but "it will work" as no error will be thrown. It would be sad to force developpers to duplicate code to have some cache in dev.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ops, ok let's keep it as it and inject translator.logging.inner for translation.warmer in https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/LoggingTranslatorPass.php#L40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks ! Now I know I must not check the french version of the documentation. Decorating Services are not in this version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
translator.logging.inner does not exists in prod env. What should I do ? This is useful mainly in prod env.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in prod we use translator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you didn't understand :D, in translation.xml we inject translator
<service id="translation.warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer" public="false">
<argument type="service" id="translator" />
<tag name="kernel.cache_warmer" />
</service>
and in LoggingTranslatorPass.php#L40
$container->getDefinition('translation.warmer')->replaceArgument(0, new Reference('translator.logging.inner'));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know... Like french people say I quickly understand but you have to explain to me a long time 😃
Anyway I understood and I'll push my update soon. Thks !
@xavierleune I just left 2 comments. this will probably be my last remark :) |
ping @xavierleune |
Yes I'm here... I was making a new PR for some other changes 😄 Thks |
57cd942
to
c56d47c
Compare
👍 |
public function warmUp($cacheDir) | ||
{ | ||
if (null !== $this->options['cache_dir']) { | ||
foreach (array_keys($this->resourceFiles) as $locale) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you update this part as #14022 has been merged to avoid a BC break?
5d8b58a
to
93b0f0c
Compare
ebc84cb
to
df1e752
Compare
@fabpot done, thank your for the feedback. |
@aitboudad the last word is for you :) |
{ | ||
if (null !== $this->options['cache_dir']) { | ||
if (null !== $this->locale) { | ||
$this->loadCatalogue($this->locale); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this will only load the catalogue for the current locale?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current locale can be not in the fallback locales (if the current is the default one). So I prefer to load the catalogue for the current locale and then for fallback locales.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loading all locales that are be part of $resourceFiles for me here make sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I rolled back because with last update in 2.7, it was broken. And I think it's weird to use file names to retrieve available locales.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok sound good :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aitboudad actually this is ok for prod environment but in dev, resourceFiles is empty. If that's ok for you I'll add test and push update today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi @xavierleune :D, just remove https://github.com/xavierleune/symfony/blob/feature/13919-translations-cache-warmer/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php#L95 and it will work in dev env.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aitboudad I guess it was here for a good reason. Seems it avoid a kind of infinite (or very long) loop somewhere.
Results for time php app/console cache:clear
in prod or dev environments :
Env | Type | Before | After |
---|---|---|---|
prod | real | 0m6.315s | 1m48.515s |
user | 0m3.224s | 1m28.898s | |
sys | 0m0.696s | 0m9.677s | |
---- | ---- | -------- | --------- |
dev | real | 0m3.364s | 2m0.263s |
user | 0m2.228s | 1m34.026s | |
sys | 0m0.448s | 0m13.129s |
I don't have time to investigate more. I'll add some tests and then I'll be done for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll see it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xavierleune fixed by aitboudad@106d04e can you include my commit into this PR or just allow me to commit into your fork.
df1e752
to
a83a7dd
Compare
👍, ping @fabpot |
a83a7dd
to
5000800
Compare
3f82dbe
to
d1015db
Compare
@aitboudad I cherry picked your changes, thx |
@aitboudad can you please take a look to the travis build ? I'm not sur why it's broken. All tests ran fine on my laptop... |
@xavierleune not related to this PR (#14002 need to be merged into master) |
@xavierleune Can you rebase to get rid of the merge commit? |
@fabpot just FYI I moved the property resourceFiles into options of Translator |
*/ | ||
public function warmUp($cacheDir) | ||
{ | ||
if ($this->translator instanceof WarmableInterface) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't understand, why we need check this here when we could change the constructor type hint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot. Otherwise we forbid decorating the translator with a non-warmable one (or we need to add complex logic in a compiler pass to determine whether we should disable this cache warmer entirely and remove it from the container to avoid being broken)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually already do some reflection stuff in that compiler pass. But yeah, it's probably not worth to put much effort in this though.
4a926f2
to
f5f8395
Compare
@fabpot done |
|
||
/** | ||
* | ||
* @param TranslatorInterface $translator A Translator instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no value at all in this phpdoc. It only duplicates the signature
f5f8395
to
58799d0
Compare
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function warmUp($cacheDir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public methods should be before protected and private ones
58799d0
to
94d3876
Compare
ping @fabpot, this is ready for merge. |
Thank you @xavierleune. |
…p (xavierleune) This PR was merged into the 2.7 branch. Discussion ---------- [2.7][Translation] generate translation cache at warmup | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13919 | License | MIT | Doc PR | NA This PR uses the parameters "locale" and "fallback_locales" to generate the catalogues at warmup, avoiding the creation of files at runtime. Commits ------- 94d3876 FIX #13919 added TranslationsCacheWarmer to generate catalogues at warmup
…ot used. (aitboudad) This PR was merged into the 2.7 branch. Discussion ---------- [FrameworkBundle][Translation] skip warmUp when cache is not used. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Fixed tickets | #13942 | Tests pass? | yes | License | MIT Commits ------- efb10f6 [FrameworkBundle][Translation] skip warmUp when cache is not used.
This PR uses the parameters "locale" and "fallback_locales" to generate the catalogues at warmup, avoiding the creation of files at runtime.