Skip to content

Commit baebffa

Browse files
committed
runtime: update docs, minor improvements
1 parent 076d05c commit baebffa

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

fluent.runtime/docs/usage.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ attribute, and maybe a title attribute.
286286
... .aria-label = Login input value
287287
... .title = Type your login email
288288
""")
289-
>>> message, attributes = l10n.format_message(
290-
"login-input", {"placeholder": "email@example.com"}
289+
>>> value, attributes = l10n.format_message(
290+
"login-input", {"email": "email@example.com"}
291291
)
292-
>>> message
292+
>>> value
293293
'Predefined value'
294294
>>> attributes
295295
{'placeholder': 'email@example.com', 'aria-label': 'Login input value', 'title': 'Type your login email'}
@@ -298,9 +298,9 @@ You can also use the formatted message without unpacking it.
298298

299299
.. code-block:: python
300300
>>> fmt_msg = l10n.format_message(
301-
"login-input", {"placeholder": "email@example.com"}
301+
"login-input", {"email": "email@example.com"}
302302
)
303-
>>> fmt_msg.message
303+
>>> fmt_msg.value
304304
'Predefined value'
305305
>>> fmt_msg.attributes
306306
{'placeholder': 'email@example.com', 'aria-label': 'Login input value', 'title': 'Type your login email'}

fluent.runtime/fluent/runtime/fallback.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
)
1414

1515
from fluent.syntax import FluentParser
16-
from typing_extensions import NamedTuple
16+
from typing import NamedTuple
1717

1818
from .bundle import FluentBundle
1919

@@ -24,7 +24,7 @@
2424

2525

2626
class FormattedMessage(NamedTuple):
27-
message: Union[str, None]
27+
value: Union[str, None]
2828
attributes: Dict[str, str]
2929

3030

@@ -57,42 +57,44 @@ def __init__(
5757
def format_message(
5858
self, msg_id: str, args: Union[Dict[str, Any], None] = None
5959
) -> 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],
7971
)
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+
)
8183

8284
def format_value(
8385
self, msg_id: str, args: Union[Dict[str, Any], None] = None
8486
) -> 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
9698

9799
def _create_bundle(self, locales: List[str]) -> FluentBundle:
98100
return self.bundle_class(

0 commit comments

Comments
 (0)