|
13 | 13 | ) |
14 | 14 |
|
15 | 15 | from fluent.syntax import FluentParser |
16 | | -from typing_extensions import NamedTuple |
| 16 | +from typing import NamedTuple |
17 | 17 |
|
18 | 18 | from .bundle import FluentBundle |
19 | 19 |
|
|
24 | 24 |
|
25 | 25 |
|
26 | 26 | class FormattedMessage(NamedTuple): |
27 | | - message: Union[str, None] |
| 27 | + value: Union[str, None] |
28 | 28 | attributes: Dict[str, str] |
29 | 29 |
|
30 | 30 |
|
@@ -57,42 +57,44 @@ def __init__( |
57 | 57 | def format_message( |
58 | 58 | self, msg_id: str, args: Union[Dict[str, Any], None] = None |
59 | 59 | ) -> FormattedMessage: |
60 | | - for bundle in self._bundles(): |
61 | | - if not bundle.has_message(msg_id): |
62 | | - continue |
63 | | - msg = bundle.get_message(msg_id) |
64 | | - formatted_attrs = { |
65 | | - attr: cast( |
66 | | - str, |
67 | | - bundle.format_pattern(msg.attributes[attr], args)[0], |
68 | | - ) |
69 | | - for attr in msg.attributes |
70 | | - } |
71 | | - if not msg.value: |
72 | | - val = None |
73 | | - else: |
74 | | - val, _errors = bundle.format_pattern(msg.value, args) |
75 | | - return FormattedMessage( |
76 | | - # Never FluentNone when format_pattern called externally |
77 | | - cast(str, val), |
78 | | - formatted_attrs, |
| 60 | + bundle, msg = next(( |
| 61 | + (bundle, bundle.get_message(msg_id)) |
| 62 | + for bundle in self._bundles() |
| 63 | + if bundle.has_message(msg_id) |
| 64 | + ), (None, None)) |
| 65 | + if not msg: |
| 66 | + return FormattedMessage(msg_id, {}) |
| 67 | + formatted_attrs = { |
| 68 | + attr: cast( |
| 69 | + str, |
| 70 | + bundle.format_pattern(msg.attributes[attr], args)[0], |
79 | 71 | ) |
80 | | - return FormattedMessage(msg_id, {}) |
| 72 | + for attr in msg.attributes |
| 73 | + } |
| 74 | + if not msg.value: |
| 75 | + val = None |
| 76 | + else: |
| 77 | + val, _errors = bundle.format_pattern(msg.value, args) |
| 78 | + return FormattedMessage( |
| 79 | + # Never FluentNone when format_pattern called externally |
| 80 | + cast(str, val), |
| 81 | + formatted_attrs, |
| 82 | + ) |
81 | 83 |
|
82 | 84 | def format_value( |
83 | 85 | self, msg_id: str, args: Union[Dict[str, Any], None] = None |
84 | 86 | ) -> str: |
85 | | - for bundle in self._bundles(): |
86 | | - if not bundle.has_message(msg_id): |
87 | | - continue |
88 | | - msg = bundle.get_message(msg_id) |
89 | | - if not msg.value: |
90 | | - continue |
91 | | - val, _errors = bundle.format_pattern(msg.value, args) |
92 | | - return cast( |
93 | | - str, val |
94 | | - ) # Never FluentNone when format_pattern called externally |
95 | | - return msg_id |
| 87 | + bundle, msg = next(( |
| 88 | + (bundle, bundle.get_message(msg_id)) |
| 89 | + for bundle in self._bundles() |
| 90 | + if bundle.has_message(msg_id) |
| 91 | + ), (None, None)) |
| 92 | + if not msg or not msg.value: |
| 93 | + return msg_id |
| 94 | + val, _errors = bundle.format_pattern(msg.value, args) |
| 95 | + return cast( |
| 96 | + str, val |
| 97 | + ) # Never FluentNone when format_pattern called externally |
96 | 98 |
|
97 | 99 | def _create_bundle(self, locales: List[str]) -> FluentBundle: |
98 | 100 | return self.bundle_class( |
|
0 commit comments