From 22fe10148ad097929872226f697c6888a6c55e2f Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Mon, 24 Dec 2018 15:17:38 +0530 Subject: [PATCH] [Fix] Translation not working for the html data (#6647) * [Fix] Translation not working for the html data * Test cases * Fixed codacy * Fixed is html method * Fixed test cases --- frappe/__init__.py | 8 ++++- .../doctype/translation/test_translation.py | 31 +++++++++++++++++++ .../core/doctype/translation/translation.py | 8 +++++ .../js/frappe/views/translation_manager.js | 4 +-- frappe/translate.py | 14 ++++++++- frappe/utils/data.py | 7 +---- 6 files changed, 62 insertions(+), 10 deletions(-) diff --git a/frappe/__init__.py b/frappe/__init__.py index 931bcccb6e..399ea2f21d 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -45,6 +45,7 @@ class _dict(dict): def _(msg, lang=None): """Returns translated string in current lang, if exists.""" from frappe.translate import get_full_dict + from frappe.utils import strip_html_tags, is_html if not hasattr(local, 'lang'): local.lang = lang or 'en' @@ -52,11 +53,16 @@ def _(msg, lang=None): if not lang: lang = local.lang + non_translated_msg = msg + + if is_html(msg): + msg = strip_html_tags(msg) + # msg should always be unicode msg = as_unicode(msg).strip() # return lang_full_dict according to lang passed parameter - return get_full_dict(lang).get(msg) or msg + return get_full_dict(lang).get(msg) or non_translated_msg def as_unicode(text, encoding='utf-8'): '''Convert to unicode if required''' diff --git a/frappe/core/doctype/translation/test_translation.py b/frappe/core/doctype/translation/test_translation.py index e54a8868fd..cfca3b53a4 100644 --- a/frappe/core/doctype/translation/test_translation.py +++ b/frappe/core/doctype/translation/test_translation.py @@ -57,6 +57,37 @@ class TestTranslation(unittest.TestCase): frappe.local.lang_full_dict=None self.assertTrue(_(data[1][0]), data[1][1]) + def test_html_content_data_translation(self): + source = """ + MacBook Air lasts up to an incredible 12 hours between charges. So from your morning coffee to + your evening commute, you can work unplugged. When it’s time to kick back and relax, + you can get up to 12 hours of iTunes movie playback. And with up to 30 days of standby time, + you can go away for weeks and pick up where you left off.Whatever the task, + fifth-generation Intel Core i5 and i7 processors with Intel HD Graphics 6000 are up to it.
+ """ + + target = """ + MacBook Air dura hasta 12 horas increíbles entre cargas. Por lo tanto, + desde el café de la mañana hasta el viaje nocturno, puede trabajar desconectado. + Cuando es hora de descansar y relajarse, puede obtener hasta 12 horas de reproducción de películas de iTunes. + Y con hasta 30 días de tiempo de espera, puede irse por semanas y continuar donde lo dejó. Sea cual sea la tarea, + los procesadores Intel Core i5 e i7 de quinta generación con Intel HD Graphics 6000 son capaces de hacerlo. + """ + + create_translation('es', [source, target]) + + source = """ + MacBook Air lasts up to an incredible 12 hours between charges. So from your morning coffee to + your evening commute, you can work unplugged. When it’s time to kick back and relax, + you can get up to 12 hours of iTunes movie playback. And with up to 30 days of standby time, + you can go away for weeks and pick up where you left off.Whatever the task, + fifth-generation Intel Core i5 and i7 processors with Intel HD Graphics 6000 are up to it.
+ """ + + self.assertTrue(_(source), target) + def get_translation_data(): html_source_data = """ Test Data""" diff --git a/frappe/core/doctype/translation/translation.py b/frappe/core/doctype/translation/translation.py index f69ab7c6de..88a8302e18 100644 --- a/frappe/core/doctype/translation/translation.py +++ b/frappe/core/doctype/translation/translation.py @@ -6,8 +6,16 @@ from __future__ import unicode_literals import frappe from frappe.model.document import Document from frappe.translate import clear_cache +from frappe.utils import strip_html_tags, is_html class Translation(Document): + def validate(self): + if is_html(self.source_name): + self.remove_html_from_source() + + def remove_html_from_source(self): + self.source_name = strip_html_tags(self.source_name).strip() + def on_update(self): clear_cache() diff --git a/frappe/public/js/frappe/views/translation_manager.js b/frappe/public/js/frappe/views/translation_manager.js index 76d867cca2..66fd267b95 100644 --- a/frappe/public/js/frappe/views/translation_manager.js +++ b/frappe/public/js/frappe/views/translation_manager.js @@ -60,7 +60,7 @@ frappe.views.TranslationManager = class TranslationManager { { label: 'Translation', fieldname: 'translation', - fieldtype: 'Data', + fieldtype: 'Text', in_list_view: 1, columns: 7 } @@ -78,7 +78,7 @@ frappe.views.TranslationManager = class TranslationManager { return frappe.db.get_list('Translation', { fields: ['name', 'language', 'target_name as translation'], filters: { - source_name: this.source_name + source_name: strip_html(this.source_name) } }); } diff --git a/frappe/translate.py b/frappe/translate.py index 9ca08b042e..12b6c722fc 100644 --- a/frappe/translate.py +++ b/frappe/translate.py @@ -14,7 +14,7 @@ from six import iteritems, text_type, string_types, PY2 import frappe, os, re, io, codecs, json from frappe.model.utils import render_include, InvalidIncludePath -from frappe.utils import strip +from frappe.utils import strip, strip_html_tags, is_html from jinja2 import TemplateError import itertools, operator @@ -741,3 +741,15 @@ def update_translations_for_source(source=None, translation_dict=None): doc.save() return translation_records + +@frappe.whitelist() +def get_translations(source_name): + if is_html(source_name): + source_name = strip_html_tags(source_name) + + return frappe.db.get_list('Translation', + fields = ['name', 'language', 'target_name as translation'], + filters = { + 'source_name': source_name + } + ) \ No newline at end of file diff --git a/frappe/utils/data.py b/frappe/utils/data.py index a09f9a1354..83e4082426 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -568,12 +568,7 @@ def in_words(integer, in_million=True): return ret.replace('-', ' ') def is_html(text): - out = False - for key in ["
", "]+>', text) def is_image(filepath): from mimetypes import guess_type