diff --git a/app/islands/TranslatorButton.tsx b/app/islands/TranslatorButton.tsx new file mode 100644 index 0000000..c6ab9df --- /dev/null +++ b/app/islands/TranslatorButton.tsx @@ -0,0 +1,74 @@ +import { type FC, useState } from "hono/jsx"; + +type SupportedTranslatorAPI = + | "en" + | "zn" + | "ja" + | "pt" + | "ru" + | "es" + | "tr" + | "hi" + | "vi" + | "bn"; + +const SELECT_OPTIONS: SupportedTranslatorAPI[] = [ + "ja", + "en", + "zn", + "pt", + "ru", + "es", + "tr", + "hi", + "vi", + "bn", +]; + +type Props = { + content: string; +}; + +const TranslatorButton: FC = ({ content }) => { + const [value, setValue] = useState("ja"); + + // biome-ignore lint: lint/suspicious/noExplicitAny + const handleSelectChange = async (e: any) => { + setValue(e.target.value); + if ( + "translation" in self && + // biome-ignore lint: lint/suspicious/noExplicitAny + "createTranslator" in (self.translation as any) + ) { + // biome-ignore lint: lint/suspicious/noExplicitAny + const translator = await (self.translation as any).createTranslator({ + sourceLanguage: value, + targetLanguage: e.target.value, + }); + await translator.translate(content); + } else { + window.alert("The Translator API is not supported"); + } + }; + + return ( + <> + + + + ); +}; + +export default TranslatorButton; diff --git a/app/routes/posts/_renderer.tsx b/app/routes/posts/_renderer.tsx index 41bdfec..b8f2b67 100644 --- a/app/routes/posts/_renderer.tsx +++ b/app/routes/posts/_renderer.tsx @@ -1,11 +1,15 @@ import { jsxRenderer } from "hono/jsx-renderer"; import LikeButton from "../../islands/LikeButton"; import SnsButton from "../../islands/SnsButton"; +import TranslatorButton from "../../islands/TranslatorButton"; export default jsxRenderer(({ children, Layout, frontmatter }) => { const _title = `${frontmatter?.title} | yossy.dev`; return ( +
+ +

{frontmatter?.title}