Skip to content

[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

Merged

Conversation

xavierleune
Copy link
Contributor

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.

@@ -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>
Copy link
Member

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.

Copy link
Contributor Author

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,

Copy link
Member

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.

Copy link
Contributor Author

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

@fabpot fabpot changed the title FIX #13919 added TranslationsCacheWarmer to generate catalogues at warmup added TranslationsCacheWarmer to generate catalogues at warmup Mar 17, 2015
@xavierleune
Copy link
Contributor Author

Thanks for your feedback @aitboudad, @xabbuh and @fabpot. I wait for the last feedback about $cacheDir before I push a new commit.

if ($this->locale !== null) {
$this->loadCatalogue($this->locale);
}
foreach ($this->fallbackLocales as $locale) {
Copy link
Contributor

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

Copy link
Contributor Author

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 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, that sounds reasonable

@aitboudad
Copy link
Contributor

@xavierleune Could you add some tests as well?

@xavierleune
Copy link
Contributor Author

@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 ?

@aitboudad
Copy link
Contributor

@xavierleune you can check if catalogues are generated when locale it's not registered

@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from 050b8b4 to f2c0226 Compare March 17, 2015 09:02
@xavierleune
Copy link
Contributor Author

@aitboudad I'm not sure that's very relevant

*/
public function warmUp($cacheDir)
{
if ($this->options['cache_dir'] !== null) {
Copy link
Contributor

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']

@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from f2c0226 to 57cd942 Compare March 17, 2015 10:20
@xavierleune
Copy link
Contributor Author

@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" />
Copy link
Contributor

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" />

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor

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'));

Copy link
Contributor Author

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 !

@aitboudad
Copy link
Contributor

@xavierleune I just left 2 comments. this will probably be my last remark :)

@aitboudad
Copy link
Contributor

ping @xavierleune

@xavierleune
Copy link
Contributor Author

Yes I'm here... I was making a new PR for some other changes 😄
I'll test your suggestions today.

Thks

@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from 57cd942 to c56d47c Compare March 23, 2015 17:07
@dunglas
Copy link
Member

dunglas commented Mar 23, 2015

👍

public function warmUp($cacheDir)
{
if (null !== $this->options['cache_dir']) {
foreach (array_keys($this->resourceFiles) as $locale) {
Copy link
Member

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?

@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from 5d8b58a to 93b0f0c Compare March 24, 2015 13:26
@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch 2 times, most recently from ebc84cb to df1e752 Compare March 24, 2015 13:52
@xavierleune
Copy link
Contributor Author

@fabpot done, thank your for the feedback.

@fabpot
Copy link
Member

fabpot commented Mar 24, 2015

@aitboudad the last word is for you :)

{
if (null !== $this->options['cache_dir']) {
if (null !== $this->locale) {
$this->loadCatalogue($this->locale);
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok sound good :)

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll see it

Copy link
Contributor

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.

@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from df1e752 to a83a7dd Compare March 24, 2015 20:39
@aitboudad
Copy link
Contributor

👍, ping @fabpot

@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from a83a7dd to 5000800 Compare March 26, 2015 09:56
@aitboudad aitboudad changed the title added TranslationsCacheWarmer to generate catalogues at warmup [2.7][Translation] generate translation cache at warmup Mar 29, 2015
@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from 3f82dbe to d1015db Compare March 30, 2015 09:01
@xavierleune
Copy link
Contributor Author

@aitboudad I cherry picked your changes, thx

@xavierleune
Copy link
Contributor Author

@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...

@aitboudad
Copy link
Contributor

@xavierleune not related to this PR (#14002 need to be merged into master)

@fabpot
Copy link
Member

fabpot commented Mar 30, 2015

@xavierleune Can you rebase to get rid of the merge commit?

@aitboudad
Copy link
Contributor

@fabpot just FYI I moved the property resourceFiles into options of Translator

*/
public function warmUp($cacheDir)
{
if ($this->translator instanceof WarmableInterface) {
Copy link
Member

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.

Copy link
Member

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)

Copy link
Member

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.

@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from 4a926f2 to f5f8395 Compare March 31, 2015 10:23
@xavierleune
Copy link
Contributor Author

@fabpot done
@aitboudad all right, thx


/**
*
* @param TranslatorInterface $translator A Translator instance
Copy link
Member

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

@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from f5f8395 to 58799d0 Compare March 31, 2015 10:53
/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
Copy link
Member

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

@xavierleune xavierleune force-pushed the feature/13919-translations-cache-warmer branch from 58799d0 to 94d3876 Compare March 31, 2015 12:08
@aitboudad
Copy link
Contributor

ping @fabpot, this is ready for merge.

@aitboudad
Copy link
Contributor

Thank you @xavierleune.

@aitboudad aitboudad merged commit 94d3876 into symfony:2.7 Apr 3, 2015
aitboudad added a commit that referenced this pull request Apr 3, 2015
…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
aitboudad added a commit that referenced this pull request Apr 27, 2015
…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.
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.

8 participants