Skip to content

Commit

Permalink
(fix) Coerce language code on filename lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Fievet committed Apr 20, 2024
1 parent 9b83a8f commit a10eacc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/statici18n/management/commands/compilejsi18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ def _create_json_catalog(self, locale, domain, packages):
def _create_output(
self, outputdir, outputformat, locale, domain, packages, namespace
):
outputfile = os.path.join(outputdir, get_filename(locale, domain, outputformat))
try:
filename = get_filename(locale, domain, outputformat)
except LookupError:
# Silence error for backward-compatibility
return ""

outputfile = os.path.join(outputdir, filename)
basedir = os.path.dirname(outputfile)
if not os.path.isdir(basedir):
os.makedirs(basedir)
Expand Down
12 changes: 7 additions & 5 deletions src/statici18n/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
from collections.abc import Sequence
from importlib import import_module
import os

from django.utils.translation import get_supported_language_variant
from django.utils.translation.trans_real import to_language

from statici18n.conf import settings

Expand All @@ -14,7 +17,7 @@ def get_mod_func(callback):
dot = callback.rindex(".")
except ValueError:
return callback, ""
return callback[:dot], callback[dot + 1 :]
return callback[:dot], callback[dot + 1:]


def get_filename(*args, **kwargs):
Expand All @@ -29,13 +32,12 @@ def get_filename(*args, **kwargs):
return _filename_func(*args, **kwargs)


def default_filename(language_code, domain, output_format="js"):
def default_filename(locale, domain, output_format="js"):
language_code = get_supported_language_variant(locale)
return os.path.join(language_code, "%s.%s" % (domain, output_format))


def legacy_filename(locale, domain, output_format="js"):
from django.utils.translation.trans_real import to_language

return os.path.join(to_language(locale), "%s.%s" % (domain, output_format))


Expand Down
9 changes: 8 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
from statici18n import utils


@pytest.mark.parametrize("locale", ["en", "zh-hans", "ko-KR"])
@pytest.mark.parametrize("locale", ["en", "fr", "zh-Hans", "ko-KR"])
def test_default_filename(locale):
filename = utils.get_filename(locale, "djangojs")
assert filename == "%s/djangojs.js" % locale


@pytest.mark.parametrize("locale", ["en", "en-gb"])
def test_default_filename_coerce_locale(settings, locale):
settings.LANGUAGES = [("en", "English")]

assert utils.default_filename(locale, "django") == "en/django.js"


@pytest.mark.parametrize("fmt", ["js", "json", "yaml"])
def test_default_filename_with_outputformat(fmt):
filename = utils.get_filename("en", "djangojs", fmt)
Expand Down

0 comments on commit a10eacc

Please sign in to comment.