danhper / python-i18n

Easy to use i18n library for Python
https://pypi.python.org/pypi/python-i18n
MIT License
228 stars 45 forks source link

YAML arrays not supported #22

Open ZRunner opened 4 years ago

ZRunner commented 4 years ago

Hello! Either I didn't understand something, or it looks like that package doesn't support translations arrays. I tried to put an array in my yaml file, which is correctly parsed and all, but it looks like trying to get it with i18n.t() raises a nice TypeError, when trying to format a string-which-is-actually-a-list.

Here's the YAML content I put :

fr:
  time:
    months:
      - janvier
      - février
      - mars
      - avril
      - mai
      - juin
      - juillet
      - août
      - septembre
      - octobre
      - novembre
      - décembre

And the error output:

Traceback (most recent call last):
  File "/Users/.../languages.py", line 32, in tr
    return i18n.t(key, locale=lang)
  File "/usr/local/lib/python3.8/site-packages/i18n/translator.py", line 28, in t
    return translate(key, locale=locale, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/i18n/translator.py", line 44, in translate
    return TranslationFormatter(translation).format(**kwargs)
  File "/usr/local/lib/python3.8/site-packages/i18n/translator.py", line 18, in format
    return self.safe_substitute(**kwargs)
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/string.py", line 147, in safe_substitute
    return self.pattern.sub(convert, self.template)
TypeError: expected string or bytes-like object

I guess detecting the variable type before trying to apply a substitution would be enough to fix it, but to be honest I didn't really look at the internal code, so I let you handle that part. EDIT: Just created a PR for you, let me know if I did something wrong!

Python version: Python 3.8.6 Package version: 0.3.9