summaryrefslogtreecommitdiff
path: root/examples/docs/src/pages/index.astro
blob: f5a8fd318504e415a3c92044a17f20d81e1b295a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
---
import Layout from '../layouts/MainLayout.astro';
import { KNOWN_LANGUAGES } from "../languages";
---
<div id="known_languages" hidden>{KNOWN_LANGUAGES.join(",")}</div>
<script>
  // WIP: trigger a client-side redirect based on the browser language.
  // A vercel.json redirect is enforced in production, so no user should ever see this page.
  // Remove the vercel.json redirect when this is ready.
  const KNOWN_LANGUAGES = document.querySelector("#known_languages")?.textContent?.split(",") ?? ['bg', 'de','en','es','fi','nl','pt-br','zh-CN','zh-TW', 'fr'];
  let newLangWithRegion = (window.navigator.userLanguage || window.navigator.language || 'en-US').substr(0, 5);
  let newLang = newLangWithRegion.substr(0, 2);

  let langPathRegex = new RegExp(`\/(${KNOWN_LANGUAGES.join("|")})\/`);
  let actualDest = window.location.pathname.replace(langPathRegex, "/");
  if (actualDest == "/") actualDest = `/introduction`;
  if (KNOWN_LANGUAGES.includes(newLangWithRegion)) {
    window.location.pathname = '/' + newLangWithRegion + actualDest;
  } else if (KNOWN_LANGUAGES.includes(newLang)) {
    window.location.pathname = '/' + newLang + actualDest;
  } else {
    window.location.pathname = actualDest;
  }
</script>