language.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { route as localeRoute, strip as localeStrip } from "../../../../console/app/src/lib/language"
  2. import type { Locale } from "../../../../console/app/src/lib/language"
  3. export {
  4. LOCALES,
  5. LOCALE_COOKIE,
  6. LOCALE_HEADER,
  7. clearCookie,
  8. cookie,
  9. detectFromAcceptLanguage,
  10. detectFromLanguages,
  11. dir,
  12. fromPathname,
  13. label,
  14. localeFromCookieHeader,
  15. localeFromRequest,
  16. parseLocale,
  17. tag,
  18. } from "../../../../console/app/src/lib/language"
  19. export type { Locale } from "../../../../console/app/src/lib/language"
  20. export const basePath = "/data"
  21. export const baseUrl = "https://opencode.ai"
  22. function normalizeDataPathname(pathname: string) {
  23. const next = localeStrip(pathname)
  24. const path = next.startsWith("/") ? next : `/${next}`
  25. const trailing = path.endsWith("/")
  26. const segments = path.split("/").filter(Boolean)
  27. const dataIndex = segments.lastIndexOf(basePath.slice(1))
  28. const rest = dataIndex === -1 ? segments : segments.slice(dataIndex + 1)
  29. const normalized = `${basePath}${rest.length ? `/${rest.join("/")}` : "/"}`
  30. if (trailing && !normalized.endsWith("/")) return `${normalized}/`
  31. return normalized
  32. }
  33. export function strip(pathname: string) {
  34. return normalizeDataPathname(pathname)
  35. }
  36. export function route(locale: Locale, pathname: string) {
  37. return localeRoute(locale, normalizeDataPathname(pathname))
  38. }
  39. export function localizedUrl(locale: Locale, pathname: string) {
  40. return `${baseUrl}${route(locale, pathname)}`
  41. }