language.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. export const LOCALES = [
  2. "en",
  3. "zh",
  4. "zht",
  5. "ko",
  6. "de",
  7. "es",
  8. "fr",
  9. "it",
  10. "da",
  11. "ja",
  12. "pl",
  13. "ru",
  14. "ar",
  15. "no",
  16. "br",
  17. "th",
  18. "tr",
  19. ] as const
  20. export type Locale = (typeof LOCALES)[number]
  21. export const LOCALE_COOKIE = "oc_locale" as const
  22. export const LOCALE_HEADER = "x-opencode-locale" as const
  23. function fix(pathname: string) {
  24. if (pathname.startsWith("/")) return pathname
  25. return `/${pathname}`
  26. }
  27. const LABEL = {
  28. en: "English",
  29. zh: "简体中文",
  30. zht: "繁體中文",
  31. ko: "한국어",
  32. de: "Deutsch",
  33. es: "Español",
  34. fr: "Français",
  35. it: "Italiano",
  36. da: "Dansk",
  37. ja: "日本語",
  38. pl: "Polski",
  39. ru: "Русский",
  40. ar: "العربية",
  41. no: "Norsk",
  42. br: "Português (Brasil)",
  43. th: "ไทย",
  44. tr: "Türkçe",
  45. } satisfies Record<Locale, string>
  46. const TAG = {
  47. en: "en",
  48. zh: "zh-Hans",
  49. zht: "zh-Hant",
  50. ko: "ko",
  51. de: "de",
  52. es: "es",
  53. fr: "fr",
  54. it: "it",
  55. da: "da",
  56. ja: "ja",
  57. pl: "pl",
  58. ru: "ru",
  59. ar: "ar",
  60. no: "no",
  61. br: "pt-BR",
  62. th: "th",
  63. tr: "tr",
  64. } satisfies Record<Locale, string>
  65. export function parseLocale(value: unknown): Locale | null {
  66. if (typeof value !== "string") return null
  67. if ((LOCALES as readonly string[]).includes(value)) return value as Locale
  68. return null
  69. }
  70. export function fromPathname(pathname: string) {
  71. return parseLocale(fix(pathname).split("/")[1])
  72. }
  73. export function strip(pathname: string) {
  74. const locale = fromPathname(pathname)
  75. if (!locale) return fix(pathname)
  76. const next = fix(pathname).slice(locale.length + 1)
  77. if (!next) return "/"
  78. if (next.startsWith("/")) return next
  79. return `/${next}`
  80. }
  81. export function route(locale: Locale, pathname: string) {
  82. const next = strip(pathname)
  83. if (next.startsWith("/docs")) return next
  84. if (next.startsWith("/auth")) return next
  85. if (next.startsWith("/workspace")) return next
  86. if (locale === "en") return next
  87. if (next === "/") return `/${locale}`
  88. return `/${locale}${next}`
  89. }
  90. export function label(locale: Locale) {
  91. return LABEL[locale]
  92. }
  93. export function tag(locale: Locale) {
  94. return TAG[locale]
  95. }
  96. export function dir(locale: Locale) {
  97. if (locale === "ar") return "rtl"
  98. return "ltr"
  99. }
  100. function match(input: string): Locale | null {
  101. const value = input.trim().toLowerCase()
  102. if (!value) return null
  103. if (value.startsWith("zh")) {
  104. if (value.includes("hant") || value.includes("-tw") || value.includes("-hk") || value.includes("-mo")) return "zht"
  105. return "zh"
  106. }
  107. if (value.startsWith("ko")) return "ko"
  108. if (value.startsWith("de")) return "de"
  109. if (value.startsWith("es")) return "es"
  110. if (value.startsWith("fr")) return "fr"
  111. if (value.startsWith("it")) return "it"
  112. if (value.startsWith("da")) return "da"
  113. if (value.startsWith("ja")) return "ja"
  114. if (value.startsWith("pl")) return "pl"
  115. if (value.startsWith("ru")) return "ru"
  116. if (value.startsWith("ar")) return "ar"
  117. if (value.startsWith("tr")) return "tr"
  118. if (value.startsWith("th")) return "th"
  119. if (value.startsWith("pt")) return "br"
  120. if (value.startsWith("no") || value.startsWith("nb") || value.startsWith("nn")) return "no"
  121. if (value.startsWith("en")) return "en"
  122. return null
  123. }
  124. export function detectFromLanguages(languages: readonly string[]) {
  125. for (const language of languages) {
  126. const locale = match(language)
  127. if (locale) return locale
  128. }
  129. return "en" satisfies Locale
  130. }
  131. export function detectFromAcceptLanguage(header: string | null) {
  132. if (!header) return "en" satisfies Locale
  133. const items = header
  134. .split(",")
  135. .map((raw) => raw.trim())
  136. .filter(Boolean)
  137. .map((raw) => {
  138. const parts = raw.split(";").map((x) => x.trim())
  139. const lang = parts[0] ?? ""
  140. const q = parts
  141. .slice(1)
  142. .find((x) => x.startsWith("q="))
  143. ?.slice(2)
  144. return {
  145. lang,
  146. q: q ? Number.parseFloat(q) : 1,
  147. }
  148. })
  149. .sort((a, b) => b.q - a.q)
  150. for (const item of items) {
  151. if (!item.lang || item.lang === "*") continue
  152. const locale = match(item.lang)
  153. if (locale) return locale
  154. }
  155. return "en" satisfies Locale
  156. }
  157. export function localeFromCookieHeader(header: string | null) {
  158. if (!header) return null
  159. const raw = header
  160. .split(";")
  161. .map((x) => x.trim())
  162. .find((x) => x.startsWith(`${LOCALE_COOKIE}=`))
  163. ?.slice(`${LOCALE_COOKIE}=`.length)
  164. if (!raw) return null
  165. return parseLocale(decodeURIComponent(raw))
  166. }
  167. export function localeFromRequest(request: Request) {
  168. const fromHeader = parseLocale(request.headers.get(LOCALE_HEADER))
  169. if (fromHeader) return fromHeader
  170. const fromPath = fromPathname(new URL(request.url).pathname)
  171. if (fromPath) return fromPath
  172. return (
  173. localeFromCookieHeader(request.headers.get("cookie")) ??
  174. detectFromAcceptLanguage(request.headers.get("accept-language"))
  175. )
  176. }
  177. export function cookie(locale: Locale) {
  178. return `${LOCALE_COOKIE}=${encodeURIComponent(locale)}; Path=/; Max-Age=31536000; SameSite=Lax`
  179. }
  180. export function clearCookie() {
  181. return `${LOCALE_COOKIE}=; Path=/; Max-Age=0; SameSite=Lax`
  182. }