Skip to content

Commit

Permalink
disable language toggle when no t function available
Browse files Browse the repository at this point in the history
  • Loading branch information
kane50613 committed Apr 26, 2024
1 parent ded4e84 commit 6329cb9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/components/header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { HeaderMenu } from "./header-menu";
import { Image } from "astro:assets";
import logo from "@/assets/logo.png";
import { ThemeToggle } from "./theme-toggle";
import { TranslateFunction } from "../utils/language";
import { createTranslation, TranslateFunction } from "../utils/language";
import { LanguageSelect } from "./language-select";
const t = Astro.props.t as TranslateFunction;
const t = (Astro.props.t as TranslateFunction) ?? createTranslation("zh-tw");
const url = new URL(Astro.url);
Expand All @@ -29,7 +29,12 @@ const code = t("code");
<span class="font-medium">{t("global.header.title")}</span>
</a>
<div class="flex-grow"></div>
<LanguageSelect path={url.pathname} code={code} client:load />
<LanguageSelect
path={url.pathname}
disabled={!Astro.props.t}
code={code}
client:load
/>
<ThemeToggle client:visible />
<HeaderMenu />
</nav>
Expand Down
4 changes: 3 additions & 1 deletion src/components/language-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import type { FC } from "react";
export const LanguageSelect: FC<{
code: string;
path: string;
}> = ({ code, path }) => {
disabled?: boolean;
}> = ({ code, path, disabled }) => {
if (path.endsWith("/")) path = path.slice(0, -1);

return (
<Select
value={code}
disabled={disabled}
onValueChange={(lang) => (location.href = `/${lang}${path}`)}
>
<SelectTrigger
Expand Down
8 changes: 5 additions & 3 deletions src/layouts/base-layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ImageMetadata } from "astro";
import logo from "@/assets/logo.png";
import { cn } from "@/utils/cn";
import { TranslateFunction } from "@/utils/language";
import { createTranslation } from "../utils/language";
interface Props {
title: string;
Expand All @@ -15,12 +16,13 @@ interface Props {
t: TranslateFunction;
}
const t = (Astro.props.t as TranslateFunction) ?? createTranslation("zh-tw");
const {
title,
description = Astro.props.t("global.description"),
description = t("global.description"),
icon,
class: className,
t,
} = Astro.props;
const suffix = t("global.title");
Expand Down Expand Up @@ -79,7 +81,7 @@ const themeScript = `!function e(){let t=localStorage.getItem("theme");!t&&windo
crossorigin="anonymous"></script>
</head>
<body>
<Header {t} />
<Header t={Astro.props.t} />
</body>
<div class={cn("mb-8 md:mb-4 container", className)}>
<slot />
Expand Down
4 changes: 0 additions & 4 deletions src/pages/zh-tw/address-to-english.astro
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
---
import BaseLayout from "../../layouts/base-layout.astro";
import { createTranslation } from "../../utils/language";
import Adsense from "../../components/adsense.astro";
export { getStaticPaths } from "../../utils/language";
import { AddressToEnglishInput } from "../../components/address-to-english-input";
const t = createTranslation("zh-tw");
---

<BaseLayout
{t}
title="台灣中華郵政地址轉英文"
description="輸入臺灣地址轉為英文格式,支援中文、全形數字轉換"
>
Expand Down

0 comments on commit 6329cb9

Please sign in to comment.