Browse Source

[fix] translate.py - escape \n, don't ingore empty lines

version-14
Anand Doshi 11 years ago
parent
commit
a04531248f
2 changed files with 26 additions and 9 deletions
  1. +5
    -2
      frappe/__init__.py
  2. +21
    -7
      frappe/translate.py

+ 5
- 2
frappe/__init__.py View File

@@ -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 []



+ 21
- 7
frappe/translate.py View File

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



Loading…
Cancel
Save