mirror of
https://github.com/beilunyang/moemail.git
synced 2026-06-07 08:29:56 +08:00
feat(i18n): enhance localization support with new languages(zh-tw/ja)
This commit is contained in:
38
app/hooks/use-locale-switcher.ts
Normal file
38
app/hooks/use-locale-switcher.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user