37
37
def get_rst_file (pofilename ):
38
38
"""Given a .po filename returns the corresponding .rst filename"""
39
39
basename = os .path .basename (pofilename )
40
- basename , ext = basename .split ('.' )
40
+ basename , ext = basename .rsplit ('.' , 1 )
41
41
rstfilename = os .path .join (RST_TRADUCIDOS_DIR , f'{ basename } .rst' )
42
42
if os .path .exists (rstfilename ):
43
43
return rstfilename
44
44
45
45
46
46
def get_rst_original_filename (rstfilename ):
47
+ rst_original_filename = ''
47
48
if rstfilename .endswith ('real-index.rst' ):
48
49
rst_original_filename = 'index.rst'
49
50
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 )
51
53
if os .path .exists (rst_original_filename ):
52
54
return rst_original_filename
53
55
54
56
55
57
def create_english_spanish_sentences (rstfilename ):
56
58
"""Create a tuple of (english, spanish) sentences for rstfilename"""
57
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
+
58
84
# NOTE: we could use docutils and parse the rst in the correct way, but
59
85
# that will probably take more time
86
+ with open (get_rst_original_filename (rstfilename )) as fd :
87
+ english = get_paragraph (fd )
60
88
61
89
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
70
94
71
95
72
- def get_rst_translation_text (rstfilename , text ):
96
+ def get_rst_translation_text (rstfilename , english_spanish , text ):
73
97
"""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
75
101
76
102
77
103
def update_po_translation (pofilename , english , spanish ):
@@ -84,7 +110,7 @@ def update_po_translation(pofilename, english, spanish):
84
110
if rstfilename is None :
85
111
continue
86
112
87
- create_english_spanish_sentences (rstfilename )
113
+ english_spanish = create_english_spanish_sentences (rstfilename )
88
114
89
115
po = polib .pofile (pofilename )
90
116
for entry in po :
@@ -94,7 +120,7 @@ def update_po_translation(pofilename, english, spanish):
94
120
# Do not override already translated text
95
121
continue
96
122
97
- translated_text = get_rst_translation_text (rstfilename , english_text )
123
+ translated_text = get_rst_translation_text (rstfilename , english_spanish , english_text )
98
124
if translated_text is None :
99
125
continue
100
126
0 commit comments