Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions press.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export default defineConfig({
href="/favicon-16x16.png"
/>
<link rel="manifest" href="/site.webmanifest" />
<script src="/language-redirect.js" />

{/* SEO / crawler hints */}
<meta name="author" content="RustFS" />
Expand Down
22 changes: 22 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="noindex, follow" />
<title>RustFS Documentation</title>
<script src="/language-redirect.js"></script>
</head>
<body>
<main>
<p>Choose your language:</p>
<nav aria-label="Documentation language">
<a href="/en">English</a>
<a href="/zh">简体中文</a>
<a href="/de">Deutsch</a>
<a href="/fr">Français</a>
<a href="/ja">日本語</a>
</nav>
</main>
</body>
</html>
65 changes: 65 additions & 0 deletions public/language-redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(() => {
const supportedLanguages = new Set(["en", "zh", "de", "fr", "ja"]);
const storageKey = "rustfs-docs-language";
const getPathLanguage = () =>
window.location.pathname.match(/^\/(en|zh|de|fr|ja)(?:\/|$)/)?.[1];
Comment on lines +2 to +5
const savePathLanguage = () => {
const language = getPathLanguage();
if (!language) return;

try {
window.localStorage.setItem(storageKey, language);
} catch {
// Language detection still works when storage is unavailable.
}
};

if (getPathLanguage()) {
savePathLanguage();

if (window.history) {
const pushState = window.history.pushState;
window.history.pushState = function (...args) {
const result = pushState.apply(this, args);
window.queueMicrotask(savePathLanguage);
return result;
};

const replaceState = window.history.replaceState;
window.history.replaceState = function (...args) {
const result = replaceState.apply(this, args);
window.queueMicrotask(savePathLanguage);
return result;
};
window.addEventListener("popstate", savePathLanguage);
}

return;
}

if (window.location.pathname !== "/" && window.location.pathname !== "/index.html") {
return;
}

let savedLanguage;
try {
savedLanguage = window.localStorage.getItem(storageKey);
} catch {
// Fall back to the browser language when storage is unavailable.
}

const browserLanguages = navigator.languages?.length
? navigator.languages
: [navigator.language];
const detectedLanguage = browserLanguages
.map((language) => language?.toLowerCase().split("-")[0])
.find((language) => language && supportedLanguages.has(language));
const language =
savedLanguage && supportedLanguages.has(savedLanguage)
? savedLanguage
: (detectedLanguage ?? "en");

window.location.replace(
`/${language}${window.location.search}${window.location.hash}`,
);
})();
5 changes: 4 additions & 1 deletion scripts/add-language-redirects.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import { fileURLToPath } from 'node:url';
import { collectLanguageRedirects } from './language-redirects.mjs';

const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const SOURCE_INDEX = path.join(ROOT, 'public', 'index.html');
const OUTPUT_INDEX = path.join(ROOT, 'dist', 'public', 'index.html');
const OUTPUT_REDIRECTS = path.join(ROOT, 'dist', 'public', '_redirects');
const rules = collectLanguageRedirects();

const output = [...rules].map(([source, target]) => `${source} ${target}`).join('\n');
fs.copyFileSync(SOURCE_INDEX, OUTPUT_INDEX);
fs.writeFileSync(OUTPUT_REDIRECTS, `${output}\n`);
console.log(`language redirects: wrote ${rules.size} Cloudflare rules`);
console.log(`language redirects: wrote the language entry page and ${rules.size} Cloudflare rules`);
1 change: 0 additions & 1 deletion scripts/language-redirects.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function prefixEnglishTarget(target) {
export function collectLanguageRedirects() {
/** @type {Map<string, string>} */
const rules = new Map();
rules.set('/', '/en 302');

const existingRules = fs.readFileSync(SOURCE_REDIRECTS, 'utf8').split('\n');
for (const line of existingRules) {
Expand Down