From a04531248fb05b9ef3c2396a02001570e7fd0db9 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 31 Jul 2014 23:13:25 +0530 Subject: [PATCH] [fix] translate.py - escape \n, don't ingore empty lines --- frappe/__init__.py | 7 +++++-- frappe/translate.py | 28 +++++++++++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/frappe/__init__.py b/frappe/__init__.py index 80f486ea87..0299081e6a 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -485,10 +485,13 @@ def setup_module_map(): _cache.set_value("app_modules", local.app_modules) _cache.set_value("module_app", local.module_app) -def get_file_items(path, raise_not_found=False): +def get_file_items(path, raise_not_found=False, ignore_empty_lines=True): content = read_file(path, raise_not_found=raise_not_found) if content: - return [p.strip() for p in content.splitlines() if p.strip() and not p.startswith("#")] + # \ufeff is no-width-break, \u200b is no-width-space + content = content.replace("\ufeff", "").replace("\u200b", "").strip() + + return [p.strip() for p in content.splitlines() if (not ignore_empty_lines) or (p.strip() and not p.startswith("#"))] else: return [] diff --git a/frappe/translate.py b/frappe/translate.py index cc4f91c86a..62c26a35f1 100644 --- a/frappe/translate.py +++ b/frappe/translate.py @@ -301,12 +301,17 @@ def get_untranslated(lang, untranslated_file, get_all=False): for app in apps: messages.extend(get_messages_for_app(app)) + def escape_newlines(s): + return (s.replace("\\\n", "|||||") + .replace("\\n", "||||") + .replace("\n", "|||")) + if get_all: print str(len(messages)) + " messages" with open(untranslated_file, "w") as f: for m in messages: - # replace \n with \\n so that internal linebreaks don't get split - f.write((m.replace("\n", "\\n") + os.linesep).encode("utf-8")) + # replace \n with ||| so that internal linebreaks don't get split + f.write((escape_newlines(m) + os.linesep).encode("utf-8")) else: full_dict = get_full_dict(lang) @@ -318,8 +323,8 @@ def get_untranslated(lang, untranslated_file, get_all=False): print str(len(untranslated)) + " missing translations of " + str(len(messages)) with open(untranslated_file, "w") as f: for m in untranslated: - # replace \n with \\n so that internal linebreaks don't get split - f.write((m.replace("\n", "\\n") + os.linesep).encode("utf-8")) + # replace \n with ||| so that internal linebreaks don't get split + f.write((escape_newlines(m) + os.linesep).encode("utf-8")) else: print "all translated!" @@ -327,11 +332,20 @@ def update_translations(lang, untranslated_file, translated_file): clear_cache() full_dict = get_full_dict(lang) + def restore_newlines(s): + return (s.replace("|||||", "\\\n") + .replace("| | | | |", "\\\n") + .replace("||||", "\\n") + .replace("| | | |", "\\n") + .replace("|||", "\n") + .replace("| | |", "\n")) + translation_dict = {} - for key, value in zip(frappe.get_file_items(untranslated_file), - frappe.get_file_items(translated_file)): + for key, value in zip(frappe.get_file_items(untranslated_file, ignore_empty_lines=False), + frappe.get_file_items(translated_file, ignore_empty_lines=False)): + # undo hack in get_untranslated - translation_dict[key.replace("\\n", "\n")] = value.replace("\\n", "\n") + translation_dict[restore_newlines(key)] = restore_newlines(value) full_dict.update(translation_dict)