Skip to content

Commit 25b5aa7

Browse files
committed
Initial working version of rst2po.py
1 parent 5e07374 commit 25b5aa7

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

.migration/rst2po.py

+40-14
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,67 @@
3737
def get_rst_file(pofilename):
3838
"""Given a .po filename returns the corresponding .rst filename"""
3939
basename = os.path.basename(pofilename)
40-
basename, ext = basename.split('.')
40+
basename, ext = basename.rsplit('.', 1)
4141
rstfilename = os.path.join(RST_TRADUCIDOS_DIR, f'{basename}.rst')
4242
if os.path.exists(rstfilename):
4343
return rstfilename
4444

4545

4646
def get_rst_original_filename(rstfilename):
47+
rst_original_filename = ''
4748
if rstfilename.endswith('real-index.rst'):
4849
rst_original_filename = 'index.rst'
4950

50-
rst_original_filename = os.path.join(RST_ORIGINAL_DIR, rst_original_filename)
51+
basename = os.path.basename(rst_original_filename or rstfilename)
52+
rst_original_filename = os.path.join(RST_ORIGINAL_DIR, basename)
5153
if os.path.exists(rst_original_filename):
5254
return rst_original_filename
5355

5456

5557
def create_english_spanish_sentences(rstfilename):
5658
"""Create a tuple of (english, spanish) sentences for rstfilename"""
5759

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+
5884
# NOTE: we could use docutils and parse the rst in the correct way, but
5985
# that will probably take more time
86+
with open(get_rst_original_filename(rstfilename)) as fd:
87+
english = get_paragraph(fd)
6088

6189
with open(rstfilename) as fd:
62-
lines = []
63-
for line in fd.read().splitlines():
64-
if re.match('^[a-zA-Z] ', line):
65-
# keep text lines only
66-
lines.append(line)
67-
# make the document just one line so we can split it in sentences
68-
document = ' '.join(lines)
69-
import pdb; pdb.set_trace()
90+
spanish = get_paragraph(fd)
91+
92+
result = list(zip(english, spanish))
93+
return result
7094

7195

72-
def get_rst_translation_text(rstfilename, text):
96+
def get_rst_translation_text(rstfilename, english_spanish, text):
7397
"""Given an rstfilename an a text returns the corresponding translated text if exists"""
74-
pass
98+
for en, es in english_spanish:
99+
if en == text:
100+
return es
75101

76102

77103
def update_po_translation(pofilename, english, spanish):
@@ -84,7 +110,7 @@ def update_po_translation(pofilename, english, spanish):
84110
if rstfilename is None:
85111
continue
86112

87-
create_english_spanish_sentences(rstfilename)
113+
english_spanish = create_english_spanish_sentences(rstfilename)
88114

89115
po = polib.pofile(pofilename)
90116
for entry in po:
@@ -94,7 +120,7 @@ def update_po_translation(pofilename, english, spanish):
94120
# Do not override already translated text
95121
continue
96122

97-
translated_text = get_rst_translation_text(rstfilename, english_text)
123+
translated_text = get_rst_translation_text(rstfilename, english_spanish, english_text)
98124
if translated_text is None:
99125
continue
100126

0 commit comments

Comments
 (0)