mirror of
https://github.com/minetest/contentdb.git
synced 2024-11-10 01:23:48 +01:00
Fix textdomain validation in .tr parser
This commit is contained in:
parent
40d572d645
commit
966023be17
@ -38,6 +38,7 @@ def parse_tr(filepath: str) -> Translation:
|
||||
assert filename_parts[-1] == "tr"
|
||||
language = filename_parts[-2]
|
||||
textdomain = ".".join(filename_parts[0:-2])
|
||||
had_textdomain_comment = False
|
||||
|
||||
with open(filepath, "r", encoding="utf-8") as existing_file:
|
||||
lines = existing_file.readlines()
|
||||
@ -54,9 +55,14 @@ def parse_tr(filepath: str) -> Translation:
|
||||
# discard all subsequent textdomain lines
|
||||
if line.startswith("# textdomain:"):
|
||||
line_textdomain = line[13:].strip()
|
||||
had_textdomain_comment = True
|
||||
if line_textdomain != textdomain:
|
||||
raise SyntaxError(
|
||||
f"Line {line_index + 1}: The filename's textdomain ({textdomain}) should match the comment ({line_textdomain})")
|
||||
|
||||
elif not had_textdomain_comment:
|
||||
raise SyntaxError(f"Missing `# textdomain: {textdomain}` at the top of the file")
|
||||
|
||||
else:
|
||||
i = 0
|
||||
had_equals = False
|
||||
@ -84,7 +90,7 @@ def parse_tr(filepath: str) -> Translation:
|
||||
if int(code) != next_variable:
|
||||
raise SyntaxError(
|
||||
f"Line {line_index + 1}: Arguments out of order in source, found @{code} and expected @{next_variable}." +
|
||||
"Arguments in source must be in increasing order, without gaps or repetitions, starting from 1")
|
||||
" Arguments in source must be in increasing order, without gaps or repetitions, starting from 1")
|
||||
|
||||
next_variable += 1
|
||||
else:
|
||||
|
@ -1 +1,2 @@
|
||||
# textdomain: bad_args
|
||||
Some @1 args @5=Some @1 args @5
|
||||
|
@ -1 +1,2 @@
|
||||
# textdomain: bad_escape
|
||||
Bad @x escape = Bad @x escape
|
||||
|
@ -20,15 +20,14 @@ def test_parses_tr():
|
||||
assert out.entries["Maybe @\n@=@"] == "Peut être @\n@=@"
|
||||
|
||||
|
||||
def test_parses_tr_infers_textdomain():
|
||||
def test_parses_tr_error_on_missing_textdomain():
|
||||
dirname = os.path.dirname(__file__)
|
||||
filepath = os.path.join(dirname, "no_textdomain_comment.fr.tr")
|
||||
out = parse_tr(filepath)
|
||||
|
||||
assert out.language == "fr"
|
||||
assert out.textdomain == "no_textdomain_comment"
|
||||
assert len(out.entries) == 1
|
||||
assert out.entries["Hello, World!"] == "Bonjour, Monde!"
|
||||
with pytest.raises(SyntaxError) as e:
|
||||
parse_tr(filepath)
|
||||
|
||||
assert str(e.value) == "Missing `# textdomain: no_textdomain_comment` at the top of the file"
|
||||
|
||||
|
||||
def test_parses_tr_error_on_textdomain_mismatch():
|
||||
@ -58,7 +57,7 @@ def test_parses_tr_error_on_bad_escape():
|
||||
with pytest.raises(SyntaxError) as e:
|
||||
parse_tr(filepath)
|
||||
|
||||
assert str(e.value) == "Line 1: Unknown escape character: x"
|
||||
assert str(e.value) == "Line 2: Unknown escape character: x"
|
||||
|
||||
|
||||
def test_parses_tr_error_on_bad_args():
|
||||
@ -68,7 +67,7 @@ def test_parses_tr_error_on_bad_args():
|
||||
with pytest.raises(SyntaxError) as e:
|
||||
parse_tr(filepath)
|
||||
|
||||
assert "Line 1: Arguments out of order in source, found @5 and expected @2." in str(e.value)
|
||||
assert "Line 2: Arguments out of order in source, found @5 and expected @2." in str(e.value)
|
||||
|
||||
|
||||
def test_parses_tr_error_on_unknown_arg():
|
||||
@ -78,4 +77,4 @@ def test_parses_tr_error_on_unknown_arg():
|
||||
with pytest.raises(SyntaxError) as e:
|
||||
parse_tr(filepath)
|
||||
|
||||
assert str(e.value) == "Line 1: Unknown argument @2 in translated string"
|
||||
assert str(e.value) == "Line 2: Unknown argument @2 in translated string"
|
||||
|
@ -1 +1,2 @@
|
||||
# textdomain: unknown_arg
|
||||
Some @1 arg=Some @2 arg
|
||||
|
Loading…
Reference in New Issue
Block a user