Browse Source

[12.0][MIG] web_translate_dialog

pull/1312/head
Timon Tschanz 5 years ago
parent
commit
3fcec53586
  1. 1
      web_translate_dialog/__init__.py
  2. 1
      web_translate_dialog/__manifest__.py
  3. 104
      web_translate_dialog/static/src/js/web_translate_dialog.js
  4. 24
      web_translate_dialog/static/src/xml/base.xml

1
web_translate_dialog/__init__.py

@ -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).

1
web_translate_dialog/__manifest__.py

@ -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).

104
web_translate_dialog/static/src/js/web_translate_dialog.js

@ -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_keys = [field];
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.translatable_fields = [event_data.fieldName];
this.res_id = res_id;
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) {
var values = {};
_.each(self.translatable_fields_keys, function(field) {
values[field] = self.view.fields[field].get_value();
});
self.set_fields_values(lg, values);
deff.resolve();
} else {
self.view.dataset.call('read',[[self.view.datarecord.id],
self.translatable_fields_keys, '_classic_read',
self.view.dataset.get_context({
'lang': lg.code })]).done(
self._rpc({
model: self.view.modelName,
method: 'read',
args: [
[self.res_id],
],
kwargs: {
fields: self.translatable_fields,
context: context,
},
}).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) {
save_mutex.exec(function() {
var context = new Context(session.user_context);
context.lang = code;
self._rpc({
model: self.view.modelName,
method: 'write',
args: [self.res_id, text],
kwargs: {context: context}
});
if (code === self.view_language) { if (code === self.view_language) {
self.view.set_values(text);
_.each(text, function(value, key) {
self.view.$el.find('input[name="'+ key + '"]').val(value);
});
} }
translation_mutex.exec(function() {
return new data.DataSet(self, self.view.dataset.model,
self.view.dataset.get_context()).write(
self.view.datarecord.id, text,
{ context : { 'lang': code }});
}); });
}); });
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({
on_translate: function() {
BasicController.include({
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);
}, },
}); });

24
web_translate_dialog/static/src/xml/base.xml

@ -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-att-data-field="field.name">
<t t-foreach="widget.translatable_fields" t-as="field_name">
<t t-set="field" t-value="widget.view.searchView.fields[field_name]" />
<tr t-att-data-field="field_name">
<td class="oe_form_frame_cell" width="1%" nowrap="nowrap"> <td class="oe_form_frame_cell" width="1%" nowrap="nowrap">
<label class="oe_label"><t t-esc="field.string"/>:</label> <label class="oe_label"><t t-esc="field.string"/>:</label>
</td> </td>
<td t-foreach="widget.languages" t-as="lg" class="oe_form_frame_cell"> <td t-foreach="widget.languages" t-as="lg" class="oe_form_frame_cell">
<input t-if="field.field.type == 'char' || field.field.type == 'url'"
type="text" t-attf-name="#{lg.code}-#{field.name}"
<input t-if="field.type == 'char' || field.type == 'url'"
type="text" t-attf-name="#{lg.code}-#{field_name}"
value="" data-value="" class="oe_translation_field"/> value="" data-value="" class="oe_translation_field"/>
<textarea t-if="field.field.type == 'text'"
t-attf-name="#{lg.code}-#{field.name}" data-value=""
<textarea t-if="field.type == 'text'"
t-attf-name="#{lg.code}-#{field_name}" data-value=""
class="oe_translation_field" ></textarea> class="oe_translation_field" ></textarea>
<div t-if="field.field.type == 'html'" class="oe_form_field_html">
<div t-if="field.type == 'html'" class="oe_form_field_html">
<textarea class="oe_translation_field oe_form_field" <textarea class="oe_translation_field oe_form_field"
t-attf-name="#{lg.code}-#{field.name}" data-value=""/>
t-attf-name="#{lg.code}-#{field_name}" data-value=""/>
</div> </div>
</td> </td>
</tr> </tr>
</t>
</table> </table>
</div> </div>
</t> </t>

Loading…
Cancel
Save