Skip to Content
Menu
This question has been flagged
2 Replies
15605 Views

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

Avatar
Discard
Author

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

Best Answer

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

Avatar
Discard
Best Answer

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 


Avatar
Discard