3
0
Fork 0

[12.0][MIG] web_translate_dialog

13.0
Timon Tschanz 2019-06-21 18:59:38 +02:00 committed by fshah
parent 341aa4b137
commit c7bf51250b
4 changed files with 79 additions and 79 deletions

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2012 Guewen Baconnier (Camptocamp SA) # Copyright 2012 Guewen Baconnier (Camptocamp SA)
# Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com> # Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2012 Guewen Baconnier (Camptocamp SA) # Copyright 2012 Guewen Baconnier (Camptocamp SA)
# Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com> # Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

View File

@ -6,49 +6,37 @@ odoo.define('web_translate_dialog.translate_dialog', function(require){
"use strict"; "use strict";
var core = require('web.core'); var core = require('web.core');
var common = require('web.form_common'); var BasicController = require('web.BasicController');
var data = require('web.data'); var data = require('web.data');
var Context = require('web.Context');
var concurrency = require('web.concurrency');
var Dialog = require('web.Dialog'); var Dialog = require('web.Dialog');
var FormView = require('web.FormView'); var FormView = require('web.FormView');
var View = require('web.View'); var View = require('web.AbstractView');
var session = require('web.session');
var _t = core._t; var _t = core._t;
var QWeb = core.qweb; var QWeb = core.qweb;
var Mutex = concurrency.Mutex;
var translateDialog = Dialog.extend({ var translateDialog = Dialog.extend({
template: "TranslateDialog", template: "TranslateDialog",
init: function(parent, field, content) { init: function(parent, event_data, res_id, content) {
this._super(parent, this._super(parent,
{title: _t("Translations"), {title: _t("Translations"),
width: '90%', width: '90%',
height: '80%'}, height: '80%'},
content); content);
this.view_language = this.session.user_context.lang; this.view_language = session.user_context.lang;
this.view = parent; this.view = parent;
this.view_type = parent.fields_view.type || ''; this.view_type = parent.viewType || '';
this.$view_form = null; this.$view_form = null;
this.$sidebar_form = null; this.$sidebar_form = null;
if (field) { this.translatable_fields = [event_data.fieldName];
this.translatable_fields_keys = [field]; this.res_id = res_id;
this.translatable_fields = _.filter(
this.view.translatable_fields || [],
function(i) {
return i.name === field;
}
);
} else {
this.translatable_fields_keys = _.map(
this.view.translatable_fields || [],
function(i) {
return i.name;
}
);
this.translatable_fields = this.view.translatable_fields.slice(0);
}
this.languages = null; this.languages = null;
this.languages_loaded = $.Deferred(); this.languages_loaded = $.Deferred();
(new data.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(), (new data.DataSetSearch(this, 'res.lang', parent.searchView.dataset.get_context(),
[['translatable', '=', '1']])).read_slice(['code', 'name'], [['translatable', '=', '1']])).read_slice(['code', 'name'],
{ sort: 'id' }).then(this.on_languages_loaded); { sort: 'id' }).then(this.on_languages_loaded);
}, },
@ -78,7 +66,7 @@ var translateDialog = Dialog.extend({
}, },
initialize_html_fields: function(lang) { initialize_html_fields: function(lang) {
var self = this; var self = this;
_.each(this.translatable_fields_keys, function(f) { _.each(this.translatable_fields, function(f) {
// Initialize summernote if HTML field // Initialize summernote if HTML field
self.$el.find('.oe_form_field_html .oe_translation_field[name="' + lang.code + '-' + f + '"]').each(function() { self.$el.find('.oe_form_field_html .oe_translation_field[name="' + lang.code + '-' + f + '"]').each(function() {
var $parent = $(this).summernote({ var $parent = $(this).summernote({
@ -113,7 +101,7 @@ var translateDialog = Dialog.extend({
}, },
set_fields_values: function(lang, values) { set_fields_values: function(lang, values) {
var self = this; var self = this;
_.each(this.translatable_fields_keys, function(f) { _.each(this.translatable_fields, function(f) {
self.$el.find('.oe_translation_field[name="' + lang.code + self.$el.find('.oe_translation_field[name="' + lang.code +
'-' + f + '"]').val(values[f] || '').attr( '-' + f + '"]').val(values[f] || '').attr(
'data-value', values[f] || ''); 'data-value', values[f] || '');
@ -130,32 +118,35 @@ var translateDialog = Dialog.extend({
this.$el.find('.oe_translation_field').val('').removeClass('touched'); this.$el.find('.oe_translation_field').val('').removeClass('touched');
_.each(self.languages, function(lg) { _.each(self.languages, function(lg) {
var context = session.user_context;
context.lang = lg.code;
var deff = $.Deferred(); var deff = $.Deferred();
deferred.push(deff); deferred.push(deff);
if (lg.code === self.view_language) { self._rpc({
var values = {}; model: self.view.modelName,
_.each(self.translatable_fields_keys, function(field) { method: 'read',
values[field] = self.view.fields[field].get_value(); args: [
}); [self.res_id],
self.set_fields_values(lg, values); ],
deff.resolve(); kwargs: {
} else { fields: self.translatable_fields,
self.view.dataset.call('read',[[self.view.datarecord.id], context: context,
self.translatable_fields_keys, '_classic_read', },
self.view.dataset.get_context({ }).done(
'lang': lg.code })]).done( function (rows) {
function (rows) {
if (rows[0]){
self.set_fields_values(lg, rows[0]); self.set_fields_values(lg, rows[0]);
deff.resolve(); deff.resolve();
}); }
} });
}); });
return deferred; return deferred;
}, },
on_button_save: function() { on_button_save: function() {
var translations = {}, var translations = {},
self = this, self = this,
translation_mutex = new $.Mutex(); save_mutex = new Mutex();
self.$el.find('.oe_translation_field.touched').each(function() { self.$el.find('.oe_translation_field.touched').each(function() {
var field = $(this).attr('name').split('-'); var field = $(this).attr('name').split('-');
if (!translations[field[0]]) { if (!translations[field[0]]) {
@ -164,16 +155,23 @@ var translateDialog = Dialog.extend({
translations[field[0]][field[1]] = $(this).val(); translations[field[0]][field[1]] = $(this).val();
}); });
_.each(translations, function(text, code) { _.each(translations, function(text, code) {
if (code === self.view_language) { save_mutex.exec(function() {
self.view.set_values(text); var context = new Context(session.user_context);
} context.lang = code;
translation_mutex.exec(function() { self._rpc({
return new data.DataSet(self, self.view.dataset.model, model: self.view.modelName,
self.view.dataset.get_context()).write( method: 'write',
self.view.datarecord.id, text, args: [self.res_id, text],
{ context : { 'lang': code }}); kwargs: {context: context}
});
if (code === self.view_language) {
_.each(text, function(value, key) {
self.view.$el.find('input[name="'+ key + '"]').val(value);
});
}
}); });
}); });
session.user_context.lang = self.view_language;
this.close(); this.close();
}, },
on_button_close: function() { on_button_close: function() {
@ -204,15 +202,17 @@ FormView.include({
}); });
View.include({ View.include({
open_translate_dialog: function(field) {
new translateDialog(this, field).open();
}
}); });
common.AbstractField.include({ BasicController.include({
on_translate: function() {
open_translate_dialog: function(field, res_id) {
new translateDialog(this, field, res_id).open();
},
_onTranslate: function(event) {
// the image next to the fields opens the translate dialog // the image next to the fields opens the translate dialog
this.view.open_translate_dialog(this.name); this.open_translate_dialog(event.data, event.target.res_id);
}, },
}); });

View File

@ -6,35 +6,37 @@
<t t-name="TranslateDialog"> <t t-name="TranslateDialog">
<div class="modal-body"> <div class="modal-body">
<table t-if="widget.view.translatable_fields" <table t-if="widget.translatable_fields"
class="oe_frame oe_forms oe_translation_form" class="oe_frame oe_forms oe_translation_form"
border="0" cellpadding="0" cellspacing="0" width="100%"> border="0" cellpadding="0" cellspacing="0" width="100%">
<tr> <tr>
<td class="oe_form_separator" width="1%" nowrap="nowrap"> <th class="oe_form_separator" width="1%" nowrap="nowrap">
<div class="separator horizontal">Field</div> <div class="separator horizontal">Field</div>
</td> </th>
<th t-foreach="widget.languages" align="left"> <th t-foreach="widget.languages" align="left">
<div class="separator horizontal"><t t-esc="name"/></div> <div class="separator horizontal"><t t-esc="name"/></div>
</th> </th>
</tr> </tr>
<tr t-foreach="widget.translatable_fields" t-as="field" <t t-foreach="widget.translatable_fields" t-as="field_name">
t-att-data-field="field.name"> <t t-set="field" t-value="widget.view.searchView.fields[field_name]" />
<td class="oe_form_frame_cell" width="1%" nowrap="nowrap"> <tr t-att-data-field="field_name">
<label class="oe_label"><t t-esc="field.string"/>:</label> <td class="oe_form_frame_cell" width="1%" nowrap="nowrap">
</td> <label class="oe_label"><t t-esc="field.string"/>:</label>
<td t-foreach="widget.languages" t-as="lg" class="oe_form_frame_cell"> </td>
<input t-if="field.field.type == 'char' || field.field.type == 'url'" <td t-foreach="widget.languages" t-as="lg" class="oe_form_frame_cell">
type="text" t-attf-name="#{lg.code}-#{field.name}" <input t-if="field.type == 'char' || field.type == 'url'"
value="" data-value="" class="oe_translation_field"/> type="text" t-attf-name="#{lg.code}-#{field_name}"
<textarea t-if="field.field.type == 'text'" value="" data-value="" class="oe_translation_field"/>
t-attf-name="#{lg.code}-#{field.name}" data-value="" <textarea t-if="field.type == 'text'"
class="oe_translation_field" ></textarea> t-attf-name="#{lg.code}-#{field_name}" data-value=""
<div t-if="field.field.type == 'html'" class="oe_form_field_html"> class="oe_translation_field" ></textarea>
<textarea class="oe_translation_field oe_form_field" <div t-if="field.type == 'html'" class="oe_form_field_html">
t-attf-name="#{lg.code}-#{field.name}" data-value=""/> <textarea class="oe_translation_field oe_form_field"
</div> t-attf-name="#{lg.code}-#{field_name}" data-value=""/>
</td> </div>
</tr> </td>
</tr>
</t>
</table> </table>
</div> </div>
</t> </t>