Skip to content

Commit

Permalink
fix funcs without positional args (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
solaluset authored May 6, 2024
1 parent d4630f3 commit 19de827
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 5 additions & 1 deletion i18n/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ def __getitem__(self, key: str) -> Any:
if args:
f = get_function(name, self.locale)
if f:
arg_list = args.strip(")").split(config.get("argument_delimiter"))
args = args.strip(")")
if args:
arg_list = args.split(config.get("argument_delimiter"))
else:
arg_list = []
try:
return f(*arg_list, **self.kwargs)
except KeyError as e:
Expand Down
21 changes: 19 additions & 2 deletions i18n/tests/translation_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def setUpClass(cls):
translations.add('foo.custom_func', '%{count} day%{p(|s)}')
translations.add('foo.inexistent_func', '%{a(b|c)}')
translations.add('foo.comma_separated_args', '%{f(1,2,3)}')
translations.add(
"foo.no_args_func",
"%count1 apples, %count2 bananas, %total() fruits total",
)

custom_functions.add_function('p', lambda *a, **kw: a[kw['count'] != 1])

Expand Down Expand Up @@ -210,6 +214,17 @@ def test_inexistent_function(self):
with self.assertRaises(KeyError):
t('foo.inexistent_func')

def test_function_without_args(self):
custom_functions.add_function(
"total",
lambda **kw: kw["count1"] + kw["count2"],
config.get("locale"),
)
self.assertEqual(
t("foo.no_args_func", count1=7, count2=3),
"7 apples, 3 bananas, 10 fruits total",
)

def test_bad_locale_func(self):
custom_functions.add_function(
"p",
Expand All @@ -226,8 +241,10 @@ def test_argument_delimiter_change(self):
lambda *a, **kw: a[kw["value"] - 1],
config.get("locale"),
)
self.assertEqual(t('foo.comma_separated_args', value=1), '1')
config.set('argument_delimiter', '|')
try:
self.assertEqual(t("foo.comma_separated_args", value=1), "1")
finally:
config.set("argument_delimiter", "|")

def test_placeholder_delimiter_change(self):
config.set('placeholder_delimiter', '$')
Expand Down

0 comments on commit 19de827

Please sign in to comment.