4 var website = openerp.website;
5 website.add_template_file('/website/static/src/xml/website.translator.xml');
6 var nodialog = 'website_translator_nodialog';
8 website.EditorBar.include({
9 do_not_translate : ['-','*','!'],
10 events: _.extend({}, website.EditorBar.prototype.events, {
11 'click a[data-action=edit_master]': 'edit_master',
15 this.initial_content = {};
16 return this._super.apply(this, arguments).then(function () {
17 self.$("button[data-action=edit]").removeClass("hidden");
18 self.$('button[data-action=edit]')
20 if (website.is_editable_button) {
21 self.$('button[data-action=edit]')
22 .after(openerp.qweb.render('website.TranslatorAdditionalButtons'));
24 self.$('.js_hide_on_translate').hide();
29 var mysuper = this._super;
30 if (!localStorage[nodialog]) {
31 var dialog = new website.TranslatorDialog();
32 dialog.appendTo($(document.body));
33 dialog.on('activate', this, function () {
34 localStorage[nodialog] = dialog.$('input[name=do_not_show]').prop('checked') || '';
35 dialog.$el.modal('hide');
36 self.translate().then(function () {
38 if(self.gengo_translate){
39 self.translation_gengo_display()
44 this.translate().then(function () {
46 if(self.gengo_translate){
47 self.translation_gengo_display()
52 edit_master: function (ev) {
54 var link = $('.js_language_selector a[data-default-lang]')[0];
56 link.search += (link.search ? '&' : '?') + 'enable_editor=1';
57 window.location = link.attributes.href.value;
60 translate: function () {
62 this.translations = null;
63 return openerp.jsonRpc('/website/get_view_translations', 'call', {
64 'xml_id': $(document.documentElement).data('view-xmlid'),
65 'lang': website.get_context().lang,
66 }).then(function (translations) {
67 self.translations = translations;
68 self.processTranslatableNodes();
69 // Disable non translatable t-fields
70 $('[data-oe-type][data-oe-translate="0"]').removeAttr('data-oe-type');
73 processTranslatableNodes: function () {
75 var source_attr = 'data-oe-source-id';
76 var $editables = $('[data-oe-model="ir.ui.view"]')
78 .not('.oe_snippets,.oe_snippet, .oe_snippet *, .navbar-toggle')
79 .not('[data-oe-type]');
81 $editables.each(function () {
83 var source_id = $node.parents('[' + source_attr + ']:first').attr(source_attr)|0;
84 var view_id = $node.attr('data-oe-source-id') || source_id || $node.attr('data-oe-id');
85 self.transNode(this, view_id|0);
87 $('.oe_translatable_text').on('paste', function () {
89 setTimeout(function () {
90 self.sanitizeNode(node);
93 $(document).on('blur keyup paste', '.oe_translatable_text[contenteditable]', function(ev) {
95 setTimeout(function () {
96 // Doing stuff next tick because paste and keyup events are
97 // fired before the content is changed
98 if (ev.type == 'paste') {
99 self.sanitizeNode($node[0]);
101 if (self.getInitialContent($node[0]) !== $node.text()) {
102 $node.addClass('oe_dirty').removeClass('oe_translatable_todo oe_translatable_inprogress');
107 getInitialContent: function (node) {
108 return this.initial_content[node.attributes['data-oe-nodeid'].value];
110 sanitizeNode: function (node) {
111 node.text(node.text());
113 isTextNode: function (node) {
114 return node.nodeType === 3 || node.nodeType === 4;
116 isTranslatable: function (text) {
117 return text && _.str.trim(text) !== '';
119 markTranslatableNode: function (node, view_id) {
120 // TODO: link nodes with same content
121 node.className += ' oe_translatable_text';
122 node.setAttribute('data-oe-translation-view-id', view_id);
123 var content = node.childNodes[0].data.trim();
124 var trans = this.translations.filter(function (t) {
125 return t.res_id === view_id && t.value === content;
128 node.setAttribute('data-oe-translation-id', trans[0].id);
129 if(trans[0].gengo_translation && (trans[0].state == 'inprogress' || trans[0].state == 'to_translate')){
130 node.className += ' oe_translatable_inprogress';
133 node.className += this.do_not_translate.indexOf(node.textContent.trim()) ? ' oe_translatable_todo' : '';
135 node.contentEditable = true;
136 var nid = _.uniqueId();
137 $(node).attr('data-oe-nodeid', nid);
138 this.initial_content[nid] = content;
142 var mysuper = this._super;
144 // this._super.apply(this, arguments);
145 $('.oe_translatable_text.oe_dirty').each(function () {
147 var data = $node.data();
148 if (!trans[data.oeTranslationViewId]) {
149 trans[data.oeTranslationViewId] = [];
151 trans[data.oeTranslationViewId].push({
152 initial_content: self.getInitialContent(this),
153 new_content: $node.text(),
154 translation_id: data.oeTranslationId || null
157 openerp.jsonRpc('/website/set_translations', 'call', {
159 'lang': website.get_context()['lang'],
160 }).then(function () {
162 }).fail(function () {
163 // TODO: bootstrap alert with error message
164 alert("Could not save translation");
167 transNode: function (node, view_id) {
168 // Mostly handling text and cdata nodes here
169 // so avoid jquery usage in this function
170 if (node.attributes['data-oe-type']) {
171 if (node.attributes['data-oe-translate'].value == '1') {
172 node.className += ' oe_translatable_field';
174 } else if (node.childNodes.length === 1
175 && this.isTextNode(node.childNodes[0])
176 && !node.getAttribute('data-oe-model')) {
177 this.markTranslatableNode(node, view_id);
179 for (var i = 0, l = node.childNodes.length; i < l; i ++) {
180 var n = node.childNodes[i];
181 if (this.isTextNode(n)) {
182 if (this.isTranslatable(n.data)) {
183 var container = document.createElement('span');
184 node.insertBefore(container, n);
185 container.appendChild(n);
186 this.markTranslatableNode(container, view_id);
189 this.transNode(n, view_id);
196 website.RTE.include({
198 this._super.apply(this, arguments);
201 fetch_editables: function (root) {
202 $(root).click(function (ev) {
205 return $('[data-oe-translate="1"]');
209 website.TranslatorDialog = openerp.Widget.extend({
210 events: _.extend({}, website.EditorBar.prototype.events, {
211 'hidden.bs.modal': 'destroy',
212 'click button[data-action=activate]': function (ev) {
213 this.trigger('activate');
216 template: 'website.TranslatorDialog',