Coverage for app/venv/lib/python3.14/site-packages/weblate/trans/autofixes/custom.py: 0%
17 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
1# Copyright © Michal Čihař <michal@weblate.org>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
5"""Auto fixes implemented for specific environments and not enabled by default."""
7import re
9from django.utils.translation import gettext_lazy
11from weblate.trans.autofixes.base import AutoFix
13QUOTE_PARAM = re.compile(r"'(\{[^}]+\})'")
14SINGLE_APO = re.compile(r"'{1,3}")
15DOUBLE_APO = re.compile(r"'{4,}")
16REPLACEMENT = "__weblate:quote__"
17REPLACE_STRING = rf"{REPLACEMENT}\1{REPLACEMENT}"
20class DoubleApostrophes(AutoFix):
21 """
22 Ensures apostrophes are escaped in Java Properties MessageFormat string.
24 - all apostrophes except ones around {} vars are doubled
26 Note: This fix is not really generically applicable in all cases, that's
27 why it's not enabled by default.
28 """
30 fix_id = "java-messageformat"
31 name = gettext_lazy("Apostrophes in Java MessageFormat")
33 def fix_single_target(self, target, source, unit):
34 flags = unit.all_flags
35 if ("auto-java-messageformat" not in flags or "{0" not in source) and (
36 "java-format" not in flags
37 ):
38 return target, False
39 # Split on apostrophe
40 new = SINGLE_APO.sub(
41 "''", DOUBLE_APO.sub("''''", QUOTE_PARAM.sub(REPLACE_STRING, target))
42 ).replace(REPLACEMENT, "'")
43 return new, new != target