mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 02:37:05 +02:00
Merge pull request #355 from Hitjack007/restore-locale-redirect
This commit is contained in:
1 file changed
+24
@@ -1,3 +1,27 @@
|
||||
<script>
|
||||
import locales from "@blitzkit/i18n/locales.json";
|
||||
import { getBestLocale } from "../core/i18n/getBestLocale";
|
||||
|
||||
const pathname = window.location.pathname;
|
||||
const hasPreference = localStorage.getItem("preferred-locale") !== null;
|
||||
const bestLocale = getBestLocale();
|
||||
const currentLocale =
|
||||
locales.supported.find(
|
||||
({ locale }) =>
|
||||
pathname === `/${locale}` || pathname.startsWith(`/${locale}/`),
|
||||
)?.locale ?? locales.default;
|
||||
|
||||
if (
|
||||
bestLocale !== currentLocale &&
|
||||
(hasPreference || currentLocale === locales.default)
|
||||
) {
|
||||
const rawPath =
|
||||
currentLocale === locales.default
|
||||
? pathname
|
||||
: pathname.slice(`/${currentLocale}`.length) || "/";
|
||||
|
||||
window.location.pathname = `${
|
||||
bestLocale === locales.default ? "" : `/${bestLocale}`
|
||||
}${rawPath}`;
|
||||
}
|
||||
</script>
|
||||
Reference in new issue
Block a user