Skip to content

Commit e06e3e3

Browse files
committed
Post process app PO to merge paragraphs
1 parent 575b574 commit e06e3e3

20 files changed

Lines changed: 832 additions & 1952 deletions

addons/learn_pot_extractor/LearnPOTExtractor.gd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func _generate_all_pot_files() -> void:
5555
await _generate_course_pot()
5656
await _generate_application_pot()
5757
await _generate_supplemantary_pots()
58+
await _slipstream_and_clean()
5859

5960

6061
# ⚠ Only use if you know what you're doing ⚠
@@ -154,6 +155,21 @@ func _slipstream_existing_translations() -> void:
154155
var global_og_lang_app := "%s/%s/application.po" % [global_base_dir, lang]
155156
OS.execute("msgmerge", ["--no-wrap", "-o", global_lang_app, global_og_lang_app, global_app])
156157

158+
159+
# post process to match old format, since godot handles linebroken paragraphs whole
160+
var header := []
161+
var tr_blocks := SHARED.build_tr_blocks(global_lang_app, true, header)
162+
var unsure_tr_blocks := SHARED.get_unsure_tr_blocks(global_lang_app)
163+
164+
for i in range(unsure_tr_blocks.size()-1, -1, -1):
165+
var unsure_block: Dictionary = unsure_tr_blocks[i]
166+
for block in tr_blocks:
167+
if unsure_block.id in block.id and "\\n" in block.id:
168+
block.comments.comments.erase("fuzzy")
169+
block.str = "%s\\n\\n%s" % [unsure_block.str, block.str]
170+
171+
SHARED.write_from_tr_blocks(global_lang_app, "\n".join(header), tr_blocks)
172+
157173
var global_og_lang_error := "%s/%s/error_database.po" % [global_base_dir, lang]
158174
var global_og_lang_glossary := "%s/%s/glossary_database.po" % [global_base_dir, lang]
159175
var global_og_lang_doc := "%s/%s/classref_database.po" % [global_base_dir, lang]

addons/learn_pot_extractor/Shared.gd

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ static var POT_PATTERN := RegEx.create_from_string(
99
r'(?<id>msgid (?:""\n(?:"(?:\\.|[^"\\])*"\n)+|"(?:\\.|[^"\\])*"\n))' +
1010
r'(?<str>msgstr (?:""\n(?:"(?:\\.|[^"\\])*"\n)+|"(?:\\.|[^"\\])*"\n))'
1111
)
12+
13+
static var UNSURE_POT_PATTERN := RegEx.create_from_string(
14+
r'(?<id>#~ msgid (?:""\n(?:"(?:\\.|[^"\\])*"\n)+|"(?:\\.|[^"\\])*"\n))' +
15+
r'(?<str>#~ msgstr (?:""\n(?:"(?:\\.|[^"\\])*"\n)+|"(?:\\.|[^"\\])*"\n))'
16+
)
1217
static var GLOSSARY_TERM_RE := RegEx.create_from_string(r'\[glossary term=\\"([^\\]+)\\"\]')
1318
static var TAG_RE := RegEx.create_from_string(r'\[[^\[]+\]([^\[]+)\[[^\[]+\]')
1419
static var SPACE_NEWLINE_RE := RegEx.create_from_string(r'\s+\\n')
1520
static var WHITESPACE_RE := RegEx.create_from_string(r'\s+')
1621

1722

18-
static func build_tr_blocks(po_file: String, skip_header := true, out_header: Array = []) -> Array[Dictionary]:
23+
static func get_unsure_tr_blocks(po_file: String, skip_header := true, out_header: Array = []) -> Array[Dictionary]:
24+
return build_tr_blocks(po_file, skip_header, out_header, UNSURE_POT_PATTERN, 3)
25+
26+
27+
static func build_tr_blocks(po_file: String, skip_header := true, out_header: Array = [], target_regex := POT_PATTERN, prefix_offset := 0) -> Array[Dictionary]:
1928
var po_text := FileAccess.open(po_file, FileAccess.READ).get_as_text()
2029

