[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
This commit is contained in:
parent
4ee79078f2
commit
22fe10148a
6 changed files with 62 additions and 10 deletions
|
|
@ -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'''
|
||||
|
|
|
|||
|
|
@ -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 = """
|
||||
<span style="color: rgb(51, 51, 51); font-family: "Amazon Ember", Arial, sans-serif; font-size:
|
||||
small;">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.</span><br>
|
||||
"""
|
||||
|
||||
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 = """
|
||||
<span style="font-family: "Amazon Ember", Arial, sans-serif; font-size:
|
||||
small; color: rgb(51, 51, 51);">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.</span><br>
|
||||
"""
|
||||
|
||||
self.assertTrue(_(source), target)
|
||||
|
||||
def get_translation_data():
|
||||
html_source_data = """<font color="#848484" face="arial, tahoma, verdana, sans-serif">
|
||||
<span style="font-size: 11px; line-height: 16.9px;">Test Data</span></font>"""
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
)
|
||||
|
|
@ -568,12 +568,7 @@ def in_words(integer, in_million=True):
|
|||
return ret.replace('-', ' ')
|
||||
|
||||
def is_html(text):
|
||||
out = False
|
||||
for key in ["<br>", "<p", "<img", "<div"]:
|
||||
if key in text:
|
||||
out = True
|
||||
break
|
||||
return out
|
||||
return re.search('<[^>]+>', text)
|
||||
|
||||
def is_image(filepath):
|
||||
from mimetypes import guess_type
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue