Skip to content

Commit

Permalink
Implemented Bundle.format_lazy
Browse files Browse the repository at this point in the history
Not documented yet
  • Loading branch information
spookylukey committed Jun 2, 2018
1 parent fe3ec56 commit 7af8128
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/api/bundle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
select variants.



Error handling in Bundle
========================

Expand Down
10 changes: 7 additions & 3 deletions src/django_ftl/bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from collections import OrderedDict
from threading import local

import six
from django.dispatch import Signal
from django.utils import lru_cache
from django.utils.functional import cached_property
from django.utils.functional import cached_property, lazy
from fluent.context import MessageContext

from .conf import get_setting
Expand All @@ -16,6 +17,8 @@

ftl_logger = logging.getLogger('django_ftl.message_errors')

text_type = six.text_type


class NoLocaleSet(AssertionError):
pass
Expand Down Expand Up @@ -207,8 +210,7 @@ def current_message_contexts(self):
self._current_message_contexts = contexts
return contexts

def format(self, message_id, args=None,
lazy=False):
def format(self, message_id, args=None):
message_contexts = self.current_message_contexts
for i, context in enumerate(message_contexts):
try:
Expand All @@ -222,6 +224,8 @@ def format(self, message_id, args=None,
# None were successful, return default
return '???'

format_lazy = lazy(format, text_type)

def _log_error(self,
context,
message_id,
Expand Down
28 changes: 27 additions & 1 deletion tests/test_bundle.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# -*- coding-utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

import six
from django.test import TestCase, override_settings
from django.utils.encoding import force_text

from django_ftl import activate_locale, deactivate_locale
from django_ftl.bundles import Bundle, FileNotFoundError, NoLocaleSet, locale_lookups

text_type = six.text_type


class TestBundles(TestCase):

Expand Down Expand Up @@ -163,7 +167,29 @@ def test_number_formatting_for_fallback(self):

# TODO - check caches are actually working

# TODO lazy strings
def test_lazy(self):
bundle = Bundle(['tests/main.ftl'], default_locale='en')

l = bundle.format_lazy('simple')
self.assertEqual(force_text(l), 'Simple')
activate_locale('fr-FR')
self.assertEqual(force_text(l), 'Facile')
deactivate_locale()
self.assertEqual(force_text(l), 'Simple')

def test_lazy_with_require_activate(self):
bundle = Bundle(['tests/main.ftl'],
default_locale='en',
require_activate=True)
self.assertRaises(NoLocaleSet, bundle.format, 'simple')
msg = bundle.format_lazy('simple')

self.assertRaises(NoLocaleSet, force_text, msg)

activate_locale('en')
self.assertEqual(force_text(msg), 'Simple')
activate_locale('fr-FR')
self.assertEqual(force_text(msg), 'Facile')


class TestLocaleLookups(TestCase):
Expand Down

0 comments on commit 7af8128

Please sign in to comment.