mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Fix placeholder quotes (#114974)
* When quoting placeholders, always use double quotes so Lokalise recognizes the placeholder. * Ensure that strings does not contain placeholders in single quotes. * Avoid redefining value * Moved string_with_no_placeholders_in_single_quotes * Define regex once * Fix tests
This commit is contained in:
committed by
GitHub
parent
bd9070be11
commit
fdef3ece13
@@ -24,6 +24,7 @@ REMOVED = 2
|
||||
RE_REFERENCE = r"\[\%key:(.+)\%\]"
|
||||
RE_TRANSLATION_KEY = re.compile(r"^(?!.+[_-]{2})(?![_-])[a-z0-9-_]+(?<![_-])$")
|
||||
RE_COMBINED_REFERENCE = re.compile(r"(.+\[%)|(%\].+)")
|
||||
RE_PLACEHOLDER_IN_SINGLE_QUOTES = re.compile(r"'{\w+}'")
|
||||
|
||||
# Only allow translation of integration names if they contain non-brand names
|
||||
ALLOW_NAME_TRANSLATION = {
|
||||
@@ -128,14 +129,25 @@ def translation_value_validator(value: Any) -> str:
|
||||
"""Validate that the value is a valid translation.
|
||||
|
||||
- prevents string with HTML
|
||||
- prevents strings with single quoted placeholders
|
||||
- prevents combined translations
|
||||
"""
|
||||
value = cv.string_with_no_html(value)
|
||||
value = string_no_single_quoted_placeholders(value)
|
||||
if RE_COMBINED_REFERENCE.search(value):
|
||||
raise vol.Invalid("the string should not contain combined translations")
|
||||
return str(value)
|
||||
|
||||
|
||||
def string_no_single_quoted_placeholders(value: str) -> str:
|
||||
"""Validate that the value does not contain placeholders inside single quotes."""
|
||||
if RE_PLACEHOLDER_IN_SINGLE_QUOTES.search(value):
|
||||
raise vol.Invalid(
|
||||
"the string should not contain placeholders inside single quotes"
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
def gen_data_entry_schema(
|
||||
*,
|
||||
config: Config,
|
||||
|
||||
Reference in New Issue
Block a user