Marco Ricci commited on 2025-01-13 17:28:25
Zeige 1 geänderte Dateien mit 23 Einfügungen und 10 Löschungen.
We do need to keep track of the trimming status when adding those variants to the cache, because the debug output formatter will discover the trimmed fields in the template anyway and otherwise attempt to emit them. So store the set of trimmed fields in the cache for each message, and pass it on to the formatter. We will assume that if a field gets trimmed, then it had a `None` value, which is true for all the fields we currently support trimming.
... | ... |
@@ -89,7 +89,10 @@ def load_translations( |
89 | 89 |
|
90 | 90 |
|
91 | 91 |
translation = load_translations() |
92 |
-_debug_translation_message_cache: dict[tuple[str, str], MsgTemplate] = {} |
|
92 |
+_debug_translation_message_cache: dict[ |
|
93 |
+ tuple[str, str], |
|
94 |
+ tuple[MsgTemplate, frozenset], |
|
95 |
+] = {} |
|
93 | 96 |
|
94 | 97 |
|
95 | 98 |
class DebugTranslations(gettext.NullTranslations): |
... | ... |
@@ -107,14 +110,19 @@ class DebugTranslations(gettext.NullTranslations): |
107 | 110 |
for enum_class in MSG_TEMPLATE_CLASSES: |
108 | 111 |
for member in enum_class.__members__.values(): |
109 | 112 |
value = cast('TranslatableString', member.value) |
113 |
+ queue: list[tuple[TranslatableString, frozenset[str]]] = [ |
|
114 |
+ (value, frozenset()) |
|
115 |
+ ] |
|
110 | 116 |
value2 = value.maybe_without_filename() |
111 |
- for v in {value, value2}: |
|
117 |
+ if value != value2: |
|
118 |
+ queue.append((value2, frozenset({'filename'}))) |
|
119 |
+ for v, trimmed in queue: |
|
112 | 120 |
singular = v.singular |
113 | 121 |
plural = v.plural |
114 | 122 |
context = v.l10n_context |
115 |
- cache.setdefault((context, singular), member) |
|
123 |
+ cache.setdefault((context, singular), (member, trimmed)) |
|
116 | 124 |
if plural: |
117 |
- cache.setdefault((context, plural), member) |
|
125 |
+ cache.setdefault((context, plural), (member, trimmed)) |
|
118 | 126 |
|
119 | 127 |
@classmethod |
120 | 128 |
def _locate_message( |
... | ... |
@@ -127,31 +135,36 @@ class DebugTranslations(gettext.NullTranslations): |
127 | 135 |
n: int = 1, |
128 | 136 |
) -> str: |
129 | 137 |
try: |
130 |
- enum_value = _debug_translation_message_cache[context, message] |
|
138 |
+ enum_value, trimmed = _debug_translation_message_cache[ |
|
139 |
+ context, message |
|
140 |
+ ] |
|
131 | 141 |
except KeyError: |
132 | 142 |
return message if not message_plural or n == 1 else message_plural |
133 | 143 |
return cls._format_enum_name_maybe_with_fields( |
134 | 144 |
enum_name=str(enum_value), |
135 | 145 |
ts=cast('TranslatableString', enum_value.value), |
146 |
+ trimmed=trimmed, |
|
136 | 147 |
) |
137 | 148 |
|
138 | 149 |
@staticmethod |
139 | 150 |
def _format_enum_name_maybe_with_fields( |
140 | 151 |
enum_name: str, |
141 | 152 |
ts: TranslatableString, |
153 |
+ trimmed: frozenset[str] = frozenset(), |
|
142 | 154 |
) -> str: |
143 | 155 |
formatter = string.Formatter() |
144 | 156 |
fields: dict[str, int] = {} |
145 | 157 |
for _lit, field, _spec, _conv in formatter.parse(ts.singular): |
146 | 158 |
if field is not None and field not in fields: |
147 | 159 |
fields[field] = len(fields) |
148 |
- sorted_fields = [ |
|
149 |
- f'{field}={{{field}!r}}' |
|
150 |
- for field in sorted(fields.keys(), key=fields.__getitem__) |
|
160 |
+ sorted_fields = sorted(fields.keys(), key=fields.__getitem__) |
|
161 |
+ formatted_fields = [ |
|
162 |
+ f'{f}=None' if f in trimmed else f'{f}={{{f}!r}}' |
|
163 |
+ for f in sorted_fields |
|
151 | 164 |
] |
152 | 165 |
return ( |
153 |
- '{!s}({})'.format(enum_name, ', '.join(sorted_fields)) |
|
154 |
- if sorted_fields |
|
166 |
+ '{!s}({})'.format(enum_name, ', '.join(formatted_fields)) |
|
167 |
+ if formatted_fields |
|
155 | 168 |
else str(enum_name) |
156 | 169 |
) |
157 | 170 |
|
158 | 171 |