1111from __future__ import absolute_import
1212import os
1313
14- # this is a workaround for a snow leopard bug that babel does not
15- # work around :)
16- if os .environ .get ('LC_CTYPE' , '' ).lower () == 'utf-8' :
17- os .environ ['LC_CTYPE' ] = 'en_US.utf-8'
18-
1914from datetime import datetime
2015from contextlib import contextmanager
2116from flask import current_app , request
3025 timezone = pytz .timezone
3126 UTC = pytz .UTC
3227
33- from flask_babel ._compat import string_types , text_type
28+ from flask_babel ._compat import string_types
29+ from flask_babel .speaklater import LazyString
3430
3531
3632class Babel (object ):
@@ -128,7 +124,7 @@ def localeselector(self, f):
128124 time. If `None` is returned, the locale falls back to the one from
129125 the configuration.
130126
131- This has to return the locale as string (eg: ``'de_AT'``, ''`en_US`'' )
127+ This has to return the locale as string (eg: ``'de_AT'``, ``'en_US'`` )
132128 """
133129 assert self .locale_selector_func is None , \
134130 'a localeselector function is already registered'
@@ -217,19 +213,27 @@ def get_translations():
217213 """
218214 ctx = _get_current_context ()
219215
216+ if ctx is None :
217+ return support .NullTranslations ()
218+
220219 translations = getattr (ctx , 'babel_translations' , None )
221220 if translations is None :
222221 translations = support .Translations ()
223222
224223 babel = current_app .extensions ['babel' ]
225224 for dirname in babel .translation_directories :
226- translations .merge (
227- support .Translations .load (
225+ catalog = support .Translations .load (
228226 dirname ,
229227 [get_locale ()],
230228 babel .domain
231229 )
232- )
230+ translations .merge (catalog )
231+ # FIXME: Workaround for merge() being really, really stupid. It
232+ # does not copy _info, plural(), or any other instance variables
233+ # populated by GNUTranslations. We probably want to stop using
234+ # `support.Translations.merge` entirely.
235+ if hasattr (catalog , 'plural' ):
236+ translations .plural = catalog .plural
233237
234238 ctx .babel_translations = translations
235239
@@ -437,10 +441,9 @@ def format_time(time=None, format=None, rebase=True):
437441
438442
439443def format_timedelta (datetime_or_timedelta , granularity = 'second' ,
440- add_direction = False ):
444+ add_direction = False , threshold = 0.85 ):
441445 """Format the elapsed time from the given date to now or the given
442- timedelta. This currently requires an unreleased development
443- version of Babel.
446+ timedelta.
444447
445448 This function is also available in the template context as filter
446449 named `timedeltaformat`.
@@ -450,6 +453,7 @@ def format_timedelta(datetime_or_timedelta, granularity='second',
450453 return dates .format_timedelta (
451454 datetime_or_timedelta ,
452455 granularity ,
456+ threshold = threshold ,
453457 add_direction = add_direction ,
454458 locale = get_locale ()
455459 )
@@ -601,21 +605,6 @@ def npgettext(context, singular, plural, num, **variables):
601605 return s if not variables else s % variables
602606
603607
604- def make_json_lazy_string (func , * args , ** kwargs ):
605- """Like :method:`speaklater.make_lazy_string` but returns a subclass
606- that provides an :method:`__html__` method. That method is used by
607- :class:`flask.json.JSONEncoder` to serialize objects of unrecognized
608- types.
609- """
610- from speaklater import _LazyString
611-
612- class JsonLazyString (_LazyString ):
613- def __html__ (self ):
614- return text_type (self )
615-
616- return JsonLazyString (func , args , kwargs )
617-
618-
619608def lazy_gettext (string , ** variables ):
620609 """Like :func:`gettext` but the string returned is lazy which means
621610 it will be translated when it is used as an actual string.
@@ -628,7 +617,7 @@ def lazy_gettext(string, **variables):
628617 def index():
629618 return unicode(hello)
630619 """
631- return make_json_lazy_string (gettext , string , ** variables )
620+ return LazyString (gettext , string , ** variables )
632621
633622
634623def lazy_pgettext (context , string , ** variables ):
@@ -637,7 +626,7 @@ def lazy_pgettext(context, string, **variables):
637626
638627 .. versionadded:: 0.7
639628 """
640- return make_json_lazy_string (pgettext , context , string , ** variables )
629+ return LazyString (pgettext , context , string , ** variables )
641630
642631
643632def _get_current_context ():
0 commit comments