Skip to content

Commit

Permalink
Merge pull request #333 from aveuiller/332_define_default_get_locale
Browse files Browse the repository at this point in the history
fix(get_locale): set default locale to 'en' without Flask-Babel
  • Loading branch information
kvesteri committed Dec 27, 2018
2 parents 717432b + c863854 commit faec07a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
10 changes: 6 additions & 4 deletions sqlalchemy_utils/i18n.py
Expand Up @@ -14,13 +14,15 @@
except ImportError:
babel = None


try:
from flask_babel import get_locale
except ImportError:
def get_locale(): return babel.Locale('en')
except AttributeError:
# As babel is optional, we may raise an AttributeError accessing it
def get_locale():
raise ImproperlyConfigured(
'Could not load get_locale function from Flask-Babel. Either '
'install Flask-Babel or make a similar function and override it '
'Could not load get_locale function using Babel. Either '
'install Babel or make a similar function and override it '
'in this module.'
)

Expand Down
6 changes: 5 additions & 1 deletion sqlalchemy_utils/primitives/country.py
Expand Up @@ -25,7 +25,8 @@ class Country(object):
Country(Country('FI')).code # 'FI'
Country always validates the given code.
Country always validates the given code if you use at least the optional
dependency list 'babel', otherwise no validation are performed.
::
Expand Down Expand Up @@ -76,6 +77,9 @@ def validate(self, code):
raise ValueError(
'Could not convert string to country code: {0}'.format(code)
)
except AttributeError:
# As babel is optional, we may raise an AttributeError accessing it
pass

def __eq__(self, other):
if isinstance(other, Country):
Expand Down
6 changes: 5 additions & 1 deletion sqlalchemy_utils/primitives/currency.py
Expand Up @@ -26,7 +26,8 @@ class Currency(object):
Currency(Currency('USD')).code # 'USD'
Currency always validates the given code.
Currency always validates the given code if you use at least the optional
dependency list 'babel', otherwise no validation are performed.
::
Expand Down Expand Up @@ -75,6 +76,9 @@ def validate(self, code):
i18n.babel.Locale('en').currencies[code]
except KeyError:
raise ValueError("'{0}' is not valid currency code.".format(code))
except AttributeError:
# As babel is optional, we may raise an AttributeError accessing it
pass

@property
def symbol(self):
Expand Down

0 comments on commit faec07a

Please sign in to comment.