-
Notifications
You must be signed in to change notification settings - Fork 29.5k
[IMP] core: Environment._ for translations and LazyTranslate #174844
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
Conversation
b2471ba
to
b45ee0f
Compare
b45ee0f
to
74e4d2f
Compare
Performance improvementTranslationdef test_(self):
return _("some string")
def testenv(self):
return self.env._("some string")
def testenvmod(self):
return self.env._("some string", _mod='base')
self_fr = self.with_context(lang="fr_BE")
%timeit test_(self)
# before: 1.27 μs ± 14.3 ns per loop
# after: 1.4 μs ± 17.6 ns per loop
%timeit test_(self_fr)
# before: 11 μs ± 40.8 ns per loop
# after: 12.5 μs ± 157 ns per loop
%timeit testenv(self)
# 286 ns ± 1.52 ns per loop
%timeit testenv(self_fr)
# 11.4 μs ± 73.3 ns per loop
%timeit testenvmod(self)
# 290 ns ± 0.57 ns per loop
%timeit testenvmod(self_fr)
# 467 ns ± 1.02 ns per loop There is one more function call and isinstance checks that add ~150ns to the original function. Lazy# before
%timeit _lt("hello world")
# 45.3 μs ± 201 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
# after
%timeit lt = LazyTranslate('odoo.addons.test')
# 2.63 μs ± 27.3 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
%timeit lt("hello world")
# 4.05 μs ± 50 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) For 10 translations we go from 450μs to 43μs: 10x improvement. |
74e4d2f
to
931a666
Compare
3d55405
to
03adb5c
Compare
Hi, the following lint detects all the old cases using And there are a lot of occurrences: My question is, what is the plan with all these sentences using the old way I mean, it will be migrated to new |
Probably someday we will script that migration, but there are cases where it is not 100% equivalent; right now it is during review that people can prefer one over the other. The old |
Related to odoo/odoo#174844 The new way to translate is using `self.env._()` instead of `_` method But if there is not environment available in the code you can use LazyTranslate ```python from odoo. tools import LazyTranslate _lt = LazyTranslate(__name__) ```
Related to odoo/odoo#174844 The new way to translate is using `self.env._()` instead of `_` method But if there is not environment available in the code you can use LazyTranslate ```python from odoo. tools import LazyTranslate _lt = LazyTranslate(__name__) ``` Co-authored-by: trisdoan <doanminhtri8183@gmail.com>
- The es.po translation file was updated. - The check_access_rights method was updated to check_access. [1] - The _filter_access_rules_python method was updated to _filtered_access. [2] - String translation calls using _() were replaced with self.env._(). [3] - Version numbers were updated from "17.0" to "18.0". - Tree views were updated to list views in templates. [4] - The field `is_editable` is removed from the picking form view, as invisible fields are no longer required due to [5]. [1]: odoo/odoo#179148 [2]: odoo/odoo#179148 [3]: odoo/odoo#174844 [4]: odoo/odoo#159909 [5]: odoo/odoo#137031
- The es.po translation file was updated. - The check_access_rights method was updated to check_access. [1] - The _filter_access_rules_python method was updated to _filtered_access. [1] - String translation calls using _() were replaced with self.env._(). [2] - Version numbers were updated from "17.0" to "18.0". - Tree views were updated to list views in templates. [3] - The field `is_editable` is removed from the picking form view, as invisible fields are no longer required due to [4]. [1]: odoo/odoo#179148 [2]: odoo/odoo#174844 [3]: odoo/odoo#159909 [4]: odoo/odoo#137031
- The es.po translation file was updated. - The check_access_rights method was updated to check_access. [1] - The _filter_access_rules_python method was updated to _filtered_access. [1] - String translation calls using _() were replaced with self.env._(). [2] - Version numbers were updated from "17.0" to "18.0". - Tree views were updated to list views in templates. [3] - The field `is_editable` is removed from the picking form view, as invisible fields are no longer required due to [4]. [1]: odoo/odoo#179148 [2]: odoo/odoo#174844 [3]: odoo/odoo#159909 [4]: odoo/odoo#137031
Python translations need to know the language and the module to find the translated term. Today's
_
function must resolve both of these values from the current stack by using some heuristics.These heuristics don't always work properly: to find the language we look in the caller's scope to find it in the context or to find the environment from which we can read lang.
For the module, if the function is used outside of an addon, we default to 'base'.
We add a function
Environment._
for translations. That function uses itslang
property for the language without resorting to inspecting the call stack. You can also pass the module, but we can still resolve it dynamically.For lazy translations, we create a new
LazyTranslate
factory that you can use as_lt = LazyTranslate(__name__)
in any addon. This will resolve the addon name once per file from the name of the package without going throughinspect
. You can then useEnvironment._
to get the translation orstr()
to resolve the lanugage dynamically.odoo/enterprise#67528
odoo/documentation#10421
task-4034999
I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr