Skip to content

Commit

Permalink
Locale sidebar URL fixes. (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMB11 authored Feb 18, 2024
1 parent b8a1a12 commit 2310c69
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions .vitepress/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { resolve } from "path/posix";
import { ExtendedSidebarItem } from "./sidebars/utils";
import { DefaultTheme, LocaleConfig } from "vitepress";

export function applyTranslations(translationSource: { [key: string]: string; }, fallbackSource: { [key: string]: string }, sidebar: ExtendedSidebarItem[]): ExtendedSidebarItem[] {
export function applyTranslations(locale: string, translationSource: { [key: string]: string; }, fallbackSource: { [key: string]: string }, sidebar: ExtendedSidebarItem[]): ExtendedSidebarItem[] {
const sidebarCopy = JSON.parse(JSON.stringify(sidebar));

for (const item of sidebarCopy) {
Expand All @@ -17,8 +17,13 @@ export function applyTranslations(translationSource: { [key: string]: string; },
item.text = translationSource[item.text];
}

if (item.link && locale !== "en_us") {
// Prefix the link with the locale
item.link = `/${locale}${item.link}`;
}

if (item.items) {
item.items = applyTranslations(translationSource, fallbackSource, item.items);
item.items = applyTranslations(locale, translationSource, fallbackSource, item.items);
}
}

Expand All @@ -33,7 +38,7 @@ export function generateTranslatedSidebars(_rootDir: string, sidebars: { [url: s
// Create the default english sidebar.
for (const sidebarPair of Object.entries(sidebars)) {
const [url, sidebar] = sidebarPair;
sidebarResult[url] = applyTranslations(englishFallbacks, englishFallbacks, sidebar);
sidebarResult[url] = applyTranslations("en_us", englishFallbacks, englishFallbacks, sidebar);
}

const translatedFolder = resolve(_rootDir, "..", "translated");
Expand Down Expand Up @@ -61,7 +66,7 @@ export function generateTranslatedSidebars(_rootDir: string, sidebars: { [url: s
for (const sidebarPair of Object.entries(sidebars)) {
const [url, sidebar] = sidebarPair;

sidebarResult[`/${folder}${url}`] = applyTranslations(translations, englishFallbacks, sidebar);
sidebarResult[`/${folder}${url}`] = applyTranslations(folder, translations, englishFallbacks, sidebar);
}
}

Expand Down

0 comments on commit 2310c69

Please sign in to comment.