language.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. const DOCS_LOCALE = {
  104. ar: "ar",
  105. da: "da",
  106. de: "de",
  107. en: "en",
  108. es: "es",
  109. fr: "fr",
  110. it: "it",
  111. ja: "ja",
  112. ko: "ko",
  113. nb: "no",
  114. "pt-br": "br",
  115. root: "en",
  116. ru: "ru",
  117. th: "th",
  118. tr: "tr",
  119. "zh-cn": "zh",
  120. "zh-tw": "zht",
  121. } as const satisfies Record<string, Locale>
  122. function suffix(pathname: string) {
  123. const index = pathname.search(/[?#]/)
  124. if (index === -1) {
  125. return {
  126. path: fix(pathname),
  127. suffix: "",
  128. }
  129. }
  130. return {
  131. path: fix(pathname.slice(0, index)),
  132. suffix: pathname.slice(index),
  133. }
  134. }
  135. export function docs(locale: Locale, pathname: string) {
  136. const value = DOCS[locale]
  137. const next = suffix(pathname)
  138. if (next.path !== "/docs" && next.path !== "/docs/" && !next.path.startsWith("/docs/")) {
  139. return `${next.path}${next.suffix}`
  140. }
  141. if (value === "root") {
  142. if (next.path === "/docs/en") return `/docs${next.suffix}`
  143. if (next.path === "/docs/en/") return `/docs/${next.suffix}`
  144. if (next.path.startsWith("/docs/en/")) return `/docs/${next.path.slice("/docs/en/".length)}${next.suffix}`
  145. return `${next.path}${next.suffix}`
  146. }
  147. if (next.path === "/docs") return `/docs/${value}${next.suffix}`
  148. if (next.path === "/docs/") return `/docs/${value}/${next.suffix}`
  149. const head = next.path.slice("/docs/".length).split("/")[0] ?? ""
  150. if (!head) return `/docs/${value}/${next.suffix}`
  151. if (DOCS_SEGMENT.has(head)) return `${next.path}${next.suffix}`
  152. if (head.startsWith("_")) return `${next.path}${next.suffix}`
  153. if (head.includes(".")) return `${next.path}${next.suffix}`
  154. return `/docs/${value}${next.path.slice("/docs".length)}${next.suffix}`
  155. }
  156. export function parseLocale(value: unknown): Locale | null {
  157. if (typeof value !== "string") return null
  158. if ((LOCALES as readonly string[]).includes(value)) return value as Locale
  159. return null
  160. }
  161. export function fromPathname(pathname: string) {
  162. return parseLocale(fix(pathname).split("/")[1])
  163. }
  164. export function fromDocsPathname(pathname: string) {
  165. const next = fix(pathname)
  166. const value = next.split("/")[2]?.toLowerCase()
  167. if (!value) return null
  168. if (!next.startsWith("/docs/")) return null
  169. if (!(value in DOCS_LOCALE)) return null
  170. return DOCS_LOCALE[value as keyof typeof DOCS_LOCALE]
  171. }
  172. export function strip(pathname: string) {
  173. const locale = fromPathname(pathname)
  174. if (!locale) return fix(pathname)
  175. const next = fix(pathname).slice(locale.length + 1)
  176. if (!next) return "/"
  177. if (next.startsWith("/")) return next
  178. return `/${next}`
  179. }
  180. export function route(locale: Locale, pathname: string) {
  181. const next = strip(pathname)
  182. if (next.startsWith("/docs")) return docs(locale, next)
  183. if (next.startsWith("/auth")) return next
  184. if (next.startsWith("/workspace")) return next
  185. if (locale === "en") return next
  186. if (next === "/") return `/${locale}`
  187. return `/${locale}${next}`
  188. }
  189. export function label(locale: Locale) {
  190. return LABEL[locale]
  191. }
  192. export function tag(locale: Locale) {
  193. return TAG[locale]
  194. }
  195. export function dir(locale: Locale) {
  196. if (locale === "ar") return "rtl"
  197. return "ltr"
  198. }
  199. function match(input: string): Locale | null {
  200. const value = input.trim().toLowerCase()
  201. if (!value) return null
  202. if (value.startsWith("zh")) {
  203. if (value.includes("hant") || value.includes("-tw") || value.includes("-hk") || value.includes("-mo")) return "zht"
  204. return "zh"
  205. }
  206. if (value.startsWith("ko")) return "ko"
  207. if (value.startsWith("de")) return "de"
  208. if (value.startsWith("es")) return "es"
  209. if (value.startsWith("fr")) return "fr"
  210. if (value.startsWith("it")) return "it"
  211. if (value.startsWith("da")) return "da"
  212. if (value.startsWith("ja")) return "ja"
  213. if (value.startsWith("pl")) return "pl"
  214. if (value.startsWith("ru")) return "ru"
  215. if (value.startsWith("ar")) return "ar"
  216. if (value.startsWith("tr")) return "tr"
  217. if (value.startsWith("th")) return "th"
  218. if (value.startsWith("pt")) return "br"
  219. if (value.startsWith("no") || value.startsWith("nb") || value.startsWith("nn")) return "no"
  220. if (value.startsWith("en")) return "en"
  221. return null
  222. }
  223. export function detectFromLanguages(languages: readonly string[]) {
  224. for (const language of languages) {
  225. const locale = match(language)
  226. if (locale) return locale
  227. }
  228. return "en" satisfies Locale
  229. }
  230. export function detectFromAcceptLanguage(header: string | null) {
  231. if (!header) return "en" satisfies Locale
  232. const items = header
  233. .split(",")
  234. .map((raw) => raw.trim())
  235. .filter(Boolean)
  236. .map((raw) => {
  237. const parts = raw.split(";").map((x) => x.trim())
  238. const lang = parts[0] ?? ""
  239. const q = parts
  240. .slice(1)
  241. .find((x) => x.startsWith("q="))
  242. ?.slice(2)
  243. return {
  244. lang,
  245. q: q ? Number.parseFloat(q) : 1,
  246. }
  247. })
  248. .sort((a, b) => b.q - a.q)
  249. for (const item of items) {
  250. if (!item.lang || item.lang === "*") continue
  251. const locale = match(item.lang)
  252. if (locale) return locale
  253. }
  254. return "en" satisfies Locale
  255. }
  256. export function localeFromCookieHeader(header: string | null) {
  257. if (!header) return null
  258. const raw = header
  259. .split(";")
  260. .map((x) => x.trim())
  261. .find((x) => x.startsWith(`${LOCALE_COOKIE}=`))
  262. ?.slice(`${LOCALE_COOKIE}=`.length)
  263. if (!raw) return null
  264. return parseLocale(decodeURIComponent(raw))
  265. }
  266. export function localeFromRequest(request: Request) {
  267. const fromHeader = parseLocale(request.headers.get(LOCALE_HEADER))
  268. if (fromHeader) return fromHeader
  269. const fromPath = fromPathname(new URL(request.url).pathname)
  270. if (fromPath) return fromPath
  271. const fromDocsPath = fromDocsPathname(new URL(request.url).pathname)
  272. if (fromDocsPath) return fromDocsPath
  273. return (
  274. localeFromCookieHeader(request.headers.get("cookie")) ??
  275. detectFromAcceptLanguage(request.headers.get("accept-language"))
  276. )
  277. }
  278. export function cookie(locale: Locale) {
  279. return `${LOCALE_COOKIE}=${encodeURIComponent(locale)}; Path=/; Max-Age=31536000; SameSite=Lax`
  280. }
  281. export function clearCookie() {
  282. return `${LOCALE_COOKIE}=; Path=/; Max-Age=0; SameSite=Lax`
  283. }