Internationalize your 🧀
Martin TRIGAUX - Doing Stuff
What is _() anyway
from odoo import models, _
class Tartiflette(models.Model):
_name = 'cheese.potatoes'
def copy(self, default=None):
default['name'] = _("Copy of %s") % self.name
return super(Tartiflette, self).copy(default=default)
var AbstractField = require('web.AbstractField');
var core = require('web.core');
var _t = core._t;
var TartifletteWidget = AbstractField.extend({
_render: function () {
var $cheese = $('<img/>', {
title: _t("Life is great. Cheese makes it better."),
src: "https://upload.wikimedia.org/../128px-Emoji_u1f9c0.svg.png",
});
this.$el.html($cheese);
},
});
registry.add("tartiflette", TartifletteWidget);
What is _() anyway (2)
def _(self, text):
lang = self._context.get('lang')
if lang:
trad = self.env['ir.translation'].search(
[('lang', '=', lang), ('src', '=', text)])
return trad[0].value
return text
(... not really but you get the idea)
Do(n't)
text = _("I love %s" % cheese.name)
Don't
Do
text = _("I love %s") % cheese.name
#. module: tartiflette
#: code:addons/tartiflette/models/cheese.py:42
#, python-format
msgid "I love %s"
msgstr ""
Not "I love Brie", "I love Gruyère", "I love Gouda"....
Do(n't) (2)
text = _("I will eat a ")
if self.veggie:
text += _("veggieflette")
else:
text += _("tartiflette")
Don't
Do
if self.veggie:
text += _("I will eat a veggieflette")
else:
text += _("I will eat a tartiflette")
Do(n't) (3)
TARTIFLETTE_NAME = {
'classic': _("Tartiflette"),
'veggie': _("Veggieflette"),
}
class Tartiflette(models.Model):
def _compute_display_name(self):
self.display_name = TARTIFLETTE_NAME[self.version]
Don't
Do
class Tartiflette(models.Model):
def _get_tartiflette_names(self):
return {
'classic': _("Tartiflette"),
'veggie': _("Veggieflette"),
}
def _compute_display_name(self):
self.display_name = self._get_tartiflette_name()[self.version]
Thanks!
i18n your code
By Martin T
i18n your code
- 754