import { apiGet } from '@/lib/api' import { currentRenderContext } from '@/lib/renderContext' import type { To } from 'react-router-dom' export const SUPPORTED_LOCALES = ['ja', 'ain-Kana'] as const export type LocaleCode = typeof SUPPORTED_LOCALES[number] // This UI locale uses Japanese content until the API supports Mongolian. export const MONGOLIAN_LOCALE = { code: 'mn-Mong', name: 'ᠮᠣᠩᠭᠣᠯ' } as const export type UILocaleCode = LocaleCode | typeof MONGOLIAN_LOCALE.code export type AvailableLocale = { code: string; name: string } export const fetchLocales = (): Promise<{ locales: AvailableLocale[] }> => apiGet ('/locales') export const isLocaleCode = (value: unknown): value is LocaleCode => SUPPORTED_LOCALES.some (locale => locale === value) export const localeLanguage = (locale: LocaleCode): 'ja' | 'ain' => locale === 'ja' ? 'ja' : 'ain' export const localeFromPath = (pathname: string): LocaleCode | null => { const segment = pathname.split (/[/?#]/)[1] return isLocaleCode (segment) ? segment : null } // Frontend layout selection must not change the API's content locale. export const isVerticalPreviewPath = (pathname: string): boolean => /^\/mn-Mong(?:[/?#]|$)/.test (pathname) export const isUILocaleCode = (value: unknown): value is UILocaleCode => value === MONGOLIAN_LOCALE.code || isLocaleCode (value) export const uiLocaleFromPath = (pathname: string): UILocaleCode => isVerticalPreviewPath (pathname) ? MONGOLIAN_LOCALE.code : localeFromPath (pathname) ?? 'ja' export const resolveViewingLocale = ( pathname: string, routeLocale?: unknown, ): LocaleCode => isLocaleCode (routeLocale) ? routeLocale : localeFromPath (pathname) ?? 'ja' export const currentViewingLocale = (): LocaleCode => resolveViewingLocale (currentRenderContext ()?.pathname ?? '') export const unlocalisedPath = (pathname: string): string => { const locale = isVerticalPreviewPath (pathname) ? 'mn-Mong' : localeFromPath (pathname) return locale == null ? pathname : pathname.slice (locale.length + 1) || '/' } export const localiseTo = (to: To, locale: LocaleCode, viewingPath = ''): To => { const pathname = typeof to === 'string' ? to : to.pathname if (pathname == null || !(pathname.startsWith ('/')) || pathname.startsWith ('//') || localeFromPath (pathname) != null || isVerticalPreviewPath (pathname)) return to const prefix = isVerticalPreviewPath (viewingPath) ? 'mn-Mong' : locale const localised = `/${ prefix }${ pathname }` return typeof to === 'string' ? localised : { ...to, pathname: localised } } export const switchLocalePath = ( location: { pathname: string; search: string; hash: string }, locale: UILocaleCode, ): string => `/${ locale }${ unlocalisedPath (location.pathname) }${ location.search }${ location.hash }` // Keep automatic selection independent of the page and API route trees. export const automaticallySelectedLocale = (): LocaleCode => 'ja' export const legacyLocalePath = ( location: { pathname: string; search: string; hash: string }, isPage: (pathname: string) => boolean = () => false, ): string | null => { if (localeFromPath (location.pathname) != null || isVerticalPreviewPath (location.pathname) || !isPage (location.pathname)) return null return switchLocalePath (location, automaticallySelectedLocale ()) }