Skip to content

Commit

Permalink
Merge pull request #5 from dig-dao:i18n
Browse files Browse the repository at this point in the history
fix language selector
  • Loading branch information
halsk authored Dec 17, 2023
2 parents 67f1e21 + 93e8ae7 commit b305496
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/layouts/components/LanguageSwitcher.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,8 @@
import { astroI18n } from "astro-i18n";
---

<script>
import { l } from "astro-i18n";
const switchLang = (e: any) => {
console.log("switching lang to ", e.target.value);
window.location.href = l(
window.location.pathname,
{},
{ targetLocale: e.target.value },
);
};
const selector = document.querySelector(
"#language-switcher",
) as HTMLSelectElement;
console.log("add event listener to ", selector);
selector.addEventListener("change", switchLang);
</script>
<label for="language-switcher">Language</label>
<select id="language-switcher" class="ml-2">
<select id="language-switcher" class="ml-2" onchange="switchLang()">
{
Object.entries(astroI18n.locales).map(([id, lang]) => (
<option value={lang} selected={lang === astroI18n.locale}>
Expand All @@ -28,3 +12,19 @@ import { astroI18n } from "astro-i18n";
))
}
</select>
<script is:inline>
console.log("loaded language switcher");
const localePath = (path, locale) => {
const prefix = locale === "ja" ? "" : `/${locale}`;
if (path.split("/")[1] === "en") {
return prefix + "/" + path.split("/").slice(2).join("/");
} else {
if (path === "/") return prefix;
return prefix + path;
}
};
const switchLang = () => {
const lang = document.querySelector("#language-switcher").value;
window.location.href = localePath(window.location.pathname, lang);
};
</script>

0 comments on commit b305496

Please sign in to comment.