Skip to content

Commit 8cbe9ed

Browse files
committed
About to start the (english, spanish) mapping list
1 parent 3951c61 commit 8cbe9ed

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

.migration/rst2po.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# and there is no translation for that sentence/paragraph it updates the .po file with the translated text
77
# from the .rst file.
88

9+
import re
910
import glob
1011
import os
1112
import polib # fades
@@ -17,17 +18,55 @@
1718
'..',
1819
))
1920

20-
RST_DIR = os.path.abspath(
21+
RST_TRADUCIDOS_DIR = os.path.abspath(
2122
os.path.join(
2223
os.path.dirname(__file__),
2324
'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',
2433
))
2534

2635

2736

2837
def get_rst_file(pofilename):
2938
"""Given a .po filename returns the corresponding .rst filename"""
30-
pass
39+
basename = os.path.basename(pofilename)
40+
basename, ext = basename.split('.')
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+
if rstfilename.endswith('real-index.rst'):
48+
rst_original_filename = 'index.rst'
49+
50+
rst_original_filename = os.path.join(RST_ORIGINAL_DIR, rst_original_filename)
51+
if os.path.exists(rst_original_filename):
52+
return rst_original_filename
53+
54+
55+
def create_english_spanish_sentences(rstfilename):
56+
"""Create a tuple of (english, spanish) sentences for rstfilename"""
57+
58+
# NOTE: we could use docutils and parse the rst in the correct way, but
59+
# that will probably take more time
60+
61+
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()
3170

3271

3372
def get_rst_translation_text(rstfilename, text):
@@ -40,12 +79,13 @@ def update_po_translation(pofilename, english, spanish):
4079
pass
4180

4281

43-
44-
for pofilename in glob.glob(PO_DIR + '**/*.po'):
82+
for pofilename in glob.glob(PO_DIR + '**/*/*.po'):
4583
rstfilename = get_rst_file(pofilename)
46-
if rst is None:
84+
if rstfilename is None:
4785
continue
4886

87+
create_english_spanish_sentences(rstfilename)
88+
4989
po = polib.pofile(pofilename)
5090
for entry in po:
5191
english_text = entry.msgid
@@ -58,4 +98,6 @@ def update_po_translation(pofilename, english, spanish):
5898
if translated_text is None:
5999
continue
60100

61-
update_po_translation(po, english_text, translated_text)
101+
entry.msgstr = translated_text
102+
# update_po_translation(po, english_text, translated_text)
103+
po.save(pofilename)

0 commit comments

Comments
 (0)