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

Wrong locale redirection #161

Closed
arnaudpoullet opened this issue Nov 8, 2024 · 8 comments · Fixed by #162
Closed

Wrong locale redirection #161

arnaudpoullet opened this issue Nov 8, 2024 · 8 comments · Fixed by #162

Comments

@arnaudpoullet
Copy link

Hi!

Thank you very much for this crate. I'll give as much context as possible.

With this being my i18n config:

watch-additional-files = ["locales"]

[package.metadata.leptos-i18n]
default = "fr"
locales = ["fr","nl","en"]
namespaces = ["common", "home"]

And this my route config:

<Routes fallback=|| "Page not found.".into_view()>
    <I18nRoute view=|| view! { <Outlet />} >
        <Route path=StaticSegment("") view=HomePage/>
    </I18nRoute>
</Routes>

When I type /nl in my browser it immediately redirects to /en while I would expect the Locale segment in the path to have priority (except when there is a cookie?)

Additional info:

  • It is not linked to cookies because removing an existing cookie (which was set by the lib to en) or the entire cookie feature doesn't change anything
  • My browser is set with as request header Accept-Language en-GB,en;q=0.5

I use the current main branch in my Cargo.toml:
leptos_i18n = { git = "https://github.com/Baptistemontan/leptos_i18n.git", rev = "b845a8c", default-features = false, features = ["nightly","track_locale_files","json_files"] }

This is set on a new project and fetching the translations works well.

@Baptistemontan
Copy link
Owner

Baptistemontan commented Nov 8, 2024

I did a very simple reproduction:

use crate::i18n::*;
use leptos::prelude::*;
use leptos_router::{components::*, StaticSegment};

#[component]
#[allow(non_snake_case)]
pub fn App() -> impl IntoView {
    leptos_meta::provide_meta_context();

    view! {
        <I18nContextProvider>
            <Router>
                <Routes fallback=|| "Page not found.".into_view()>
                    <I18nRoute view=|| view! { <Outlet />} >
                        <Route path=StaticSegment("") view=HomePage/>
                    </I18nRoute>
                </Routes>
            </Router>
        </I18nContextProvider>
    }
}

#[component]
fn HomePage() -> impl IntoView {
    let i18n = use_i18n();

    move || i18n.get_locale().as_str()
}

using the configs you gave me, and I'm not able to reproduce your issue, I even tried different browsers too.

Yes an explicit segment does have the priority over everything, the Accept-Language header is only relevant when no locale segment is present see book

@Baptistemontan
Copy link
Owner

I forgot to ask, is this CSR or SSR ? I only tried CSR

@arnaudpoullet
Copy link
Author

arnaudpoullet commented Nov 8, 2024

Yes, that's what I read in the book as well. I'm using SSR with leptos 7rc1 and running cargo leptos watch. I'm not sure that this is relevant but my browser is Firefox.

@Baptistemontan
Copy link
Owner

I'm able to reproduce the bug with SSR, I'll fix it ASAP, Thx for reporting the issue !

@Baptistemontan
Copy link
Owner

Baptistemontan commented Nov 8, 2024

What is happening is that the client use the "lang" attribute to know what locale the server computed, but that attribute is rendered before the children, so any changes to the locale will not reflect, meaning that the locale computed by the router, which is a children of the Provider, is lost afterward. This is easily fixd by simply rendering the "lang" attribute after the children.

@Baptistemontan
Copy link
Owner

Baptistemontan commented Nov 8, 2024

I'll be pushing a new release very soon for leptos 0.7.0-rc1, it will contain the fix, for the timebeing you can use the latest commit on main (rev "aa28461" at the time of this comment)

@Baptistemontan
Copy link
Owner

Baptistemontan commented Nov 9, 2024

I just released v0.5.0-rc1 with the fix https://github.com/Baptistemontan/leptos_i18n/releases/tag/v0.5.0-rc1

@arnaudpoullet
Copy link
Author

That's awesome, thanks for the explaination and the quick fix !

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

Successfully merging a pull request may close this issue.

2 participants