boardpack/pydantic-i18n

Wrong encoding with Russian letters using JsonLoader

Eclipsium opened this issue · 4 comments

Wrong encoding with Russian letters using JsonLoader

image

File have utf-8 encoding

image

I created my own loader, where I corrected the loading of a non-existent default key, and hardcoded the encoding, but I think it's worth adding it to init method. Maybe it will be useful

    def gettext(self, key: str, locale: str) -> str:
        if locale not in self.locales:
            raise ValueError(f"Locale '{locale}' wasn't found.")

        data = self.get_translations(locale)
        if not data.get(key):
            return key
        return data.get(key)

    def get_translations(self, locale: str) -> Mapping[str, str]:
        with open(os.path.join(self.directory, f"{locale}.json"),
                  encoding='utf-8') as fp:
            data: Dict[str, str] = json.load(fp)

        return data

Hello @Eclipsium, thank you very much for your issue.
I checked the Russian locale example now on my machine and it works okay + if you check your message content you will see the strange prefix body -> files .... Could you provide a full example, please?

Regarding current key returning in the case when there isn't such key in the translations, it makes sense and will be added, thanks

@Eclipsium as you don't show a full example, I close this issue. When you get time, then please reopen