Skip to content

Commit

Permalink
Fix i18n - deadlinks + use Intl instead. (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMB11 authored Feb 18, 2024
1 parent 283fcb1 commit b8a1a12
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 270 deletions.
3 changes: 3 additions & 0 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export default defineVersionedConfig(__dirname, {
...loadLocales(__dirname)
},

// Prevent dead links from being reported as errors - allows partially translated pages to be built.
ignoreDeadLinks: true,

srcExclude: [
"README.md",
"LICENSE.md",
Expand Down
28 changes: 18 additions & 10 deletions .vitepress/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { readdirSync, readFileSync } from "fs";
import { existsSync, readdirSync, readFileSync } from "fs";
import { resolve } from "path/posix";
import { ExtendedSidebarItem } from "./sidebars/utils";
import { DefaultTheme, LocaleConfig, LocaleSpecificConfig } from "vitepress";
import inter from "inter";
import { DefaultTheme, LocaleConfig } from "vitepress";

export function applyTranslations(translationSource: { [key: string]: string; }, fallbackSource: { [key: string]: string }, sidebar: ExtendedSidebarItem[]): ExtendedSidebarItem[] {
const sidebarCopy = JSON.parse(JSON.stringify(sidebar));
Expand Down Expand Up @@ -45,7 +44,19 @@ export function generateTranslatedSidebars(_rootDir: string, sidebars: { [url: s
.map(dirent => dirent.name);

for (const folder of translatedFolders) {
const translations: { [key: string]: string; } = JSON.parse(readFileSync(resolve(translatedFolder, folder, "sidebar_translations.json"), "utf-8"));
const sidebarPath = resolve(translatedFolder, folder, "sidebar_translations.json")

// If sidebar translations dont exist, use english fallback.
if (!existsSync(sidebarPath)) {
for (const sidebarPair of Object.entries(sidebars)) {
const [url, sidebar] = sidebarPair;
sidebarResult[`/${folder}${url}`] = sidebarResult[url];
}

continue;
}

const translations: { [key: string]: string; } = JSON.parse(readFileSync(sidebarPath, "utf-8"));

for (const sidebarPair of Object.entries(sidebars)) {
const [url, sidebar] = sidebarPair;
Expand All @@ -71,13 +82,10 @@ export function loadLocales(_rootDir: string): LocaleConfig<DefaultTheme.Config>
let firstHalf: string = folder.slice(0, 2);
let secondHalf: string = folder.slice(3, 5);

let localeName: string = "";
if (firstHalf == secondHalf) {
localeName = inter.load(folder).getLanguage(folder.slice(0, 2)).displayName;
} else {
localeName = inter.load(folder).getLanguage(folder).displayName;
}
let locale = new Intl.DisplayNames([`${firstHalf}-${secondHalf.toUpperCase()}`], { type: 'language' });
let localeName = locale?.of(`${firstHalf}-${secondHalf.toUpperCase()}`)!;

// Capitalize the first letter of the locale name
localeName = localeName.charAt(0).toUpperCase() + localeName.slice(1);

locales[folder] = {
Expand Down
Loading

0 comments on commit b8a1a12

Please sign in to comment.