Handle invalid locale
This commit is contained in:
parent
d8b4724b6d
commit
47a5064609
|
|
@ -5,10 +5,12 @@ import string
|
|||
from typing import Optional
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils import translation
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from babel import Locale
|
||||
from babel.core import UnknownLocaleError
|
||||
from babel.numbers import parse_pattern
|
||||
from djmoney.money import Money
|
||||
|
||||
|
|
@ -205,7 +207,10 @@ def format_money(
|
|||
ValueError: format string is incorrectly specified
|
||||
"""
|
||||
language = locale or settings.LANGUAGE_CODE
|
||||
locale = Locale.parse(translation.to_locale(language))
|
||||
try:
|
||||
locale = Locale.parse(translation.to_locale(language))
|
||||
except (UnknownLocaleError, ValueError) as e:
|
||||
raise ValidationError(f"format_money: Invalid locale '{language}' - {e}")
|
||||
|
||||
if fmt:
|
||||
pattern = parse_pattern(fmt)
|
||||
|
|
|
|||
|
|
@ -571,6 +571,10 @@ class ReportTagTest(PartImageTestMixin, InvenTreeTestCase):
|
|||
report_tags.render_currency(m, locale='en-us'), '$1,234.56'
|
||||
)
|
||||
|
||||
# Invalid locale raises ValidationError
|
||||
with self.assertRaises(ValidationError):
|
||||
report_tags.render_currency(m, locale='xx-zz')
|
||||
|
||||
def test_create_currency(self):
|
||||
"""Test the create_currency template tag."""
|
||||
m = report_tags.create_currency(1000, 'USD')
|
||||
|
|
|
|||
Loading…
Reference in New Issue