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

Add an extractor for the user language #2197

Open
1 task done
frenetisch-applaudierend opened this issue Aug 27, 2023 · 2 comments
Open
1 task done

Add an extractor for the user language #2197

frenetisch-applaudierend opened this issue Aug 27, 2023 · 2 comments
Labels
A-axum-extra C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@frenetisch-applaudierend
  • I have looked for existing issues (including closed) about this

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 the Accept-Language header, a :lang path segment and a lang 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.

@davidpdrsn
Copy link
Member

davidpdrsn commented Aug 27, 2023

Sounds like a good fit for axum-extra. Then we can move it to axum when it’s matured.

@davidpdrsn davidpdrsn added C-feature-request Category: A feature request, i.e: not implemented / a PR. A-axum-extra labels Aug 27, 2023
@frenetisch-applaudierend
Copy link
Author

Great, I'll take a stab at it then! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum-extra C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

2 participants