Skip to content

Build your personal knowledge base with Trilium Notes

feat(website/i18n): swap locale when footer

Elian Doran9ba1e9d73212
diff --git a/apps/website/src/components/Footer.tsx b/apps/website/src/components/Footer.tsx
index 0c1cb4b6c8..c421be9618 100644
--- a/apps/website/src/components/Footer.tsx
+++ b/apps/website/src/components/Footer.tsx
@@ -5,11 +5,13 @@ import githubDiscussionsIcon from "../assets/boxicons/bx-discussion.svg?raw";
import matrixIcon from "../assets/boxicons/bx-message-dots.svg?raw";
import redditIcon from "../assets/boxicons/bx-reddit.svg?raw";
import { Link } from "./Button.js";
-import { LOCALES } from "../i18n";
+import { LOCALES, swapLocaleInUrl } from "../i18n";
import { useTranslation } from "react-i18next";
+import { useLocation } from "preact-iso";
export default function Footer() {
const { t } = useTranslation();
+ const { url } = useLocation();
return (
<footer>
@@ -26,7 +28,7 @@ export default function Footer() {
<div class="row">
<nav class="languages">
{LOCALES.map(locale => (
- <Link href={"/" + locale.id + "/"}>{locale.name}</Link>
+ <Link href={swapLocaleInUrl(url, locale.id)}>{locale.name}</Link>
))}
</nav>
</div>
diff --git a/apps/website/src/i18n.spec.ts b/apps/website/src/i18n.spec.ts
index efc1e18e87..e06cdba3c1 100644
--- a/apps/website/src/i18n.spec.ts
+++ b/apps/website/src/i18n.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { mapLocale } from "./i18n";
+import { mapLocale, swapLocaleInUrl } from "./i18n";
describe("mapLocale", () => {
it("maps Chinese", () => {
@@ -12,3 +12,12 @@ describe("mapLocale", () => {
expect(mapLocale("ro")).toStrictEqual("ro");
});
});
+
+describe("swapLocale", () => {
+ it("swap locale in URL", () => {
+ expect(swapLocaleInUrl("/get-started", "ro")).toStrictEqual("/ro/get-started");
+ expect(swapLocaleInUrl("/ro/get-started", "ro")).toStrictEqual("/ro/get-started");
+ expect(swapLocaleInUrl("/en/get-started", "ro")).toStrictEqual("/ro/get-started");
+ expect(swapLocaleInUrl("/ro/", "en")).toStrictEqual("/en/");
+ });
+});
diff --git a/apps/website/src/i18n.ts b/apps/website/src/i18n.ts
index 5469071665..291eca69b9 100644
--- a/apps/website/src/i18n.ts
+++ b/apps/website/src/i18n.ts
@@ -31,3 +31,13 @@ export function mapLocale(locale: string) {
// Default for everything else
return locale.split('-')[0]; // e.g. "en-US" -> "en"
}
+
+export function swapLocaleInUrl(url: string, newLocale: string) {
+ const components = url.split("/");
+ if (components.length === 2) {
+ return `/${newLocale}${url}`;
+ } else {
+ components[1] = newLocale;
+ return components.join("/");
+ }
+}