diff --git a/i18n/formatters.py b/i18n/formatters.py index e8c956a..020acf2 100644 --- a/i18n/formatters.py +++ b/i18n/formatters.py @@ -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: diff --git a/i18n/tests/translation_tests.py b/i18n/tests/translation_tests.py index d031b4b..961c29c 100644 --- a/i18n/tests/translation_tests.py +++ b/i18n/tests/translation_tests.py @@ -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]) @@ -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", @@ -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', '$')