copilot/add-i18n-support-for-english
copilot/add-i18n-support-for-english
dependabot/npm_and_yarn/astro-5.16.4
dependabot/npm_and_yarn/katex-0.16.27
dependabot/npm_and_yarn/prettier-3.7.4
dependabot/npm_and_yarn/tailwindcss-4.1.16
dependabot/npm_and_yarn/tailwindcss/vite-4.1.16
dependabot/npm_and_yarn/typescript-eslint-8.46.2
dependabot/npm_and_yarn/typescript-eslint/parser-8.46.2
development
main
| 1 | import { ui, defaultLang } from './ui' |
| 2 | |
| 3 | export function getLangFromUrl(url: URL) { |
| 4 | const [, lang] = url.pathname.split('/') |
| 5 | if (lang in ui) return lang as keyof typeof ui |
| 6 | return defaultLang |
| 7 | } |
| 8 | |
| 9 | export function useTranslations(lang: keyof typeof ui) { |
| 10 | return function t(key: keyof (typeof ui)[typeof defaultLang]) { |
| 11 | return ui[lang][key] || ui[defaultLang][key] |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | export function getLocalizedPath(path: string, lang: string) { |
| 16 | if (lang === defaultLang) { |
| 17 | return path |
| 18 | } |
| 19 | return `/${lang}${path}` |
| 20 | } |
| 21 | |
| 22 | export function getAlternateLanguage(currentLang: string) { |
| 23 | return currentLang === 'id' ? 'en' : 'id' |
| 24 | } |
| 25 | |
| 26 | export function getAlternateUrl(url: URL, targetLang: string) { |
| 27 | const currentLang = getLangFromUrl(url) |
| 28 | let pathname = url.pathname |
| 29 | |
| 30 | // Remove current language prefix if it exists |
| 31 | if (currentLang !== defaultLang) { |
| 32 | pathname = pathname.replace(`/${currentLang}`, '') |
| 33 | } |
| 34 | |
| 35 | // Add target language prefix if it's not the default language |
| 36 | if (targetLang !== defaultLang) { |
| 37 | pathname = `/${targetLang}${pathname}` |
| 38 | } |
| 39 | |
| 40 | // Ensure pathname starts with / |
| 41 | if (!pathname.startsWith('/')) { |
| 42 | pathname = `/${pathname}` |
| 43 | } |
| 44 | |
| 45 | return pathname |
| 46 | } |