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

[Laravel] Defer Translator + Simplify Provider #1777

Closed
wants to merge 3 commits into from

Conversation

barryvdh
Copy link
Contributor

@barryvdh barryvdh commented Jul 6, 2019

As suggested in ##1776 , maybe we can wait to load the actual Translator until it's used?

I'm also not sure why we need the Laravel Translator or the Event to get the current Locale. The getLocale() is available from the App container already.

@barryvdh
Copy link
Contributor Author

barryvdh commented Jul 6, 2019

Also I should probably note that I have not extensible tested it, but 'it works on my device' ;)

@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Jul 6, 2019

The locale is not a constant, it can change using App::setLocale('fr'); so we have to synchronize it on the event for locale change.

Copy link
Collaborator

@kylekatarnls kylekatarnls left a comment

Choose a reason for hiding this comment

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

@barryvdh
Copy link
Contributor Author

barryvdh commented Jul 6, 2019

Yes I understand the sync, but if you don't have the event dispatcher (for some reason, but mostly you have it anyways?) You can still get the current locale. Currently you are bailing out before setting it. And the translator doesn't have to be loaded just to get the Locale.

@barryvdh
Copy link
Contributor Author

barryvdh commented Jul 6, 2019

You are right about the benchmark/micro optimization. I think it would be required to test further to see what real life would change. It's a bit hard to test with opcache and all. It seems to switch a bit all the time, but it seems like it's a bit faster for non-localized requests and the same for localized requests. But need to find a good benchmark still.

setDefaultLocale:
string(17) "setDefaultLocale: 0.47ms"
string(38) "Now: Sat Jul 06 2019 22:49:54 GMT+0200"
string(13) "Boot: 1.483ms"
string(18) "Diff: 1 dag eerder"
string(23) "Localized boot: 2.018ms"

Current
string(18) "setLocale: 1.057ms"
string(38) "Now: za jul. 06 2019 22:50:15 GMT+0200"
string(13) "Boot: 1.796ms"
string(18) "Diff: 1 dag eerder"
string(23) "Localized boot: 2.076ms"

@barryvdh
Copy link
Contributor Author

barryvdh commented Jul 6, 2019

If you want I can create a PR for just the Laravel changes (eg. use the app locale directly instead of the translator)

@kylekatarnls
Copy link
Collaborator

I extracted it: #1779

Tests pass and it still work in my Laravel app.

Next step will be to make getLocale and setLocale to not trigger the lazy-load of the translator.

So I close this, but feel free to comment the other PR and propose the optimization you put in setDefaultLocale but directly in setLocale so everyone will benefit of it, not only Laravel users.

@barryvdh
Copy link
Contributor Author

barryvdh commented Jul 7, 2019

Thanks. The reason I created a new method was that I don't know the return value yet.

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

Successfully merging this pull request may close these issues.

2 participants