feat(i18n): enhance localization support with new languages(zh-tw/ja)

This commit is contained in:
beilunyang
2025-10-21 23:53:58 +08:00
parent 0c7a4d84a5
commit 7398b73f3f
20 changed files with 915 additions and 97 deletions

View File

@@ -0,0 +1,38 @@
"use client"
import { useCallback } from "react"
import { useLocale } from "next-intl"
import { usePathname, useRouter } from "next/navigation"
import { i18n, type Locale } from "@/i18n/config"
export function useLocaleSwitcher() {
const locale = useLocale()
const router = useRouter()
const pathname = usePathname()
const switchLocale = useCallback(
(newLocale: Locale) => {
if (newLocale === locale) return
document.cookie = `NEXT_LOCALE=${newLocale}; path=/; max-age=31536000`
const segments = pathname.split("/")
if (i18n.locales.includes(segments[1] as Locale)) {
segments[1] = newLocale
} else {
segments.splice(1, 0, newLocale)
}
router.push(segments.join("/"))
router.refresh()
},
[locale, pathname, router]
)
return {
locale,
switchLocale,
locales: i18n.locales,
}
}