Skip to content

Commit bd16ad8

Browse files
authored
Translation script: Fix incorrect multiline matches (#15)
Prevously, the translation update script would match 'if not input:find("%S") then' because 'S"' is a valid function call expression. This commit addresses that by only matching single lines. Whereas that's a workaround, it does work well. --> Tested on luanti/builtin
1 parent 51beb5f commit bd16ad8

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

mod_translation_updater.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,16 @@ def main():
119119
update_folder(os.path.abspath("./"))
120120

121121
# Compile pattern for matching lua function call
122-
def compile_func_call_pattern(argument_pattern):
122+
# flag = re.DOTALL : Allow matching multiple lines
123+
# flag = re.NOFLAG : Match single lines only
124+
def compile_func_call_pattern(argument_pattern: str, flag):
123125
return re.compile(
124126
# Look for beginning of file or anything that isn't a function identifier
125127
r'(?<![a-zA-Z0-9_])' +
126128
# Matches S, FS, NS, or NFS function call
127129
r'N?F?S\s*' +
128130
# The pattern to match argument
129-
argument_pattern,
130-
re.DOTALL)
131+
argument_pattern, flag)
131132

132133
# Add parentheses around a pattern
133134
def parenthesize_pattern(pattern):
@@ -148,17 +149,17 @@ def parenthesize_pattern(pattern):
148149
pattern_lua_square_bracket_string = r'\[\[(.*?)\]\]'
149150

150151
# Handles the " ... " or ' ... ' string delimiters
151-
pattern_lua_quoted = compile_func_call_pattern(parenthesize_pattern(pattern_lua_quoted_string))
152+
pattern_lua_quoted = compile_func_call_pattern(parenthesize_pattern(pattern_lua_quoted_string), re.NOFLAG)
152153

153154
# Handles the [[ ... ]] string delimiters
154-
pattern_lua_bracketed = compile_func_call_pattern(parenthesize_pattern(pattern_lua_square_bracket_string))
155+
pattern_lua_bracketed = compile_func_call_pattern(parenthesize_pattern(pattern_lua_square_bracket_string), re.DOTALL)
155156

156157
# Handles like pattern_lua_quoted, but for single parameter (without parentheses)
157158
# See https://www.lua.org/pil/5.html for informations about single argument call
158-
pattern_lua_quoted_single = compile_func_call_pattern(pattern_lua_quoted_string)
159+
pattern_lua_quoted_single = compile_func_call_pattern(pattern_lua_quoted_string, re.NOFLAG)
159160

160161
# Same as pattern_lua_quoted_single, but for [[ ... ]] string delimiters
161-
pattern_lua_bracketed_single = compile_func_call_pattern(pattern_lua_square_bracket_string)
162+
pattern_lua_bracketed_single = compile_func_call_pattern(pattern_lua_square_bracket_string, re.DOTALL)
162163

163164
# Handles "concatenation" .. " of strings"
164165
pattern_concat = re.compile(r'["\'][\s]*\.\.[\s]*["\']', re.DOTALL)
@@ -220,14 +221,15 @@ def mkdir_p(path):
220221
# dKeyStrings is a dictionary of localized string to source file sets
221222
# dOld is a dictionary of existing translations and comments from
222223
# the previous version of this text
223-
def strings_to_text(dkeyStrings, dOld, mod_name, header_comments, textdomain, templ = None):
224+
def strings_to_text(dkeyStrings: dict, dOld: dict, mod_name: str, header_comments,
225+
textdomain: str|None, templ: list|None):
224226
# if textdomain is specified, insert it at the top
225227
if textdomain != None:
226228
lOut = [textdomain] # argument is full textdomain line
227229
# otherwise, use mod name as textdomain automatically
228230
else:
229231
lOut = [f"# textdomain: {mod_name}"]
230-
if templ is not None and templ[2] and (header_comments is None or not header_comments.startswith(templ[2])):
232+
if templ != None and templ[2] and (header_comments is None or not header_comments.startswith(templ[2])):
231233
# header comments in the template file
232234
lOut.append(templ[2])
233235
if header_comments is not None:
@@ -245,6 +247,7 @@ def strings_to_text(dkeyStrings, dOld, mod_name, header_comments, textdomain, te
245247
lSourceKeys = list(dGroupedBySource.keys())
246248
lSourceKeys.sort()
247249
for source in lSourceKeys:
250+
# source: relative path to the .lua file
248251
localizedStrings = dGroupedBySource[source]
249252
if params["print-source"]:
250253
if lOut[-1] != "":
@@ -296,7 +299,8 @@ def write_template(templ_file, dkeyStrings, mod_name):
296299
# read existing template file to preserve comments
297300
existing_template = import_tr_file(templ_file)
298301

299-
text = strings_to_text(dkeyStrings, existing_template[0], mod_name, existing_template[2], existing_template[3])
302+
text = strings_to_text(dkeyStrings, existing_template[0], mod_name,
303+
existing_template[2], existing_template[3], None)
300304
mkdir_p(os.path.dirname(templ_file))
301305
with open(templ_file, "wt", encoding='utf-8') as template_file:
302306
template_file.write(text)

0 commit comments

Comments
 (0)