21 lines
568 B
TypeScript
21 lines
568 B
TypeScript
import { stripLocalePrefix, toLocalePath } from '~/utils/locale-path'
|
|
import type { SiteLocale } from '~/types/locale'
|
|
|
|
export function useLocalePath() {
|
|
const { locale } = useSiteLocale()
|
|
|
|
return (path: string, targetLocale?: SiteLocale) =>
|
|
toLocalePath(path, targetLocale ?? locale.value)
|
|
}
|
|
|
|
export function useSwitchLocale() {
|
|
const route = useRoute()
|
|
const { setLocale } = useSiteLocale()
|
|
|
|
return (next: SiteLocale) => {
|
|
const basePath = stripLocalePrefix(route.path)
|
|
setLocale(next)
|
|
return navigateTo(toLocalePath(basePath, next))
|
|
}
|
|
}
|