boardpack / pydantic-i18n

pydantic-i18n is an extension to support an i18n for the pydantic error messages.
https://pydantic-i18n.boardpack.org
MIT License
82 stars 12 forks source link

Can you add encoding option? #251

Closed doctorgu closed 4 months ago

doctorgu commented 4 months ago

Is your feature request related to a problem? Please describe. Yes, it raise "UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 46: illegal multibyte sequence" error

When I add ko_KR.json with Korean character, it shows following error when use.

  File "C:\source\py_test\test_i18n.py", line 18, in <module>
    d = get_translations("./translations", "ko_KR")
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\source\py_test\test_i18n.py", line 13, in get_translations
    data: Dict[str, str] = json.load(fp)
                           ^^^^^^^^^^^^^
  File "C:\Users\gugu.park\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
                 ^^^^^^^^^
UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 46: illegal multibyte sequence
PS C:\source\py_test> python test_i18n.py

Describe the solution you'd like

I want option for encoding when reading json file in get_translations function in loaders.py file

# raise error when Korean character
with open(os.path.join(self.directory, f"{locale}.json")) as fp:
->
# No error when Korean character, so I want "utf-8" as option
with open(os.path.join(self.directory, f"{locale}.json"), encoding="utf-8") as fp:

Additional context

Add any other context or screenshots about the feature request here.

dukkee commented 4 months ago

Hi @doctorgu Thank you for your report. Yes, sure, I will work on it today or tomorrow

dukkee commented 4 months ago

@doctorgu Please install and try a new version 0.4.4