6
6
# and there is no translation for that sentence/paragraph it updates the .po file with the translated text
7
7
# from the .rst file.
8
8
9
+ import re
9
10
import glob
10
11
import os
11
12
import polib # fades
17
18
'..' ,
18
19
))
19
20
20
- RST_DIR = os .path .abspath (
21
+ RST_TRADUCIDOS_DIR = os .path .abspath (
21
22
os .path .join (
22
23
os .path .dirname (__file__ ),
23
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' ,
24
33
))
25
34
26
35
27
36
28
37
def get_rst_file (pofilename ):
29
38
"""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 ()
31
70
32
71
33
72
def get_rst_translation_text (rstfilename , text ):
@@ -40,12 +79,13 @@ def update_po_translation(pofilename, english, spanish):
40
79
pass
41
80
42
81
43
-
44
- for pofilename in glob .glob (PO_DIR + '**/*.po' ):
82
+ for pofilename in glob .glob (PO_DIR + '**/*/*.po' ):
45
83
rstfilename = get_rst_file (pofilename )
46
- if rst is None :
84
+ if rstfilename is None :
47
85
continue
48
86
87
+ create_english_spanish_sentences (rstfilename )
88
+
49
89
po = polib .pofile (pofilename )
50
90
for entry in po :
51
91
english_text = entry .msgid
@@ -58,4 +98,6 @@ def update_po_translation(pofilename, english, spanish):
58
98
if translated_text is None :
59
99
continue
60
100
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