jordimas's picture
Update scripts
217f640
raw
history blame
1.77 kB
import polib
import re
def _remove_accelerators(result):
CHARS = (
'_', '&', '~' # Accelerators.
)
for c in CHARS:
result = result.replace(c, '')
return result.strip()
def _remove_tags(text):
clean = re.sub("<[^>]*>", "", text)
return clean
def _is_non_localized_string(src, trg):
words_src_len = len(src.split())
words_trg_len = len(trg.split())
if words_src_len > 2 and words_trg_len > 2:
return src == trg
return False
def _is_invalid(src, trg):
if len(src) < 2 or len(trg) < 2:
return True
if '\n' in src or '\n' in trg:
return True
if '@@image' in src or '@@image' in trg:
return True
if _is_non_localized_string(src, trg):
return True
return False
def generate_tsv(po_filename, output_filename):
SEPARATOR = '\t'
input_po = polib.pofile(po_filename)
entries = 0
words = 0
with open(po_filename, "r") as source,\
open(output_filename, "w") as output:
for entry in input_po:
src = _remove_accelerators(entry.msgid)
trg = _remove_accelerators(entry.msgstr)
src = _remove_tags(src)
trg = _remove_tags(trg)
if _is_invalid(src, trg):
continue
if SEPARATOR in src or SEPARATOR in trg:
continue
src = src.strip()
trg = trg.strip()
output.write(f"{src}{SEPARATOR}{trg}\n")
words += len(src.split())
entries += 1
print(f"Generated {entries} entries with {words}")
def main():
print("Generate TSV from a PO file")
generate_tsv("tots-tm.po", "MemoriesProjectesLliures.tsv")
if __name__ == "__main__":
main()