-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add an extractor for the user language #2197
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
Comments
Sounds like a good fit for axum-extra. Then we can move it to axum when it’s matured. |
Great, I'll take a stab at it then! Thanks! |
I've read the PR from a couple of years back, did you publish anything in a separate crate or did you end up just having the code in your own app? Considering the amount of work required to get something clean working, I pretty much agree this doesn't belong to axum:
So, yeah, quite a bit of work depending on how the later part would be done - I can understand not wanting to maintain all this. Even if it's not a crate, having an example to build on would be great instead of starting over. |
Hi @martinetd |
Interesting to see this getting so much activity suddenly! I just had a quick read over #2198. I've also created my own internal implementation recently 😅 My code also only aims to address the getting the ranked list. I believe that, whether or not it's directly an axum or axum-extra, the point of this extractor should be to get just the ranked list. Then the app decides what to do with it. I also supported an override via query arg, and it looks like the PR from years past went even further with path based config too. As was already pointed out, it would be nice if this could be built on typed headers. I see there's a disabled implementation, but couldn't figure out the backstory on that, so I just asked: hyperium/headers#206. I guess the biggest question is probably which language tags are accepted. There are a bunch of crates in the space, from isolang to oxilang, to language_tags to unic-langid, and... oh my 😅 It's not clear that there is a "winner" crate here yet, and in my own testing, it seems some crates don't necessarily accept all valid identifiers. So I guess the best option for the moment is to probably accept any input that might be valid and then let the user decide if they want stricter typing on top. |
@frenetisch-applaudierend Thanks! @ianthetechie Thanks! I agree having a typed header would be the cleanest way to get this first part handled easily in axum. Happy to help as free time allows. Might also be worth spending a moment to add an axum example with it once the typed header is available if @davidpdrsn is ok with that, so we have a "blessed" way documented/tested. |
Feature Request
Add an extractor for the preferred language for a user
Motivation
It is not uncommon to want to return a response in the users language, especially when building user interfaces. While it is not hard to implement this on your own, it is nevertheless some boilerplate code that could be avoided.
Additionally, having a canonical way to get the request language would allow other extractors and middleware to rely on that extractor to get the user language, e.g. to return localized error responses.
Proposal
Add a
UserLanguage
extractor that reads from the request a list of preferred user languages and makes them available for the user. Standard sources of languages could include theAccept-Language
header, a:lang
path segment and alang
query parameter.It may make sense to make the language sources configurable as well as allowing to add custom sources, such as reading the language from a cookie.
Alternatives
This could also be implemented in a crate outside of axum. The advantage being less maintenance overhead for axum and allow for more diversity in implementations. The main drawback would be that there is no canonical way for library crates to read the preferred user language. Relying on a specific third party crate may not be desirable in such cases.
Implementation
If this feature is something that would fit into the scope of axum, I would like to contribute this in a pull request.
The text was updated successfully, but these errors were encountered: