Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

feat(site): auto-detect preferred language

yoma6c5fb72030c3
diff --git a/apps/site/src/components/navbar/language-switcher.astro b/apps/site/src/components/navbar/language-switcher.astro
index 5185b91d..0845f88f 100644
--- a/apps/site/src/components/navbar/language-switcher.astro
+++ b/apps/site/src/components/navbar/language-switcher.astro
@@ -1,5 +1,5 @@
---
-import { getLocalizedPath, getSiteLocale, siteCopy } from "@/i18n";
+import { getLocalizedPath, getSiteLocale, siteCopy, siteLocaleDataAttribute } from "@/i18n";
const { pathname, compact = false } = Astro.props;
const locale = getSiteLocale(pathname);
@@ -42,6 +42,7 @@ const currentLanguageLabel = languageOptions.find((item) => item.locale === loca
languageOptions.map((item) => (
<a
href={item.path}
+ {...{ [siteLocaleDataAttribute]: item.locale }}
class:list={[
"flex items-center justify-between rounded-lg px-3 py-2 text-sm transition",
item.locale === locale
diff --git a/apps/site/src/i18n.ts b/apps/site/src/i18n.ts
index 242704b9..b69c6729 100644
--- a/apps/site/src/i18n.ts
+++ b/apps/site/src/i18n.ts
@@ -1,6 +1,8 @@
export type SiteLocale = "zh-CN" | "en-US";
export const defaultSiteLocale: SiteLocale = "zh-CN";
+export const siteLocaleStorageKey = "edgeever.site.locale";
+export const siteLocaleDataAttribute = "data-edgeever-site-locale";
export const getSiteLocale = (pathname: string): SiteLocale => (pathname === "/en" || pathname.startsWith("/en/") ? "en-US" : "zh-CN");
diff --git a/apps/site/src/layouts/Layout.astro b/apps/site/src/layouts/Layout.astro
index 3cec0e0b..8740f873 100644
--- a/apps/site/src/layouts/Layout.astro
+++ b/apps/site/src/layouts/Layout.astro
@@ -2,7 +2,7 @@
import { SEO } from "astro-seo";
import Footer from "@/components/footer.astro";
import Navbar from "@/components/navbar/navbar.astro";
-import { getLocalizedPath, getSiteLocale, siteCopy } from "@/i18n";
+import { getLocalizedPath, getSiteLocale, siteCopy, siteLocaleDataAttribute, siteLocaleStorageKey } from "@/i18n";
import "@fontsource-variable/inter/index.css";
import "@fontsource-variable/bricolage-grotesque";
import "../styles/global.css";
@@ -20,6 +20,7 @@ export interface Props {
articleTags?: string[];
jsonLd?: Record<string, unknown> | Record<string, unknown>[];
noindex?: boolean;
+ localeRedirect?: boolean;
}
const canonicalURL = new URL(Astro.url.pathname, Astro.site).toString();
@@ -43,6 +44,7 @@ const {
articleTags,
jsonLd,
noindex = false,
+ localeRedirect = true,
} = Astro.props;
const resolvedImageWithDomain = new URL(image, Astro.site).toString();
@@ -52,6 +54,83 @@ const makeTitle = title
: copy.defaultTitle;
const faviconVersion = "edgeever-brand-20260703";
+const localeRedirectScript = `(() => {
+ const storageKey = ${JSON.stringify(siteLocaleStorageKey)};
+ const localeAttribute = ${JSON.stringify(siteLocaleDataAttribute)};
+ const supportedLocales = ["zh-CN", "en-US"];
+ const normalizeLocale = (locale) => {
+ if (!locale) return null;
+ const normalized = String(locale).toLowerCase();
+
+ if (normalized === "zh" || normalized === "zh-cn" || normalized.startsWith("zh-hans")) {
+ return "zh-CN";
+ }
+
+ if (normalized === "en" || normalized === "en-us" || normalized.startsWith("en-")) {
+ return "en-US";
+ }
+
+ return null;
+ };
+ const getStoredLocale = () => {
+ try {
+ return normalizeLocale(window.localStorage.getItem(storageKey));
+ } catch {
+ return null;
+ }
+ };
+ const writeStoredLocale = (locale) => {
+ if (!supportedLocales.includes(locale)) return;
+
+ try {
+ window.localStorage.setItem(storageKey, locale);
+ } catch {
+ // Local storage can be unavailable in private or restricted browser contexts.
+ }
+ };
+ const getBrowserLocale = () => {
+ const browserLocales = navigator.languages?.length ? navigator.languages : [navigator.language];
+
+ for (const locale of browserLocales) {
+ const supported = normalizeLocale(locale);
+
+ if (supported) {
+ return supported;
+ }
+ }
+
+ return null;
+ };
+ const getCurrentLocale = (pathname) => pathname === "/en" || pathname.startsWith("/en/") ? "en-US" : "zh-CN";
+ const getLocalizedPath = (locale, pathname) => {
+ const normalizedPath = pathname.startsWith("/") ? pathname : "/" + pathname;
+
+ if (locale === "zh-CN") {
+ return normalizedPath === "/en" ? "/" : normalizedPath.replace(/^\\/en(?=\\/|$)/, "") || "/";
+ }
+
+ if (normalizedPath === "/") {
+ return "/en/";
+ }
+
+ return normalizedPath.startsWith("/en/") ? normalizedPath : "/en" + normalizedPath;
+ };
+ const desiredLocale = getStoredLocale() ?? getBrowserLocale();
+
+ if (desiredLocale && desiredLocale !== getCurrentLocale(window.location.pathname)) {
+ const nextPath = getLocalizedPath(desiredLocale, window.location.pathname);
+ window.location.replace(nextPath + window.location.search + window.location.hash);
+ }
+
+ document.addEventListener("click", (event) => {
+ const target = event.target instanceof Element ? event.target.closest("[" + localeAttribute + "]") : null;
+ const locale = target?.getAttribute(localeAttribute);
+
+ if (locale) {
+ writeStoredLocale(locale);
+ }
+ }, { capture: true });
+})();`;
---
<!doctype html>
@@ -65,6 +144,7 @@ const faviconVersion = "edgeever-brand-20260703";
<link rel="alternate" hreflang="x-default" href={zhAlternateURL} />
<meta name="theme-color" content="#f8fafc" />
<meta name="generator" content={Astro.generator} />
+ {localeRedirect && <script is:inline set:html={localeRedirectScript} />}
<!-- <link rel="preload" as="image" href={src} alt="Hero" /> -->
<SEO
diff --git a/apps/site/src/pages/404.astro b/apps/site/src/pages/404.astro
index 89f7fad0..10c00628 100644
--- a/apps/site/src/pages/404.astro
+++ b/apps/site/src/pages/404.astro
@@ -3,7 +3,7 @@ import Container from "@/components/container.astro";
import Layout from "@/layouts/Layout.astro";
---
-<Layout title="404 Not Found" noindex={true}>
+<Layout title="404 Not Found" noindex={true} localeRedirect={false}>
<Container>
<div class="min-h-[calc(100vh-16rem)] flex items-center justify-center">
<div class="mt-16 text-center">