4

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 - How to do javascript

I hope I'll have more luck with this one.

What I'm trying to do now:

var _t = null;
odoo.define('mymodule.translate', function (require) {
"use strict";
    var translation = require('web.translation');
    _t = translation._t;
    console.log("_t assigned");
});

A view:

app.categoriesView = Backbone.View.extend({
    tagName: 'div',
    className: 'categoriesView',
    template: _.template($('#categories_list_template').html()),
    initialize: function() {
        this.title = _t('OUR PRODUCTS');
        console.log("Initilized title: "+this.title);
    },
});

Po file:

#. module: mymodule
#: code:addons/mymodule/static/js/views.js:8
#, python-format
msgid "OUR PRODUCTS"
msgstr "PRODUKTI"

I don't get any errors and firebug console only says:

_t assigned
Initilized title: OUR PRODUCTS

So the string doesn't get translated. What am I doing wrong?

Community
  • 1
  • 1
user568021
  • 1,426
  • 5
  • 28
  • 55
  • should'nt you use console.log(_t("assigned")); – Charif DZ May 11 '17 at 08:52
  • no.. this console.log is just there so I know that "odoo.define(" completed successfully. – user568021 May 11 '17 at 09:20
  • did you add this to backend_asset template? – Charif DZ May 11 '17 at 09:26
  • @CherifOdoo no.. it's "frontend" what I'm doing.. – user568021 May 11 '17 at 09:27
  • i'm not a fontend expert but in backend if you want to add new widget or costum a widget you need to add you js to backend asset template in front end and don't know. what are you doing exactly how did you include you js file to the html page? when you inspect element is there script tag to include your js file? – Charif DZ May 11 '17 at 09:37

2 Answers2

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 qweb
    var 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);
    });
});
user568021
  • 1,426
  • 5
  • 28
  • 55
1

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


class IrHttp(models.AbstractModel):
    _inherit = 'ir.http'

    @classmethod
    def _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.

OlafW
  • 700
  • 6
  • 22
  • what is the `@classmethod` here for and why is it outside of any class? Because it surely makes no sense outside of a class – Salek Sep 28 '21 at 08:06
  • @Salek there were some tabs missing - I've corrected the formatting. Thanks for your hint. – OlafW Sep 29 '21 at 08:01
  • Yeah I realized it and it also worked for me - that's why I gave you the up-vote! Btw, do you have any ideas why it actually works? And why isn't the module's name already in the mods list before? – Salek Sep 29 '21 at 09:16
  • 1
    So far I can see this is a not documentated feature/requirement. If you make a global search nearly every frontend-related module implements it. No clue why it is nowhere mentioned. – OlafW Sep 29 '21 at 09:22