Skip to content

[IMP] core: Environment._ for translations #10421

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 1 commit into from

Conversation

kmagusiak
Copy link
Contributor

@kmagusiak kmagusiak commented Jul 29, 2024

@robodoo
Copy link
Collaborator

robodoo commented Jul 29, 2024

Pull request status dashboard

@kmagusiak kmagusiak force-pushed the master-env-translate-obj-krma branch 2 times, most recently from 3aa31cc to b441a35 Compare July 29, 2024 19:40
@kmagusiak kmagusiak marked this pull request as ready for review July 30, 2024 10:18
@C3POdoo C3POdoo requested review from a team, rco-odoo and HydrionBurst and removed request for a team July 30, 2024 10:21
Copy link
Collaborator

@AntoineVDV AntoineVDV left a comment

Choose a reason for hiding this comment

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

@robodoo delegate+

@kmagusiak kmagusiak force-pushed the master-env-translate-obj-krma branch 7 times, most recently from 2c0c820 to 9128522 Compare August 1, 2024 08:45
@kmagusiak kmagusiak force-pushed the master-env-translate-obj-krma branch 2 times, most recently from 7fb7524 to 9d728e7 Compare August 26, 2024 16:30
@kmagusiak kmagusiak force-pushed the master-env-translate-obj-krma branch 6 times, most recently from 339ee68 to 9f1194b Compare September 12, 2024 07:55
@kmagusiak kmagusiak force-pushed the master-env-translate-obj-krma branch from 0ae18da to 3c09a56 Compare September 12, 2024 13:06
@kmagusiak kmagusiak requested a review from rco-odoo September 12, 2024 13:15
@rco-odoo
Copy link
Member

@robodoo r+

@robodoo
Copy link
Collaborator

robodoo commented Sep 12, 2024

I'm sorry, @rco-odoo. I'm afraid I can't do that.

@kmagusiak
Copy link
Contributor Author

@robodoo r+

@robodoo
Copy link
Collaborator

robodoo commented Sep 13, 2024

@kmagusiak linked pull request(s) odoo/odoo#174844 not ready. Linked PRs are not staged until all of them are ready.

@rco-odoo rco-odoo force-pushed the master-env-translate-obj-krma branch from 3c09a56 to 5c968de Compare September 15, 2024 07:34
@kmagusiak kmagusiak force-pushed the master-env-translate-obj-krma branch from 5c968de to c3faae2 Compare September 15, 2024 14:01
@kmagusiak
Copy link
Contributor Author

@robodoo r+

robodoo pushed a commit that referenced this pull request Sep 15, 2024
odoo/odoo#174844

closes #10421

Related: odoo/enterprise#67528
Signed-off-by: Krzysztof Magusiak (krma) <krma@odoo.com>
robodoo pushed a commit to odoo/odoo that referenced this pull request Sep 15, 2024
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'. Note that `odoo.tools._` works only *directly* inside Model
methods.

We add a function `Environment._` for translations. That function uses
its `lang` property for the language without resorting to inspecting the
call stack. You can also pass the module, but we can still resolve it
dynamically.

The following list comprehension will work correctly:
`[self.env._("something %s", x) for x in xs]`

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 through `inspect`. You can then use `Environment._` to get
the translation or `str()` to resolve the lanugage dynamically.
Lazy translations should be used only for globally defined constants
that will be translated using `env._(some_lazy_text_instance)`.

task-4034999

closes #174844

Related: odoo/enterprise#67528
Related: odoo/documentation#10421
Signed-off-by: Krzysztof Magusiak (krma) <krma@odoo.com>
robodoo pushed a commit that referenced this pull request Sep 15, 2024
odoo/odoo#174844

closes #10421

Related: odoo/enterprise#67528
Signed-off-by: Krzysztof Magusiak (krma) <krma@odoo.com>
@robodoo robodoo closed this Sep 15, 2024
@robodoo robodoo added the 17.5 label Sep 15, 2024
@kmagusiak kmagusiak deleted the master-env-translate-obj-krma branch September 15, 2024 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants