Anbarryprojects / fastapi-babel

FastAPI babel support pybable tools like translation of text, formatting of dates, times and numbers, and time zones.
MIT License
46 stars 14 forks source link

Language code mismatch causes failure to load translations, because separator in language code is '-' #24

Closed ruiheng closed 4 months ago

ruiheng commented 6 months ago

Event using the lastest commit '47018bc', My FastAPI app still does not work.

My browser sends the following Accept-Languages header:

zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2

After processing by the fastapi-babel middleware, the preferred languages list contains codes like "zh-CN". However, the language subdirectories under the lang directory use underscores instead of dashes, e.g. "zh_CN".

This causes a failure to locate the proper MO files for translations.

The lang directory structure is created by the fastapi-babel command line tool, which only accepts language codes with underscores like "zh_CN". Codes with dashes like "zh-CN" cause errors in the babel library, which uses underscores by default.

My suggested fix is to simply replace dashes with underscores in the language codes, as shown in this patch:

--- middleware.py   2023-12-21 09:32:34.000000000 +0800
+++ middleware.py   2023-12-21 09:33:07.000000000 +0800
@@ -38,6 +38,7 @@
             return self.babel.config.BABEL_DEFAULT_LOCALE
         languages = re.findall(r"([a-z]{2}-[A-Z]{2}|[a-z]{2})(;q=\d.\d{1,3})?", lang_code)
         languages = sorted(languages, key=lambda x: x[1], reverse=True) # sort the priority, no priority comes last
+        languages = map(lambda x: (x[0].replace('-', '_'), x[1]), languages)
         translation_directory = Path(self.babel.config.BABEL_TRANSLATION_DIRECTORY)
         translation_files = [i.name for i in translation_directory.iterdir()]
         explicit_priority = None
Legopapurida commented 6 months ago

Hello dude we have tried this method, thanks for announcing us due this problem, we will be glad if you can fix this code and contribute to the project.

AnderUstarroz commented 4 months ago

@ruiheng is right, this needs to be resolved or language detection will be kind of useless otherwise.

I have fixed it and sent a Pull request https://github.com/Anbarryprojects/fastapi-babel/pull/26 with a performant solution.

Please @Legopapurida could you take few minutes to take a look and create the new release v0.0.9? The old v0.0.8 crashes whenever the requested language doesn't exist instead of using a default language. There is no point of having a broken release when we already have that fixed.

Thanks,

AnderUstarroz commented 4 months ago

Thanks for merging the PR @Legopapurida !

Any chance to update the pip package to v0.0.9? Then you could close the following issues:

peterschwarzdev commented 4 months ago

Before updating the pip package to v0.0.9 I think you should also merge this Pull request https://github.com/Anbarryprojects/fastapi-babel/pull/29 @Legopapurida ! It adds a huge improvement, I am using his branch in production and works great, but would be better for everyone having it released as an official PIP version.

Legopapurida commented 4 months ago

Hello everybody I apologize for the long gap in checking the issues and pull requests. I was so busy in these days.

I will check the issues and pull requests as soon as possible and this will be released to new version (v1)

Legopapurida commented 4 months ago

the problem has been fixed by @AnderUstarroz and a million thanks to @AnderUstarroz

Legopapurida commented 4 months ago

Thanks all