Odoo - 如何翻译 javascript 文件中的字符串

2024-01-31

我因对 Javascript 缺乏了解而苦苦挣扎。 Odoo 文档非常糟糕,我还有关于这个主题的另一个问题,但这里没有答案:Odoo10 - 如何执行 JavaScript https://stackoverflow.com/questions/42227474/odoo10-how-to-do-javascript

我希望我能在这件事上有更多的运气。

我现在正在尝试做的事情:

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"

我没有收到任何错误,萤火虫控制台只显示:

_t assigned
Initilized title: OUR PRODUCTS

所以该字符串不会被翻译。我究竟做错了什么?


我的一般 javascript 问题仍然存在,但至少我已经可以进行翻译了。

这个人对我帮助最大:https://www.odoo.com/forum/help-1/question/8-0-how-does-javascript-translation-tool-works-openerp-t-96934 https://www.odoo.com/forum/help-1/question/8-0-how-does-javascript-translation-tool-works-openerp-t-96934

工作代码:

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);
    });
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Odoo - 如何翻译 javascript 文件中的字符串 的相关文章

随机推荐