Closed Boscop closed 3 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
@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.
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:
Accept-Language
header and check if one locale matchI 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 ?
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!
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
It would be nice to have a helper function to set the locale based on the browser language if it's not set already.