2130
var start_index := (po_text.find("\n\n")+2)
@@ -26,13 +35,13 @@ static func build_tr_blocks(po_file: String, skip_header := true, out_header: Ar
2635
out_header[0] = header
2736

2837
var tr_blocks: Array[Dictionary] = []
29-
tr_blocks.append_array(POT_PATTERN.search_all(po_text, start_index).map(func(block_match: RegExMatch) -> Dictionary:
38+
tr_blocks.append_array(target_regex.search_all(po_text, start_index).map(func(block_match: RegExMatch) -> Dictionary:
3039
return {
3140
"comments": _parse_course_comment(block_match),
32-
"ctxt": block_match.get_string("ctxt").substr(9, block_match.get_string("ctxt").length()-11),
33-
"id": _parse_course_string(block_match, true),
34-
"normalized_id": normalize(_parse_course_string(block_match, true)),
35-
"str": _parse_course_string(block_match, false)
41+
"ctxt": block_match.get_string("ctxt").substr(9 + prefix_offset, block_match.get_string("ctxt").length()-(11 + prefix_offset)),
42+
"id": _parse_course_string(block_match, true, prefix_offset),
43+
"normalized_id": normalize(_parse_course_string(block_match, true, prefix_offset)),
44+
"str": _parse_course_string(block_match, false, prefix_offset)
3645
}
3746
))
3847
var duplicate_blocks := []
@@ -54,13 +63,18 @@ static func write_from_tr_blocks(po_file: String, header: String, blocks: Array[
5463
var lines := [header]
5564

5665
for block in blocks:
66+
var append_fuzzy := false
5767
for comment in block.comments.comments:
5868
if comment == "fuzzy":
59-
lines.append("#, fuzzy")
69+
append_fuzzy = true
6070
else:
6171
lines.append("#. %s" % [comment])
6272
for source in block.comments.sources:
63-
lines.append("#: %s:%s" % [source.lesson, source.line_number])
73+
if source.lesson:
74+
lines.append("#: %s%s" % [source.lesson, ":%s" % [source.line_number] if source.lesson.get_extension() != "tscn" else ""])
75+
76+
if append_fuzzy:
77+
lines.append("#, fuzzy")
6478

6579
if block.ctxt:
6680
lines.append('msgctxt "%s"' % [block.ctxt])
@@ -92,18 +106,21 @@ static func _parse_course_comment(target: RegExMatch) -> Dictionary:
92106

93107
var comments := target.get_string("comment").split("\n", false)
94108
for comment in comments:
95-
if comment.begins_with("#: course/"):
109+
if comment.begins_with("#: "):
96110
var line_number_idx := comment.rfind(":")
97-
var path := comment.substr(3, line_number_idx - 3)
98-
var line_number := comment.substr(line_number_idx+1).to_int()
111+
var has_line_number := comment.count(":") > 1
112+
var path := comment.substr(3, (line_number_idx - 3) if has_line_number else -1)
113+
var line_number := 0
114+
if has_line_number:
115+
line_number = comment.substr(line_number_idx+1).to_int()
99116
(result.sources as Array).push_back({"lesson": path, "line_number": line_number})
100117
else:
101118
(result.comments as Array).push_back(comment.substr(2 if comment.begins_with("# ") else 3))
102119

103120
return result
104121

105122

106-
static func _parse_course_string(target: RegExMatch, is_id: bool) -> String:
123+
static func _parse_course_string(target: RegExMatch, is_id: bool, prefix_offset := 0) -> String:
107124
var id := target.get_string("id" if is_id else "str")
108125

109126
var result := ""
@@ -112,11 +129,11 @@ static func _parse_course_string(target: RegExMatch, is_id: bool) -> String:
112129
var lines := id.split("\n").slice(1)
113130
for line in lines:
114131
if not line.ends_with('\n"'):
115-
result += line.substr(1, line.length()-2)
132+
result += line.substr(1, line.length()-(2 + prefix_offset))
116133
else:
117-
result += line.substr(1, line.length()-2) + "\n"
134+
result += line.substr(1, line.length()-(2 + prefix_offset)) + "\n"
118135
else:
119-
result = id.substr(7 + (0 if is_id else 1), id.length()-(9 + (0 if is_id else 1)))
136+
result = id.substr(7 + (0 if is_id else 1) + prefix_offset, id.length()-(9 + (0 if is_id else 1) + prefix_offset))
120137

121138
return result
122139

0 commit comments

Comments
 (0)