콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
16309 화면

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 


아바타
취소