-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f08920e
commit 875457f
Showing
10 changed files
with
126 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/(home)/article/[slug]/_components/translate-paragraph.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"use client"; | ||
import { papagoTranslateAction } from "@/actions/papago-translate-action"; | ||
import TranslateIcon from "@/assets/svg/translate.svg"; | ||
import { useServerActionState } from "@/hooks/use-server-action-state"; | ||
import { isKorean } from "@/utils/is-korean"; | ||
import clsx from "clsx"; | ||
import { useLocale } from "next-intl"; | ||
|
||
const TranslateParagraph = ({ | ||
children, | ||
...props | ||
}: { children: React.ReactNode }) => { | ||
const locale = useLocale(); | ||
const text = typeof children === "string" ? children : ""; | ||
const isKoreanText = isKorean(text); | ||
|
||
const [pending, translate, data] = useServerActionState(async () => { | ||
if (!text) return undefined; | ||
return await papagoTranslateAction(text, locale); | ||
}); | ||
|
||
return ( | ||
<div className="flex mb-4 group flex-wrap relative"> | ||
{isKoreanText ? ( | ||
<div className={clsx("select-none left-0 top-0.5 absolute")}> | ||
{pending ? ( | ||
<span className="loading loading-spinner loading-xs size-4" /> | ||
) : ( | ||
<TranslateIcon | ||
onClick={translate} | ||
className={clsx( | ||
"border rounded border-base-content group-hover:block mobile:block hidden size-4 cursor-pointer translate-y-[3px]", | ||
pending && "animate-pulse pointer-events-none", | ||
data && "!hidden", | ||
)} | ||
/> | ||
)} | ||
</div> | ||
) : null} | ||
|
||
<p className={clsx("!mb-0 w-full", isKoreanText && "pl-5")} {...props}> | ||
{children} | ||
</p> | ||
{data && ( | ||
<p className="!mb-0 pl-5 !mt-1 w-full shrink-0 text-base-content/60"> | ||
{data.translatedText} | ||
</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
export { TranslateParagraph }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const isKorean = (text: string) => { | ||
/** | ||
\uAC00-\uD7AF:韩文音节块(完整的韩文字符) | ||
包含了大部分常用的韩文字符 | ||
例如:가, 나, 다, 라 等 | ||
\u1100-\u11FF:韩文字母(谚文字母) | ||
包含韩文的基本字母(자음 辅音和 모음 元音) | ||
例如:ㄱ, ㄴ, ㄷ, ㅏ, ㅑ 等 | ||
\u3130-\u318F:韩文兼容字母 | ||
另一种表示韩文字母的 Unicode 范围 | ||
提供了额外的字母表示方式 | ||
\u3000-\u303F:标点符号和空格 | ||
包含了一些东亚标点符号 | ||
\uFF00-\uFFEF:全角字符 | ||
包含全角的韩文字符和标点符号 | ||
\s:匹配任何空白字符(空格、制表符、换行等) | ||
*/ | ||
// const symbol = /\u3000-\u303F\uFF00-\uFFEF\s/.test(text); | ||
const koreanChar = /[\uAC00-\uD7AF\u1100-\u11FF\u3130-\u318F]/.test(text); | ||
return koreanChar; | ||
}; | ||
|
||
export { isKorean }; |