|
| 1 | +# rst2po.py |
| 2 | +# Script to migrate the Python official tutorial that we have already translated in PyAr |
| 3 | +# (https://github.com/PyAr/tutorial) from reStructuredText to the new official translation format (.po) |
| 4 | +# |
| 5 | +# It parses the .rst and compare sentence/paragraph by sentence/paragraph and if the match is exact, |
| 6 | +# and there is no translation for that sentence/paragraph it updates the .po file with the translated text |
| 7 | +# from the .rst file. |
| 8 | + |
| 9 | +import re |
| 10 | +import glob |
| 11 | +import os |
| 12 | +import polib # fades |
| 13 | + |
| 14 | + |
| 15 | +PO_DIR = os.path.abspath( |
| 16 | + os.path.join( |
| 17 | + os.path.dirname(__file__), |
| 18 | + '..', |
| 19 | + )) |
| 20 | + |
| 21 | +RST_TRADUCIDOS_DIR = os.path.abspath( |
| 22 | + os.path.join( |
| 23 | + os.path.dirname(__file__), |
| 24 | + 'tutorialpyar', |
| 25 | + 'traducidos', |
| 26 | + )) |
| 27 | + |
| 28 | +RST_ORIGINAL_DIR = os.path.abspath( |
| 29 | + os.path.join( |
| 30 | + os.path.dirname(__file__), |
| 31 | + 'tutorialpyar', |
| 32 | + 'original', |
| 33 | + )) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +def get_rst_file(pofilename): |
| 38 | + """Given a .po filename returns the corresponding .rst filename""" |
| 39 | + basename = os.path.basename(pofilename) |
| 40 | + basename, ext = basename.rsplit('.', 1) |
| 41 | + rstfilename = os.path.join(RST_TRADUCIDOS_DIR, f'{basename}.rst') |
| 42 | + if os.path.exists(rstfilename): |
| 43 | + return rstfilename |
| 44 | + |
| 45 | + |
| 46 | +def get_rst_original_filename(rstfilename): |
| 47 | + rst_original_filename = '' |
| 48 | + if rstfilename.endswith('real-index.rst'): |
| 49 | + rst_original_filename = 'index.rst' |
| 50 | + |
| 51 | + basename = os.path.basename(rst_original_filename or rstfilename) |
| 52 | + rst_original_filename = os.path.join(RST_ORIGINAL_DIR, basename) |
| 53 | + if os.path.exists(rst_original_filename): |
| 54 | + return rst_original_filename |
| 55 | + |
| 56 | + |
| 57 | +def create_english_spanish_sentences(rstfilename): |
| 58 | + """Create a tuple of (english, spanish) sentences for rstfilename""" |
| 59 | + |
| 60 | + def get_paragraph(fd): |
| 61 | + lines = [] |
| 62 | + paragraph = [] |
| 63 | + for line in fd.read().splitlines(): |
| 64 | + # import pdb; pdb.set_trace() |
| 65 | + if any([ |
| 66 | + line.startswith('.. '), |
| 67 | + line.startswith('==='), |
| 68 | + line.startswith('---'), |
| 69 | + line.startswith('***'), |
| 70 | + ]): |
| 71 | + continue |
| 72 | + |
| 73 | + if line == '' and not paragraph: |
| 74 | + continue |
| 75 | + |
| 76 | + if line == '': |
| 77 | + lines.append(' '.join(paragraph)) |
| 78 | + paragraph = [] |
| 79 | + continue |
| 80 | + paragraph.append(line) |
| 81 | + |
| 82 | + return lines |
| 83 | + |
| 84 | + # NOTE: we could use docutils and parse the rst in the correct way, but |
| 85 | + # that will probably take more time |
| 86 | + with open(get_rst_original_filename(rstfilename)) as fd: |
| 87 | + english = get_paragraph(fd) |
| 88 | + |
| 89 | + with open(rstfilename) as fd: |
| 90 | + spanish = get_paragraph(fd) |
| 91 | + |
| 92 | + result = list(zip(english, spanish)) |
| 93 | + return result |
| 94 | + |
| 95 | + |
| 96 | +def get_rst_translation_text(rstfilename, english_spanish, text): |
| 97 | + """Given an rstfilename an a text returns the corresponding translated text if exists""" |
| 98 | + for en, es in english_spanish: |
| 99 | + if en.replace("!", "") == text.replace("!", ""): |
| 100 | + return es |
| 101 | + |
| 102 | + |
| 103 | +def update_po_translation(pofilename, english, spanish): |
| 104 | + """Update the pofilename with the translated spanish text""" |
| 105 | + pass |
| 106 | + |
| 107 | + |
| 108 | +for pofilename in glob.glob(PO_DIR + '**/*/*.po'): |
| 109 | + translated = False |
| 110 | + rstfilename = get_rst_file(pofilename) |
| 111 | + if rstfilename is None: |
| 112 | + continue |
| 113 | + |
| 114 | + english_spanish = create_english_spanish_sentences(rstfilename) |
| 115 | + |
| 116 | + po = polib.pofile(pofilename) |
| 117 | + for entry in po: |
| 118 | + english_text = entry.msgid |
| 119 | + spanish_text = entry.msgstr |
| 120 | + if spanish_text: |
| 121 | + # Do not override already translated text |
| 122 | + continue |
| 123 | + |
| 124 | + translated_text = get_rst_translation_text(rstfilename, english_spanish, english_text) |
| 125 | + if translated_text is None: |
| 126 | + continue |
| 127 | + |
| 128 | + translated = True |
| 129 | + |
| 130 | + entry.msgstr = translated_text |
| 131 | + # update_po_translation(po, english_text, translated_text) |
| 132 | + |
| 133 | + if translated: |
| 134 | + po.save(pofilename) |
0 commit comments