Skip to content

Commit 241741f

Browse files
committed
fix(i18n): 🐛 fixed path prefixing with locale redirection
1 parent 2c174b4 commit 241741f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

packages/perseus/src/locale_detector.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::path_prefix::get_path_prefix_client;
12
use crate::Locales;
23

34
/// Detects which locale the user should be served and redirects appropriately. This should only be used when the user navigates to a
@@ -36,12 +37,19 @@ pub fn detect_locale(url: String, locales: Locales) {
3637
}
3738
}
3839

40+
// Figure out what the new localized route should be
41+
// This is complex because we need to strip away the base path
42+
// We use the pathname part of the URL because the base path getter gets the pathname too
43+
let base_path = get_path_prefix_client(); // We know this doesn't have a trailing slash
44+
let loc = url.strip_prefix(&base_path).unwrap_or(&url);
45+
let new_loc = format!("{}/{}/{}", base_path, locale, loc);
46+
3947
// Imperatively navigate to the localized route
4048
// This certainly shouldn't fail...
4149
web_sys::window()
4250
.unwrap()
4351
.location()
44-
.replace(&format!("/{}/{}", locale, url))
52+
.replace(&new_loc)
4553
.unwrap();
4654
}
4755

0 commit comments

Comments
 (0)