Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ruiheng opened this issue Dec 21, 2023 · 7 comments

Comments

@ruiheng
Copy link

ruiheng commented Dec 21, 2023

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
Copy link
Contributor

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
Copy link
Contributor

AnderUstarroz commented Feb 8, 2024

@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 #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,

@peterschwarzdev
Copy link

Before updating the pip package to v0.0.9 I think you should also merge this Pull request #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
Copy link
Contributor

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
Copy link
Contributor

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

@Legopapurida
Copy link
Contributor

Thanks all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants