Baptistemontan / leptos_i18n

I18n library for leptos focused on ease of use and correctness
https://baptistemontan.github.io/leptos_i18n/
MIT License
101 stars 9 forks source link

Setting locale based on browser language #87

Closed Boscop closed 3 months ago

Boscop commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

@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 commented 9 months ago

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 commented 5 months ago

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 :smile: Thanks a lot!

Baptistemontan commented 3 months ago

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