コンテンツへスキップ
メニュー
この質問にフラグが付けられました
2 返信
16301 ビュー

Hi All,

For the most part, when creating models or inheriting them, the import statement reads as follows:

from odoo import api, fields, model

But occasionally, I see the following in the code:

from odoo import api, fields, models, _

and also:

from odoo.tools.translate import _

Could someone explain what is the significance of the underscore _ in the odoo import statements above? Is this used as a catchall of some kind?

Just wondering...trying to understand it, that's all!

kind rgds,

ME

アバター
破棄
著作者

Thanks for the detailed explanation Yenthe and Waleed. Certainly shared some light on this!

最善の回答

Hi musicearth,

This is a built in tool from Odoo and it makes it possible to translate text in multiple language.
Imagine that you want to show a warning to an user when something is wrong. Then you would do something like this:

raise ValidationError('At least one language must be active.')

This will work fine but what if you have one user in your system that is English and one that is Dutch? Well, both would get it in English in this case. When you use the "_" import from Odoo you'll be able to make the text translatable. Your code would look like this then:

raise ValidationError(_('At least one language must be active.'))

By using _() in the code it will be parsed by Odoo. If you then export your translations this string will be in the text values too and you'll be able to translate it in any language that you'd like.


So in short: the import allows you to manage text in multiple languages easily.

Regards,
Yenthe

アバター
破棄
最善の回答

Hello,

from odoo.tools.translate import _

This imports the function/class/module _ from odoo.tools.translate  into the current namespace.
You can check the below link:

https://stackoverflow.com/questions/2908444/what-does-from-module-import-do-in-python


 

Regards,

Waleed 


アバター
破棄