Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

[8.0] How does Javascript translation tool works ? `openerp._t`

By
PY
on 2/2/16, 9:55 AM 763 views

This question in about the frontend side (Website)

var foo = openerp._t('bar')

gives:

#. module: my_module
#. openerp-web
#: code:addons/my_module/static/src/js/main.js:351
#, python-format
msgid "bar"
msgstr ""

in the generated .pot file.

However, the translation does not work.

If I put 'bar' in the, let's say, french translation file (fr.po), restart and update my module, the translation appears in the database. I can see it at Settings/Translation/Translated terms, but not on my custom view, where I insert the javascript string.


But I know that the function works sometimes. Here's a sample from a javascript console:

openerp._t('foo')
> "foo"
openerp._t('Title')
> "Civilité"

Update:

This works in console, but not in code:

var translationDataBase = new openerp.TranslationDataBase(),
    session = new openerp.Session(),
    lang = openerp.website.get_context().lang;
    
translationDataBase.load_translations(session, ['my_module'], lang);
var _t = translationDataBase.build_translation_function();

Update 2:

It seems that it has to do with Deferred objects :/ Because I set a timeout before calling _t('foo'), and that worked!

0
PY
On 2/2/16, 11:43 AM

Here's what's working now:

//  define the translation utility
openerp._translate_modules = function(modules) {
    var translationDataBase = new openerp.TranslationDataBase(),
        session = new openerp.Session(),
        lang = openerp.website.get_context().lang;

    var dfd = $.Deferred();

    translationDataBase.load_translations(session, modules, lang).done(function() {
        var _t = translationDataBase.build_translation_function(); console.log(_t);
        dfd.resolve(_t);
    });

    return dfd.promise();
}

//  use the translation utility
$.when( openerp._translate_modules(['my_module']) ).then(
    function(_t) {
        var my_string = _t('Hello world!');

        doSomethingWithIt(my_string);
    });

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/2/16, 9:55 AM
Seen: 763 times
Last updated: 2/2/16, 11:43 AM