8
8
from fluent .syntax .ast import Message , Term
9
9
10
10
from .builtins import BUILTINS
11
- from .resolver import resolve
12
- from .utils import ATTRIBUTE_SEPARATOR , TERM_SIGIL , add_message_and_attrs_to_store , ast_to_id
11
+ from .prepare import Compiler
12
+ from .resolver import ResolverEnvironment , CurrentEnvironment
13
+ from .utils import ATTRIBUTE_SEPARATOR , TERM_SIGIL , ast_to_id , native_to_fluent
13
14
14
15
15
16
class FluentBundle (object ):
@@ -33,6 +34,8 @@ def __init__(self, locales, functions=None, use_isolating=True):
33
34
self ._functions = _functions
34
35
self ._use_isolating = use_isolating
35
36
self ._messages_and_terms = {}
37
+ self ._compiled = {}
38
+ self ._compiler = Compiler (use_isolating = use_isolating )
36
39
self ._babel_locale = self ._get_babel_locale ()
37
40
self ._plural_form = babel .plural .to_python (self ._babel_locale .plural_form )
38
41
@@ -44,22 +47,41 @@ def add_messages(self, source):
44
47
if isinstance (item , (Message , Term )):
45
48
full_id = ast_to_id (item )
46
49
if full_id not in self ._messages_and_terms :
47
- # We add attributes to the store to enable faster looker
48
- # later, and more direct code in some instances.
49
- add_message_and_attrs_to_store (self ._messages_and_terms , full_id , item )
50
+ self ._messages_and_terms [full_id ] = item
50
51
51
52
def has_message (self , message_id ):
52
53
if message_id .startswith (TERM_SIGIL ) or ATTRIBUTE_SEPARATOR in message_id :
53
54
return False
54
55
return message_id in self ._messages_and_terms
55
56
57
+ def lookup (self , full_id ):
58
+ if full_id not in self ._compiled :
59
+ entry_id = full_id .split (ATTRIBUTE_SEPARATOR , 1 )[0 ]
60
+ entry = self ._messages_and_terms [entry_id ]
61
+ compiled = self ._compiler (entry )
62
+ if compiled .value is not None :
63
+ self ._compiled [entry_id ] = compiled .value
64
+ for attr in compiled .attributes :
65
+ self ._compiled [ATTRIBUTE_SEPARATOR .join ([entry_id , attr .id .name ])] = attr .value
66
+ return self ._compiled [full_id ]
67
+
56
68
def format (self , message_id , args = None ):
57
69
if message_id .startswith (TERM_SIGIL ):
58
70
raise LookupError (message_id )
59
- message = self ._messages_and_terms [message_id ]
60
- if args is None :
61
- args = {}
62
- return resolve (self , message , args )
71
+ if args is not None :
72
+ fluent_args = {
73
+ argname : native_to_fluent (argvalue )
74
+ for argname , argvalue in args .items ()
75
+ }
76
+ else :
77
+ fluent_args = {}
78
+
79
+ errors = []
80
+ resolve = self .lookup (message_id )
81
+ env = ResolverEnvironment (context = self ,
82
+ current = CurrentEnvironment (args = fluent_args ),
83
+ errors = errors )
84
+ return [resolve (env ), errors ]
63
85
64
86
def _get_babel_locale (self ):
65
87
for l in self .locales :
0 commit comments