Skip to content

Commit 9a75356

Browse files
committed
fix external variables to be fluent types
1 parent e2f8395 commit 9a75356

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

fluent.runtime/fluent/runtime/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .builtins import BUILTINS
1111
from .prepare import Compiler
1212
from .resolver import ResolverEnvironment, CurrentEnvironment
13-
from .utils import ATTRIBUTE_SEPARATOR, TERM_SIGIL, add_message_and_attrs_to_store, ast_to_id
13+
from .utils import ATTRIBUTE_SEPARATOR, TERM_SIGIL, add_message_and_attrs_to_store, ast_to_id, native_to_fluent
1414

1515

1616
class FluentBundle(object):
@@ -65,12 +65,15 @@ def lookup(self, full_id):
6565
def format(self, message_id, args=None):
6666
if message_id.startswith(TERM_SIGIL):
6767
raise LookupError(message_id)
68-
if args is None:
69-
args = {}
68+
fluent_args = {}
69+
if args is not None:
70+
for argname, argvalue in args.items():
71+
fluent_args[argname] = native_to_fluent(argvalue)
72+
7073
errors = []
7174
resolve = self.lookup(message_id)
7275
env = ResolverEnvironment(context=self,
73-
current=CurrentEnvironment(args=args),
76+
current=CurrentEnvironment(args=fluent_args),
7477
errors=errors)
7578
return [resolve(env), errors]
7679

fluent.runtime/fluent/runtime/resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def __call__(self, env):
146146
return retval
147147

148148
def resolve(fluentish, env):
149-
if isinstance(fluentish, (FluentType, FluentNumber)):
149+
if isinstance(fluentish, FluentType):
150150
return fluentish.format(env.context._babel_locale)
151151
return fluentish
152152

fluent.runtime/fluent/runtime/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class NumberFormatOptions(object):
8181
maximumSignificantDigits = attr.ib(default=None)
8282

8383

84-
class FluentNumber(object):
84+
class FluentNumber(FluentType):
8585

8686
default_number_format_options = NumberFormatOptions()
8787

@@ -276,7 +276,7 @@ class DateFormatOptions(object):
276276
_SUPPORTED_DATETIME_OPTIONS = ['dateStyle', 'timeStyle', 'timeZone']
277277

278278

279-
class FluentDateType(object):
279+
class FluentDateType(FluentType):
280280
# We need to match signature of `__init__` and `__new__` due to the way
281281
# some Python implementation (e.g. PyPy) implement some methods.
282282
# So we leave those alone, and implement another `_init_options`

fluent.runtime/fluent/runtime/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
from datetime import date, datetime
4+
from decimal import Decimal
5+
16
from fluent.syntax.ast import AttributeExpression, Term, TermReference
27

8+
from .types import FluentInt, FluentFloat, FluentDecimal, FluentDate, FluentDateTime
39
from .errors import FluentReferenceError
410

511
TERM_SIGIL = '-'
@@ -37,6 +43,23 @@ def numeric_to_native(val):
3743
return int(val)
3844

3945

46+
def native_to_fluent(val):
47+
"""
48+
Convert a python type to a Fluent Type.
49+
"""
50+
if isinstance(val, int):
51+
return FluentInt(val)
52+
if isinstance(val, float):
53+
return FluentFloat(val)
54+
if isinstance(val, Decimal):
55+
return FluentDecimal(val)
56+
57+
if isinstance(val, date):
58+
return FluentDate.from_date(val)
59+
if isinstance(val, datetime):
60+
return FluentDateTime.from_date(val)
61+
return val
62+
4063
def reference_to_id(ref):
4164
"""
4265
Returns a string reference for a MessageReference, TermReference or AttributeExpression

0 commit comments

Comments
 (0)