14
14
from contextlib import contextmanager
15
15
from typing import List , Callable , Optional
16
16
17
+ from babel .support import Translations , NullTranslations
17
18
from flask import current_app , g
18
19
from flask .helpers import locked_cached_property
19
20
from babel import dates , numbers , support , Locale
@@ -236,40 +237,41 @@ def _resolve_directories(directories: List[str], app=None):
236
237
yield os .path .join (app .root_path , path )
237
238
238
239
239
- def get_translations ():
240
+ def get_translations () -> Translations | NullTranslations :
240
241
"""Returns the correct gettext translations that should be used for
241
242
this request. This will never fail and return a dummy translation
242
243
object if used outside the request or if a translation cannot be found.
243
244
"""
244
245
return get_domain ().get_translations ()
245
246
246
247
247
- def get_locale ():
248
+ def get_locale () -> Optional [ Locale ] :
248
249
"""Returns the locale that should be used for this request as
249
250
`babel.Locale` object. This returns `None` if used outside a request.
250
251
"""
251
252
ctx = _get_current_context ()
252
253
if ctx is None :
253
254
return None
255
+
254
256
locale = getattr (ctx , 'babel_locale' , None )
255
257
if locale is None :
256
258
babel = get_babel ()
257
259
if babel .locale_selector is None :
258
- locale = babel .default_locale
260
+ locale = babel .instance . default_locale
259
261
else :
260
262
rv = babel .locale_selector ()
261
263
if rv is None :
262
- locale = babel .default_locale
264
+ locale = babel .instance . default_locale
263
265
else :
264
266
locale = Locale .parse (rv )
265
267
ctx .babel_locale = locale
268
+
266
269
return locale
267
270
268
271
269
- def get_timezone ():
272
+ def get_timezone () -> Optional [ timezone ] :
270
273
"""Returns the timezone that should be used for this request as
271
- `pytz.timezone` object. This returns `None` if used outside of
272
- a request.
274
+ a `pytz.timezone` object. This returns `None` if used outside a request.
273
275
"""
274
276
ctx = _get_current_context ()
275
277
tzinfo = getattr (ctx , 'babel_tzinfo' , None )
@@ -345,7 +347,7 @@ def force_locale(locale):
345
347
setattr (ctx , key , value )
346
348
347
349
348
- def _get_format (key , format ):
350
+ def _get_format (key , format ) -> Optional [ str ] :
349
351
"""A small helper for the datetime formatting functions. Looks up
350
352
format defaults for different kinds.
351
353
"""
@@ -469,7 +471,7 @@ def _date_format(formatter, obj, format, rebase, **extra):
469
471
return formatter (obj , format , locale = locale , ** extra )
470
472
471
473
472
- def format_number (number ):
474
+ def format_number (number ) -> str :
473
475
"""Return the given number formatted for the locale in request
474
476
475
477
:param number: the number to format
@@ -480,7 +482,7 @@ def format_number(number):
480
482
return numbers .format_decimal (number , locale = locale )
481
483
482
484
483
- def format_decimal (number , format = None ):
485
+ def format_decimal (number , format = None ) -> str :
484
486
"""Return the given decimal number formatted for the locale in the request.
485
487
486
488
:param number: the number to format
@@ -493,7 +495,7 @@ def format_decimal(number, format=None):
493
495
494
496
495
497
def format_currency (number , currency , format = None , currency_digits = True ,
496
- format_type = 'standard' ):
498
+ format_type = 'standard' ) -> str :
497
499
"""Return the given number formatted for the locale in the request.
498
500
499
501
:param number: the number to format
@@ -517,7 +519,7 @@ def format_currency(number, currency, format=None, currency_digits=True,
517
519
)
518
520
519
521
520
- def format_percent (number , format = None ):
522
+ def format_percent (number , format = None ) -> str :
521
523
"""Return formatted percent value for the locale in the request.
522
524
523
525
:param number: the number to format
@@ -529,7 +531,7 @@ def format_percent(number, format=None):
529
531
return numbers .format_percent (number , format = format , locale = locale )
530
532
531
533
532
- def format_scientific (number , format = None ):
534
+ def format_scientific (number , format = None ) -> str :
533
535
"""Return value formatted in scientific notation for the locale in request
534
536
535
537
:param number: the number to format
@@ -723,17 +725,17 @@ def lazy_pgettext(self, context, string, **variables):
723
725
return LazyString (self .pgettext , context , string , ** variables )
724
726
725
727
726
- def _get_current_context ():
728
+ def _get_current_context () -> Optional [ SimpleNamespace ] :
727
729
if not g :
728
730
return None
729
731
730
732
if not hasattr (g , "_flask_babel" ):
731
733
g ._flask_babel = SimpleNamespace ()
732
734
733
- return g ._flask_babel
735
+ return g ._flask_babel # noqa
734
736
735
737
736
- def get_domain ():
738
+ def get_domain () -> Domain :
737
739
ctx = _get_current_context ()
738
740
if ctx is None :
739
741
# this will use NullTranslations
@@ -749,36 +751,36 @@ def get_domain():
749
751
750
752
751
753
# Create shortcuts for the default Flask domain
752
- def gettext (* args , ** kwargs ):
754
+ def gettext (* args , ** kwargs ) -> str :
753
755
return get_domain ().gettext (* args , ** kwargs )
754
756
755
757
756
758
_ = gettext
757
759
758
760
759
- def ngettext (* args , ** kwargs ):
761
+ def ngettext (* args , ** kwargs ) -> str :
760
762
return get_domain ().ngettext (* args , ** kwargs )
761
763
762
764
763
- def pgettext (* args , ** kwargs ):
765
+ def pgettext (* args , ** kwargs ) -> str :
764
766
return get_domain ().pgettext (* args , ** kwargs )
765
767
766
768
767
- def npgettext (* args , ** kwargs ):
769
+ def npgettext (* args , ** kwargs ) -> str :
768
770
return get_domain ().npgettext (* args , ** kwargs )
769
771
770
772
771
- def lazy_gettext (* args , ** kwargs ):
773
+ def lazy_gettext (* args , ** kwargs ) -> LazyString :
772
774
return LazyString (gettext , * args , ** kwargs )
773
775
774
776
775
- def lazy_pgettext (* args , ** kwargs ):
777
+ def lazy_pgettext (* args , ** kwargs ) -> LazyString :
776
778
return LazyString (pgettext , * args , ** kwargs )
777
779
778
780
779
- def lazy_ngettext (* args , ** kwargs ):
781
+ def lazy_ngettext (* args , ** kwargs ) -> LazyString :
780
782
return LazyString (ngettext , * args , ** kwargs )
781
783
782
784
783
- def lazy_npgettext (* args , ** kwargs ):
785
+ def lazy_npgettext (* args , ** kwargs ) -> LazyString :
784
786
return LazyString (npgettext , * args , ** kwargs )
0 commit comments