language.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import * as i18n from "@solid-primitives/i18n"
  2. import { createEffect, createMemo } from "solid-js"
  3. import { createStore } from "solid-js/store"
  4. import { createSimpleContext } from "@opencode-ai/ui/context"
  5. import { Persist, persisted } from "@/utils/persist"
  6. import { dict as en } from "@/i18n/en"
  7. import { dict as zh } from "@/i18n/zh"
  8. import { dict as zht } from "@/i18n/zht"
  9. import { dict as ko } from "@/i18n/ko"
  10. import { dict as de } from "@/i18n/de"
  11. import { dict as es } from "@/i18n/es"
  12. import { dict as fr } from "@/i18n/fr"
  13. import { dict as da } from "@/i18n/da"
  14. import { dict as ja } from "@/i18n/ja"
  15. import { dict as pl } from "@/i18n/pl"
  16. import { dict as ru } from "@/i18n/ru"
  17. import { dict as ar } from "@/i18n/ar"
  18. import { dict as no } from "@/i18n/no"
  19. import { dict as br } from "@/i18n/br"
  20. import { dict as th } from "@/i18n/th"
  21. import { dict as bs } from "@/i18n/bs"
  22. import { dict as tr } from "@/i18n/tr"
  23. import { dict as uiEn } from "@opencode-ai/ui/i18n/en"
  24. import { dict as uiZh } from "@opencode-ai/ui/i18n/zh"
  25. import { dict as uiZht } from "@opencode-ai/ui/i18n/zht"
  26. import { dict as uiKo } from "@opencode-ai/ui/i18n/ko"
  27. import { dict as uiDe } from "@opencode-ai/ui/i18n/de"
  28. import { dict as uiEs } from "@opencode-ai/ui/i18n/es"
  29. import { dict as uiFr } from "@opencode-ai/ui/i18n/fr"
  30. import { dict as uiDa } from "@opencode-ai/ui/i18n/da"
  31. import { dict as uiJa } from "@opencode-ai/ui/i18n/ja"
  32. import { dict as uiPl } from "@opencode-ai/ui/i18n/pl"
  33. import { dict as uiRu } from "@opencode-ai/ui/i18n/ru"
  34. import { dict as uiAr } from "@opencode-ai/ui/i18n/ar"
  35. import { dict as uiNo } from "@opencode-ai/ui/i18n/no"
  36. import { dict as uiBr } from "@opencode-ai/ui/i18n/br"
  37. import { dict as uiTh } from "@opencode-ai/ui/i18n/th"
  38. import { dict as uiBs } from "@opencode-ai/ui/i18n/bs"
  39. import { dict as uiTr } from "@opencode-ai/ui/i18n/tr"
  40. export type Locale =
  41. | "en"
  42. | "zh"
  43. | "zht"
  44. | "ko"
  45. | "de"
  46. | "es"
  47. | "fr"
  48. | "da"
  49. | "ja"
  50. | "pl"
  51. | "ru"
  52. | "ar"
  53. | "no"
  54. | "br"
  55. | "th"
  56. | "bs"
  57. | "tr"
  58. type RawDictionary = typeof en & typeof uiEn
  59. type Dictionary = i18n.Flatten<RawDictionary>
  60. function cookie(locale: Locale) {
  61. return `oc_locale=${encodeURIComponent(locale)}; Path=/; Max-Age=31536000; SameSite=Lax`
  62. }
  63. const LOCALES: readonly Locale[] = [
  64. "en",
  65. "zh",
  66. "zht",
  67. "ko",
  68. "de",
  69. "es",
  70. "fr",
  71. "da",
  72. "ja",
  73. "pl",
  74. "ru",
  75. "bs",
  76. "ar",
  77. "no",
  78. "br",
  79. "th",
  80. "tr",
  81. ]
  82. const INTL: Record<Locale, string> = {
  83. en: "en",
  84. zh: "zh-Hans",
  85. zht: "zh-Hant",
  86. ko: "ko",
  87. de: "de",
  88. es: "es",
  89. fr: "fr",
  90. da: "da",
  91. ja: "ja",
  92. pl: "pl",
  93. ru: "ru",
  94. ar: "ar",
  95. no: "nb-NO",
  96. br: "pt-BR",
  97. th: "th",
  98. bs: "bs",
  99. tr: "tr",
  100. }
  101. const LABEL_KEY: Record<Locale, keyof Dictionary> = {
  102. en: "language.en",
  103. zh: "language.zh",
  104. zht: "language.zht",
  105. ko: "language.ko",
  106. de: "language.de",
  107. es: "language.es",
  108. fr: "language.fr",
  109. da: "language.da",
  110. ja: "language.ja",
  111. pl: "language.pl",
  112. ru: "language.ru",
  113. ar: "language.ar",
  114. no: "language.no",
  115. br: "language.br",
  116. th: "language.th",
  117. bs: "language.bs",
  118. tr: "language.tr",
  119. }
  120. const base = i18n.flatten({ ...en, ...uiEn })
  121. const DICT: Record<Locale, Dictionary> = {
  122. en: base,
  123. zh: { ...base, ...i18n.flatten({ ...zh, ...uiZh }) },
  124. zht: { ...base, ...i18n.flatten({ ...zht, ...uiZht }) },
  125. ko: { ...base, ...i18n.flatten({ ...ko, ...uiKo }) },
  126. de: { ...base, ...i18n.flatten({ ...de, ...uiDe }) },
  127. es: { ...base, ...i18n.flatten({ ...es, ...uiEs }) },
  128. fr: { ...base, ...i18n.flatten({ ...fr, ...uiFr }) },
  129. da: { ...base, ...i18n.flatten({ ...da, ...uiDa }) },
  130. ja: { ...base, ...i18n.flatten({ ...ja, ...uiJa }) },
  131. pl: { ...base, ...i18n.flatten({ ...pl, ...uiPl }) },
  132. ru: { ...base, ...i18n.flatten({ ...ru, ...uiRu }) },
  133. ar: { ...base, ...i18n.flatten({ ...ar, ...uiAr }) },
  134. no: { ...base, ...i18n.flatten({ ...no, ...uiNo }) },
  135. br: { ...base, ...i18n.flatten({ ...br, ...uiBr }) },
  136. th: { ...base, ...i18n.flatten({ ...th, ...uiTh }) },
  137. bs: { ...base, ...i18n.flatten({ ...bs, ...uiBs }) },
  138. tr: { ...base, ...i18n.flatten({ ...tr, ...uiTr }) },
  139. }
  140. const localeMatchers: Array<{ locale: Locale; match: (language: string) => boolean }> = [
  141. { locale: "zht", match: (language) => language.startsWith("zh") && language.includes("hant") },
  142. { locale: "zh", match: (language) => language.startsWith("zh") },
  143. { locale: "ko", match: (language) => language.startsWith("ko") },
  144. { locale: "de", match: (language) => language.startsWith("de") },
  145. { locale: "es", match: (language) => language.startsWith("es") },
  146. { locale: "fr", match: (language) => language.startsWith("fr") },
  147. { locale: "da", match: (language) => language.startsWith("da") },
  148. { locale: "ja", match: (language) => language.startsWith("ja") },
  149. { locale: "pl", match: (language) => language.startsWith("pl") },
  150. { locale: "ru", match: (language) => language.startsWith("ru") },
  151. { locale: "ar", match: (language) => language.startsWith("ar") },
  152. {
  153. locale: "no",
  154. match: (language) => language.startsWith("no") || language.startsWith("nb") || language.startsWith("nn"),
  155. },
  156. { locale: "br", match: (language) => language.startsWith("pt") },
  157. { locale: "th", match: (language) => language.startsWith("th") },
  158. { locale: "bs", match: (language) => language.startsWith("bs") },
  159. { locale: "tr", match: (language) => language.startsWith("tr") },
  160. ]
  161. type ParityKey = "command.session.previous.unseen" | "command.session.next.unseen"
  162. const PARITY_CHECK: Record<Exclude<Locale, "en">, Record<ParityKey, string>> = {
  163. zh,
  164. zht,
  165. ko,
  166. de,
  167. es,
  168. fr,
  169. da,
  170. ja,
  171. pl,
  172. ru,
  173. ar,
  174. no,
  175. br,
  176. th,
  177. bs,
  178. tr,
  179. }
  180. void PARITY_CHECK
  181. function detectLocale(): Locale {
  182. if (typeof navigator !== "object") return "en"
  183. const languages = navigator.languages?.length ? navigator.languages : [navigator.language]
  184. for (const language of languages) {
  185. if (!language) continue
  186. const normalized = language.toLowerCase()
  187. const match = localeMatchers.find((entry) => entry.match(normalized))
  188. if (match) return match.locale
  189. }
  190. return "en"
  191. }
  192. function normalizeLocale(value: string): Locale {
  193. return LOCALES.includes(value as Locale) ? (value as Locale) : "en"
  194. }
  195. export const { use: useLanguage, provider: LanguageProvider } = createSimpleContext({
  196. name: "Language",
  197. init: () => {
  198. const [store, setStore, _, ready] = persisted(
  199. Persist.global("language", ["language.v1"]),
  200. createStore({
  201. locale: detectLocale() as Locale,
  202. }),
  203. )
  204. const locale = createMemo<Locale>(() => normalizeLocale(store.locale))
  205. const intl = createMemo(() => INTL[locale()])
  206. const dict = createMemo<Dictionary>(() => DICT[locale()])
  207. const t = i18n.translator(dict, i18n.resolveTemplate)
  208. const label = (value: Locale) => t(LABEL_KEY[value])
  209. createEffect(() => {
  210. if (typeof document !== "object") return
  211. document.documentElement.lang = locale()
  212. document.cookie = cookie(locale())
  213. })
  214. return {
  215. ready,
  216. locale,
  217. intl,
  218. locales: LOCALES,
  219. label,
  220. t,
  221. setLocale(next: Locale) {
  222. setStore("locale", normalizeLocale(next))
  223. },
  224. }
  225. },
  226. })