Skip to content Skip to sidebar Skip to footer

Odoo - How To Translate Strings In Javascript File

I'm struggling with the lack of understanding on doing Javascript. The Odoo documentation is painfully poor and I have another question on this topic without an answer here: Odoo10

Solution 1:

My general javascript problem remains, but at least I've got translations working.

This guy helped me the most: https://www.odoo.com/forum/help-1/question/8-0-how-does-javascript-translation-tool-works-openerp-t-96934

Working code:

var _t = null;

odoo.define('mymodule', function (require) {
    "use strict";
    lang = $('input[name="website_lang"]').val(); //Added this input via qwebvar core = require('web.core');
    var session = require('web.session'); _s = session;
    var utils = require('web.utils');
    translation = require('web.translation');
    var translationDataBase = new translation.TranslationDataBase();
    var dfd = $.Deferred();
    translationDataBase.load_translations(session, ['mymodule'], lang).done(function() {
        _t = translationDataBase.build_translation_function();
        dfd.resolve(_t);
    });
});

Solution 2:

Just in case someone also struggles with this issue and is pretty sure that she/he did all correct, here is a hint that helped me to solve my problem:

As Odoo lazy loads the translations for JavaScript via URL /website/translations/ you can easily monitor whether your translations are available in the response JSON data. In my case they weren't so I followed the trail of the url.

Finally all I had to do is to add an child of ir.http to my module which adds the module-name to the translation-response:

# -*- coding: utf-8 -*-from odoo import models


classIrHttp(models.AbstractModel):
    _inherit = 'ir.http'    @classmethoddef_get_translation_frontend_modules_name(cls):
        mods = super(IrHttp, cls)._get_translation_frontend_modules_name()
        return mods + ['MyModuleName']

This simple step added my module to the list of modules responded by the web-request and delivered all my translated strings.

Post a Comment for "Odoo - How To Translate Strings In Javascript File"