Skip to content

Commit 9e3fcbb

Browse files
Fix translate tags being added when existing translate tags are present (#20)
Fix translate tags being added when existing translate tags are present (#20)
1 parent e880004 commit 9e3fcbb

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

app.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,23 @@ def process_small_tag(text):
270270
wrapped_content = _wrap_in_translate(content)
271271
return f"{prefix}{wrapped_content}{suffix}"
272272

273+
def process_existing_translate(text):
274+
"""
275+
Processes existing <translate> tags in the wikitext.
276+
It removes the existing tags and processes the content through the converter,
277+
which will add new translate tags as needed.
278+
"""
279+
assert(text.startswith('<translate>') and text.endswith('</translate>')), "Invalid translate tag"
280+
start_tag_end = text.find('>') + 1
281+
end_tag_start = text.rfind('<')
282+
if start_tag_end >= end_tag_start:
283+
return ""
284+
content = text[start_tag_end:end_tag_start]
285+
if not content.strip():
286+
return content # Return just whitespace without tags
287+
# Process the content through the converter (it will add translate tags as needed)
288+
return convert_to_translatable_wikitext(content)
289+
273290
def process_nowiki(text):
274291
"""
275292
Processes <nowiki> tags in the wikitext.
@@ -539,6 +556,38 @@ def convert_to_translatable_wikitext(wikitext):
539556
curr = end_pos
540557
last = curr
541558
continue
559+
560+
# Process content inside existing <translate> tags
561+
pattern = '<translate>'
562+
if wikitext.startswith(pattern, curr):
563+
end_pattern = wikitext.find('</translate>', curr) + len('</translate>')
564+
if last < curr:
565+
parts.append((wikitext[last:curr], _wrap_in_translate))
566+
parts.append((wikitext[curr:end_pattern], process_existing_translate))
567+
curr = end_pattern
568+
last = curr
569+
continue
570+
571+
pattern = '<languages/>'
572+
if wikitext.startswith(pattern, curr):
573+
end_pattern = curr + len(pattern)
574+
if last < curr:
575+
parts.append((wikitext[last:curr], _wrap_in_translate))
576+
parts.append((wikitext[curr:end_pattern], lambda x: x))
577+
curr = end_pattern
578+
last = curr
579+
continue
580+
581+
pattern = '<language>'
582+
if wikitext.startswith(pattern, curr):
583+
end_pattern = curr + len('<language>')
584+
if last < curr:
585+
parts.append((wikitext[last:curr], _wrap_in_translate))
586+
parts.append((wikitext[curr:end_pattern], lambda x: x))
587+
curr = end_pattern
588+
last = curr
589+
continue
590+
542591
# Table block
543592
pattern = '{|'
544593
if wikitext.startswith(pattern, curr):

tests.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_template_with_parameters(self):
8888
def test_template_nested_in_text(self):
8989
self.assertEqual(
9090
convert_to_translatable_wikitext('Some text with {{a template here}} and more text.'),
91-
'<translate>Some text with</translate> {{A template here}} <translate>and more text.</translate>'
91+
'<translate>Some text with</translate> {{a template here}} <translate>and more text.</translate>'
9292
)
9393

9494
def test_nowiki_tag(self):
@@ -110,11 +110,13 @@ def test_poem_tag(self):
110110
)
111111

112112
def test_code_tag_with_tvar(self):
113+
113114
# Assuming process_code_tag assigns tvar names sequentially starting from 0
114115
self.assertEqual(
115116
convert_to_translatable_wikitext("Here is <code>some code</code> for you."),
116-
"<translate>Here is <code><tvar name=code0>some code</tvar></code> for you.</translate>"
117+
"<translate>Here is <tvar name=code0><code>some code</code></tvar> for you.</translate>"
117118
)
119+
118120

119121
def test_div_tag(self):
120122
self.assertEqual(
@@ -187,6 +189,12 @@ def test_definition_list(self):
187189
convert_to_translatable_wikitext(";Term\n:Definition\n:Description"),
188190
"; <translate>Term</translate>\n: <translate>Definition</translate>\n: <translate>Description</translate>\n"
189191
)
192+
193+
def test_existing_translate_tags(self):
194+
self.assertEqual(
195+
convert_to_translatable_wikitext("<translate>This is already translated.</translate>"),
196+
"<translate>This is already translated.</translate>"
197+
)
190198

191199
if __name__ == '__main__':
192200
unittest.main(exit=False, failfast=True)

0 commit comments

Comments
 (0)