简体   繁体   中英

How to translate a term written as a literal string on python in Odoo 10?

I have a method defined that returns In stock or Out of stock terms:

def _compute_availability(self):
    for record in self:
        if record.product_uom_qty <= record.product_id.qty_available:
            record.availability = 'In stock'
        else:
            record.availability = 'Out of stock'

This method is used in a QWeb report.

I would like to translate those terms to another language (Spanish) but when I export the .po file there is no definition for those terms (I guess because there are part of the model and not the XML report itself).

How do you define translation for terms hardcoded in a model method?

Take a look at the underscores:

from odoo.tools.translate import _

# [...]

    def _compute_availability(self):
        for record in self:
            if record.product_uom_qty <= record.product_id.qty_available:
                record.availability = _('In stock')
            else:
                record.availability = _('Out of stock')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM