language.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. const DOCS = {
  66. en: "root",
  67. zh: "zh-cn",
  68. zht: "zh-tw",
  69. ko: "ko",
  70. de: "de",
  71. es: "es",
  72. fr: "fr",
  73. it: "it",
  74. da: "da",
  75. ja: "ja",
  76. pl: "pl",
  77. ru: "ru",
  78. ar: "ar",
  79. no: "nb",
  80. br: "pt-br",
  81. th: "th",
  82. tr: "tr",
  83. } satisfies Record<Locale, string>
  84. const DOCS_SEGMENT = new Set([
  85. "ar",
  86. "bs",
  87. "da",
  88. "de",
  89. "es",
  90. "fr",
  91. "it",
  92. "ja",
  93. "ko",
  94. "nb",
  95. "pl",
  96. "pt-br",
  97. "ru",
  98. "th",
  99. "tr",
  100. "zh-cn",
  101. "zh-tw",
  102. ])
  103. function suffix(pathname: string) {
  104. const index = pathname.search(/[?#]/)
  105. if (index === -1) {
  106. return {
  107. path: fix(pathname),
  108. suffix: "",
  109. }
  110. }
  111. return {
  112. path: fix(pathname.slice(0, index)),
  113. suffix: pathname.slice(index),
  114. }
  115. }
  116. export function docs(locale: Locale, pathname: string) {
  117. const value = DOCS[locale]
  118. const next = suffix(pathname)
  119. if (next.path !== "/docs" && next.path !== "/docs/" && !next.path.startsWith("/docs/")) {
  120. return `${next.path}${next.suffix}`
  121. }
  122. if (value === "root") return `${next.path}${next.suffix}`
  123. if (next.path === "/docs") return `/docs/${value}${next.suffix}`
  124. if (next.path === "/docs/") return `/docs/${value}/${next.suffix}`
  125. const head = next.path.slice("/docs/".length).split("/")[0] ?? ""
  126. if (!head) return `/docs/${value}/${next.suffix}`
  127. if (DOCS_SEGMENT.has(head)) return `${next.path}${next.suffix}`
  128. if (head.startsWith("_")) return `${next.path}${next.suffix}`
  129. if (head.includes(".")) return `${next.path}${next.suffix}`
  130. return `/docs/${value}${next.path.slice("/docs".length)}${next.suffix}`
  131. }
  132. export function parseLocale(value: unknown): Locale | null {
  133. if (typeof value !== "string") return null
  134. if ((LOCALES as readonly string[]).includes(value)) return value as Locale
  135. return null
  136. }
  137. export function fromPathname(pathname: string) {
  138. return parseLocale(fix(pathname).split("/")[1])
  139. }
  140. export function strip(pathname: string) {
  141. const locale = fromPathname(pathname)
  142. if (!locale) return fix(pathname)
  143. const next = fix(pathname).slice(locale.length + 1)
  144. if (!next) return "/"
  145. if (next.startsWith("/")) return next
  146. return `/${next}`
  147. }
  148. export function route(locale: Locale, pathname: string) {
  149. const next = strip(pathname)
  150. if (next.startsWith("/docs")) return docs(locale, next)
  151. if (next.startsWith("/auth")) return next
  152. if (next.startsWith("/workspace")) return next
  153. if (locale === "en") return next
  154. if (next === "/") return `/${locale}`
  155. return `/${locale}${next}`
  156. }
  157. export function label(locale: Locale) {
  158. return LABEL[locale]
  159. }
  160. export function tag(locale: Locale) {
  161. return TAG[locale]
  162. }
  163. export function dir(locale: Locale) {
  164. if (locale === "ar") return "rtl"
  165. return "ltr"
  166. }
  167. function match(input: string): Locale | null {
  168. const value = input.trim().toLowerCase()
  169. if (!value) return null
  170. if (value.startsWith("zh")) {
  171. if (value.includes("hant") || value.includes("-tw") || value.includes("-hk") || value.includes("-mo")) return "zht"
  172. return "zh"
  173. }
  174. if (value.startsWith("ko")) return "ko"
  175. if (value.startsWith("de")) return "de"
  176. if (value.startsWith("es")) return "es"
  177. if (value.startsWith("fr")) return "fr"
  178. if (value.startsWith("it")) return "it"
  179. if (value.startsWith("da")) return "da"
  180. if (value.startsWith("ja")) return "ja"
  181. if (value.startsWith("pl")) return "pl"
  182. if (value.startsWith("ru")) return "ru"
  183. if (value.startsWith("ar")) return "ar"
  184. if (value.startsWith("tr")) return "tr"
  185. if (value.startsWith("th")) return "th"
  186. if (value.startsWith("pt")) return "br"
  187. if (value.startsWith("no") || value.startsWith("nb") || value.startsWith("nn")) return "no"
  188. if (value.startsWith("en")) return "en"
  189. return null
  190. }
  191. export function detectFromLanguages(languages: readonly string[]) {
  192. for (const language of languages) {
  193. const locale = match(language)
  194. if (locale) return locale
  195. }
  196. return "en" satisfies Locale
  197. }
  198. export function detectFromAcceptLanguage(header: string | null) {
  199. if (!header) return "en" satisfies Locale
  200. const items = header
  201. .split(",")
  202. .map((raw) => raw.trim())
  203. .filter(Boolean)
  204. .map((raw) => {
  205. const parts = raw.split(";").map((x) => x.trim())
  206. const lang = parts[0] ?? ""
  207. const q = parts
  208. .slice(1)
  209. .find((x) => x.startsWith("q="))
  210. ?.slice(2)
  211. return {
  212. lang,
  213. q: q ? Number.parseFloat(q) : 1,
  214. }
  215. })
  216. .sort((a, b) => b.q - a.q)
  217. for (const item of items) {
  218. if (!item.lang || item.lang === "*") continue
  219. const locale = match(item.lang)
  220. if (locale) return locale
  221. }
  222. return "en" satisfies Locale
  223. }
  224. export function localeFromCookieHeader(header: string | null) {
  225. if (!header) return null
  226. const raw = header
  227. .split(";")
  228. .map((x) => x.trim())
  229. .find((x) => x.startsWith(`${LOCALE_COOKIE}=`))
  230. ?.slice(`${LOCALE_COOKIE}=`.length)
  231. if (!raw) return null
  232. return parseLocale(decodeURIComponent(raw))
  233. }
  234. export function localeFromRequest(request: Request) {
  235. const fromHeader = parseLocale(request.headers.get(LOCALE_HEADER))
  236. if (fromHeader) return fromHeader
  237. const fromPath = fromPathname(new URL(request.url).pathname)
  238. if (fromPath) return fromPath
  239. return (
  240. localeFromCookieHeader(request.headers.get("cookie")) ??
  241. detectFromAcceptLanguage(request.headers.get("accept-language"))
  242. )
  243. }
  244. export function cookie(locale: Locale) {
  245. return `${LOCALE_COOKIE}=${encodeURIComponent(locale)}; Path=/; Max-Age=31536000; SameSite=Lax`
  246. }
  247. export function clearCookie() {
  248. return `${LOCALE_COOKIE}=; Path=/; Max-Age=0; SameSite=Lax`
  249. }