Skip to content

Commit a540993

Browse files
committed
Initial implementation of MessageContext
1 parent b17dde4 commit a540993

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

fluent/context.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
import six
4+
5+
from .syntax import FluentParser
6+
from .syntax.ast import Message, Term
7+
8+
9+
class MessageContext(object):
10+
"""
11+
Message contexts are single-language stores of translations. They are
12+
responsible for parsing translation resources in the Fluent syntax and can
13+
format translation units (entities) to strings.
14+
15+
Always use `MessageContext.format` to retrieve translation units from
16+
a context. Translations can contain references to other entities or
17+
external arguments, conditional logic in form of select expressions, traits
18+
which describe their grammatical features, and can use Fluent builtins.
19+
See the documentation of the Fluent syntax for more information.
20+
"""
21+
22+
def __init__(self, locales, functions=None, use_isolating=True):
23+
self.locales = locales
24+
self._functions = functions or {}
25+
self._use_isolating = use_isolating
26+
self._messages = {}
27+
self._terms = {}
28+
29+
def add_messages(self, source):
30+
parser = FluentParser()
31+
resource = parser.parse(source)
32+
# TODO - warn if items get overwritten
33+
for item in resource.body:
34+
if isinstance(item, Message):
35+
self._messages[item.id.name] = item
36+
elif isinstance(item, Term):
37+
self._terms[item.id.name] = item
38+
39+
def has_message(self, message_id):
40+
return message_id in self._messages
41+
42+
def messages(self):
43+
"""
44+
Returns iterable of (id, message) for the messages in this context
45+
"""
46+
return six.iteritems(self._messages)

tests/test_context.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
import unittest
4+
5+
from fluent.context import MessageContext
6+
7+
from tests.syntax import dedent_ftl
8+
9+
10+
class TestMessageContext(unittest.TestCase):
11+
def setUp(self):
12+
self.ctx = MessageContext(['en-US'])
13+
14+
def test_add_messages(self):
15+
self.ctx.add_messages(dedent_ftl("""
16+
foo = Foo
17+
bar = Bar
18+
-baz = Baz
19+
"""))
20+
self.assertIn('foo', self.ctx._messages)
21+
self.assertIn('bar', self.ctx._messages)
22+
self.assertNotIn('-baz', self.ctx._messages)
23+
self.assertIn('-baz', self.ctx._terms)
24+
25+
def test_messages(self):
26+
self.ctx.add_messages(dedent_ftl("""
27+
foo = Foo
28+
bar = Bar
29+
-baz = Baz
30+
"""))
31+
self.assertEqual(sorted([i for i, m in self.ctx.messages()]),
32+
['bar', 'foo'])
33+
34+
def test_has_message(self):
35+
self.ctx.add_messages(dedent_ftl("""
36+
foo = Foo
37+
"""))
38+
39+
self.assertTrue(self.ctx.has_message('foo'))
40+
self.assertFalse(self.ctx.has_message('bar'))

0 commit comments

Comments
 (0)