Merge pull request #1245 from Yubico/lint-customizable-punctuation

Make punctuation customizable per language in translation linter
This commit is contained in:
Emil Lundberg 2023-11-02 17:06:19 +01:00 committed by GitHub
commit fcb665e5f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 5 deletions

View File

@ -40,7 +40,7 @@ def check_duplicate_values(strings):
seen[v] = k seen[v] = k
def check_prefixes(k, v, s_max_words, s_max_len): def check_prefixes(k, v, s_max_words, s_max_len, p_ending_chars, q_ending_chars):
errs = [] errs = []
if k.startswith("s_"): if k.startswith("s_"):
if len(v) > s_max_len: if len(v) > s_max_len:
@ -53,11 +53,11 @@ def check_prefixes(k, v, s_max_words, s_max_len):
if ". " in v: if ". " in v:
errs.append("Spans multiple sentences") errs.append("Spans multiple sentences")
elif k.startswith("p_"): elif k.startswith("p_"):
if v[-1] not in ".!": if p_ending_chars and not any(v.endswith(p) for p in p_ending_chars):
errs.append("Doesn't end in punctuation") errs.append("Doesn't end in punctuation")
elif k.startswith("q_"): elif k.startswith("q_"):
if not v.endswith("?"): if q_ending_chars and not any(v.endswith(q) for q in q_ending_chars):
errs.append("Doesn't end in '?'") errs.append("Doesn't end in question mark.")
return errs return errs
@ -81,6 +81,8 @@ def lint_strings(strings, rules):
v, v,
rules.get("s_max_words", 4), rules.get("s_max_words", 4),
rules.get("s_max_len", 32), rules.get("s_max_len", 32),
rules.get("p_ending_chars", ".!"),
rules.get("q_ending_chars", "?"),
) )
) )
errs.extend(check_misc(k, v)) errs.extend(check_misc(k, v))
@ -101,7 +103,7 @@ with open(target, encoding='utf-8') as f:
strings = {k: v for k, v in values.items() if not k.startswith("@")} strings = {k: v for k, v in values.items() if not k.startswith("@")}
print(target, f"- checking {len(strings)} strings") print(target, f"- checking {len(strings)} strings")
lint_strings(strings, strings.get("@_lint_rules", {})) lint_strings(strings, values.get("@_lint_rules", {}))
check_duplicate_values(strings) check_duplicate_values(strings)
if errors: if errors:

View File

@ -16,6 +16,8 @@
}, },
"@_lint_rules": { "@_lint_rules": {
"p_ending_chars": ".!",
"q_ending_chars": "?",
"s_max_words": 4, "s_max_words": 4,
"s_max_length": 32 "s_max_length": 32
}, },

View File

@ -16,6 +16,8 @@
}, },
"@_lint_rules": { "@_lint_rules": {
"p_ending_chars": "。!",
"q_ending_chars": "?",
"s_max_words": 4, "s_max_words": 4,
"s_max_length": 32 "s_max_length": 32
}, },