-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of MessageContext
- Loading branch information
1 parent
b17dde4
commit a540993
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
import six | ||
|
||
from .syntax import FluentParser | ||
from .syntax.ast import Message, Term | ||
|
||
|
||
class MessageContext(object): | ||
""" | ||
Message contexts are single-language stores of translations. They are | ||
responsible for parsing translation resources in the Fluent syntax and can | ||
format translation units (entities) to strings. | ||
Always use `MessageContext.format` to retrieve translation units from | ||
a context. Translations can contain references to other entities or | ||
external arguments, conditional logic in form of select expressions, traits | ||
which describe their grammatical features, and can use Fluent builtins. | ||
See the documentation of the Fluent syntax for more information. | ||
""" | ||
|
||
def __init__(self, locales, functions=None, use_isolating=True): | ||
self.locales = locales | ||
self._functions = functions or {} | ||
self._use_isolating = use_isolating | ||
self._messages = {} | ||
self._terms = {} | ||
|
||
def add_messages(self, source): | ||
parser = FluentParser() | ||
resource = parser.parse(source) | ||
# TODO - warn if items get overwritten | ||
for item in resource.body: | ||
if isinstance(item, Message): | ||
self._messages[item.id.name] = item | ||
elif isinstance(item, Term): | ||
self._terms[item.id.name] = item | ||
|
||
def has_message(self, message_id): | ||
return message_id in self._messages | ||
|
||
def messages(self): | ||
""" | ||
Returns iterable of (id, message) for the messages in this context | ||
""" | ||
return six.iteritems(self._messages) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
import unittest | ||
|
||
from fluent.context import MessageContext | ||
|
||
from tests.syntax import dedent_ftl | ||
|
||
|
||
class TestMessageContext(unittest.TestCase): | ||
def setUp(self): | ||
self.ctx = MessageContext(['en-US']) | ||
|
||
def test_add_messages(self): | ||
self.ctx.add_messages(dedent_ftl(""" | ||
foo = Foo | ||
bar = Bar | ||
-baz = Baz | ||
""")) | ||
self.assertIn('foo', self.ctx._messages) | ||
self.assertIn('bar', self.ctx._messages) | ||
self.assertNotIn('-baz', self.ctx._messages) | ||
self.assertIn('-baz', self.ctx._terms) | ||
|
||
def test_messages(self): | ||
self.ctx.add_messages(dedent_ftl(""" | ||
foo = Foo | ||
bar = Bar | ||
-baz = Baz | ||
""")) | ||
self.assertEqual(sorted([i for i, m in self.ctx.messages()]), | ||
['bar', 'foo']) | ||
|
||
def test_has_message(self): | ||
self.ctx.add_messages(dedent_ftl(""" | ||
foo = Foo | ||
""")) | ||
|
||
self.assertTrue(self.ctx.has_message('foo')) | ||
self.assertFalse(self.ctx.has_message('bar')) |