mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-03 15:44:54 +00:00
Fix invalid placeholders in translations (#7612)
* Add logic to find invalid string placeholders in `checkForbiddenTerms.py` * Fix found invalid placeholder in Chinese translations
This commit is contained in:
committed by
GitHub
parent
52fcc797b7
commit
e5065d9baa
@@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
|
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
|
||||||
<plurals name="a11y_polls_percent_of_total">
|
<plurals name="a11y_polls_percent_of_total">
|
||||||
<item quantity="other">"占总投票的 %1$d%"</item>
|
<item quantity="other">"占总投票的 %1$d%%"</item>
|
||||||
</plurals>
|
</plurals>
|
||||||
<string name="a11y_polls_will_remove_selection">"将移除之前的选择"</string>
|
<string name="a11y_polls_will_remove_selection">"将移除之前的选择"</string>
|
||||||
<string name="a11y_polls_winning_answer">"此为胜出的答案"</string>
|
<string name="a11y_polls_winning_answer">"此为胜出的答案"</string>
|
||||||
|
|||||||
@@ -42,6 +42,31 @@ forbiddenTerms = {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# A complete, VALID placeholder, anchored to consume the whole token:
|
||||||
|
# %% -> literal percent (allowed)
|
||||||
|
# %s %d %f ... -> simple conversion
|
||||||
|
# %1$s %2$d ... -> positional
|
||||||
|
# %.2f %1$.2f -> optional precision
|
||||||
|
_VALID_PLACEHOLDER = re.compile(
|
||||||
|
r'%(?:\d+\$)?(?:\.\d+)?[a-zA-Z]'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Grab each candidate token: a '%' plus the run of chars that could belong to a
|
||||||
|
# specifier (digits, '$', '.', and letters). This makes '%sd' and '%1d' surface
|
||||||
|
# as single tokens instead of being partially swallowed.
|
||||||
|
_PLACEHOLDER_MATCHES = re.compile(r'%%|%[0-9$.A-Za-z]*')
|
||||||
|
|
||||||
|
def find_invalid_placeholders(value):
|
||||||
|
"""Return the list of malformed placeholder tokens found in `value`."""
|
||||||
|
invalid = []
|
||||||
|
for token in _PLACEHOLDER_MATCHES.findall(value):
|
||||||
|
if token == '%%':
|
||||||
|
continue # escaped percent is fine
|
||||||
|
if not _VALID_PLACEHOLDER.fullmatch(token):
|
||||||
|
invalid.append(token)
|
||||||
|
return invalid
|
||||||
|
|
||||||
|
|
||||||
content = minidom.parse(file)
|
content = minidom.parse(file)
|
||||||
|
|
||||||
errors = []
|
errors = []
|
||||||
@@ -60,6 +85,9 @@ for elem in content.getElementsByTagName('string'):
|
|||||||
matches = re.search(term, value)
|
matches = re.search(term, value)
|
||||||
if matches and name not in exceptions:
|
if matches and name not in exceptions:
|
||||||
errors.append('Forbidden term "' + term + '" in string: "' + name + '": ' + value)
|
errors.append('Forbidden term "' + term + '" in string: "' + name + '": ' + value)
|
||||||
|
invalid_placeholders = find_invalid_placeholders(value)
|
||||||
|
for placeholder in invalid_placeholders:
|
||||||
|
errors.append('Invalid placeholder "' + placeholder + '" in string: "' + name + '": ' + value)
|
||||||
|
|
||||||
### Plurals
|
### Plurals
|
||||||
for elem in content.getElementsByTagName('plurals'):
|
for elem in content.getElementsByTagName('plurals'):
|
||||||
@@ -78,6 +106,9 @@ for elem in content.getElementsByTagName('plurals'):
|
|||||||
matches = re.search(term, value)
|
matches = re.search(term, value)
|
||||||
if matches and name not in exceptions:
|
if matches and name not in exceptions:
|
||||||
errors.append('Forbidden term "' + term + '" in plural: "' + name + '": ' + value)
|
errors.append('Forbidden term "' + term + '" in plural: "' + name + '": ' + value)
|
||||||
|
invalid_placeholders = find_invalid_placeholders(value)
|
||||||
|
for placeholder in invalid_placeholders:
|
||||||
|
errors.append('Invalid placeholder "' + placeholder + '" in plural: "' + name + '": ' + value)
|
||||||
|
|
||||||
# If errors is not empty print the report
|
# If errors is not empty print the report
|
||||||
if errors:
|
if errors:
|
||||||
|
|||||||
Reference in New Issue
Block a user