1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00

Hassfest check for invalid localization placeholders (#155216)

This commit is contained in:
karwosts
2025-10-31 06:43:11 -07:00
committed by GitHub
parent 1ddb39f6d0
commit 7cec3aa27c
5 changed files with 20 additions and 4 deletions
+16
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
from functools import partial
import json
import re
import string
from typing import Any
import voluptuous as vol
@@ -131,10 +132,12 @@ def translation_value_validator(value: Any) -> str:
- prevents string with HTML
- prevents strings with single quoted placeholders
- prevents strings with placeholders using invalid identifiers
- prevents combined translations
"""
string_value = cv.string_with_no_html(value)
string_value = string_no_single_quoted_placeholders(string_value)
string_value = validate_placeholders(string_value)
if RE_COMBINED_REFERENCE.search(string_value):
raise vol.Invalid("the string should not contain combined translations")
if string_value != string_value.strip():
@@ -151,6 +154,19 @@ def string_no_single_quoted_placeholders(value: str) -> str:
return value
def validate_placeholders(value: str) -> str:
"""Validate that placeholders in translations use valid identifiers."""
formatter = string.Formatter()
for _, field_name, _, _ in formatter.parse(value):
if field_name: # skip literal text segments
if not field_name.isidentifier():
raise vol.Invalid(
"placeholders must be valid identifiers ([a-zA-Z_][a-zA-Z0-9_]*)"
)
return value
def gen_data_entry_schema(
*,
config: Config,