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

Setting locale based on browser language #87

Closed
Boscop opened this issue Jan 12, 2024 · 5 comments
Closed

Setting locale based on browser language #87

Boscop opened this issue Jan 12, 2024 · 5 comments

Comments

@Boscop
Copy link

Boscop commented Jan 12, 2024

It would be nice to have a helper function to set the locale based on the browser language if it's not set already.

@Baptistemontan
Copy link
Owner

What do you mean by that? A browser will send a weighted list of supported languages with the request, by default the server look at that list and choose the locale based on that, the only moment it does not do that is when a different locale is set on the client with the cookie feature

@Boscop
Copy link
Author

Boscop commented Jan 21, 2024

@Baptistemontan I mean like this i18next plugin: https://www.npmjs.com/package/i18next-browser-languagedetector
It sets the UI language based on the app's language setting / the browser language.

@Baptistemontan
Copy link
Owner

I'm working on v0.3 and want to better understand what you are asking. At the moment the locale is decided in those steps:

  1. look at the cookies, if the locale has been set then use this one
  2. look at the request Accept-Language header and check if one locale match
  3. use the default locale

I know that for now the implementation of step 2 is really naive, I'll look into it to have a more robust implementation.

Could you give me a list of what detection methods you would like to get added ?

@nag763
Copy link

nag763 commented May 31, 2024

Hello @Baptistemontan !
Joining this discussion as it would really bit useful on my project .
Actually I believe it is quite easy, but maybe the i18n_provide_context would be to be modified a bit in order to dismiss this feature in case it is not wished.
Basicly the way I implemented it on my project, as a workaround, see here if needed, is the following way :

   let Some(navigator) = leptos_use::use_window().navigator() else {
        return crate::i18n::Locale::default();
    };
    let locales: Vec<String> = navigator
        .languages()
        .into_iter()
        .filter_map(|val| val.as_string())
        .collect();
    crate::18n::Locale::find_locale(&locales)

Which is pretty handful as it allows me to have my website translated given the browser's language, and if none is found, the default is used, which is the behavior I would expect on most websites.

Especially when it comes to CSR, it makes the UX much better.

Regarding SSR, I must tell that I never used it, but ig as you mentioned, using the Accept-Language header by default would be the way. If none of the language is found in the locales, just return the default one's translations.

Let me know if I can help on this ! It would make this crate even more useful for everyone 😄
Thanks a lot!

@Baptistemontan
Copy link
Owner

I completly forgot about this issue, I could'nt really understand what was the problem because with ssr it worked, I did'nt realised the problem was with csr. I just hit that problem trying to implement end to end testing for the examples for a more robust CI and I basically came up with the same implementation as @nag763. The fix is with #108, I have some small things to finish up before releasing a new version, but it will come soon. For the time being I'm closing this issue, sorry for the wait it's been a rough 6 months for me

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

3 participants