language.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import * as i18n from "@solid-primitives/i18n"
  2. import { createEffect, createMemo, createResource } 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 uiEn } from "@opencode-ai/ui/i18n/en"
  8. export type Locale =
  9. | "en"
  10. | "zh"
  11. | "zht"
  12. | "ko"
  13. | "de"
  14. | "es"
  15. | "fr"
  16. | "da"
  17. | "ja"
  18. | "pl"
  19. | "ru"
  20. | "ar"
  21. | "no"
  22. | "br"
  23. | "th"
  24. | "bs"
  25. | "tr"
  26. type RawDictionary = typeof en & typeof uiEn
  27. type Dictionary = i18n.Flatten<RawDictionary>
  28. type Source = { dict: Record<string, string> }
  29. function cookie(locale: Locale) {
  30. return `oc_locale=${encodeURIComponent(locale)}; Path=/; Max-Age=31536000; SameSite=Lax`
  31. }
  32. const LOCALES: readonly Locale[] = [
  33. "en",
  34. "zh",
  35. "zht",
  36. "ko",
  37. "de",
  38. "es",
  39. "fr",
  40. "da",
  41. "ja",
  42. "pl",
  43. "ru",
  44. "bs",
  45. "ar",
  46. "no",
  47. "br",
  48. "th",
  49. "tr",
  50. ]
  51. const INTL: Record<Locale, string> = {
  52. en: "en",
  53. zh: "zh-Hans",
  54. zht: "zh-Hant",
  55. ko: "ko",
  56. de: "de",
  57. es: "es",
  58. fr: "fr",
  59. da: "da",
  60. ja: "ja",
  61. pl: "pl",
  62. ru: "ru",
  63. ar: "ar",
  64. no: "nb-NO",
  65. br: "pt-BR",
  66. th: "th",
  67. bs: "bs",
  68. tr: "tr",
  69. }
  70. const LABEL_KEY: Record<Locale, keyof Dictionary> = {
  71. en: "language.en",
  72. zh: "language.zh",
  73. zht: "language.zht",
  74. ko: "language.ko",
  75. de: "language.de",
  76. es: "language.es",
  77. fr: "language.fr",
  78. da: "language.da",
  79. ja: "language.ja",
  80. pl: "language.pl",
  81. ru: "language.ru",
  82. ar: "language.ar",
  83. no: "language.no",
  84. br: "language.br",
  85. th: "language.th",
  86. bs: "language.bs",
  87. tr: "language.tr",
  88. }
  89. const base = i18n.flatten({ ...en, ...uiEn })
  90. const dicts = new Map<Locale, Dictionary>([["en", base]])
  91. const merge = (app: Promise<Source>, ui: Promise<Source>) =>
  92. Promise.all([app, ui]).then(([a, b]) => ({ ...base, ...i18n.flatten({ ...a.dict, ...b.dict }) }) as Dictionary)
  93. const loaders: Record<Exclude<Locale, "en">, () => Promise<Dictionary>> = {
  94. zh: () => merge(import("@/i18n/zh"), import("@opencode-ai/ui/i18n/zh")),
  95. zht: () => merge(import("@/i18n/zht"), import("@opencode-ai/ui/i18n/zht")),
  96. ko: () => merge(import("@/i18n/ko"), import("@opencode-ai/ui/i18n/ko")),
  97. de: () => merge(import("@/i18n/de"), import("@opencode-ai/ui/i18n/de")),
  98. es: () => merge(import("@/i18n/es"), import("@opencode-ai/ui/i18n/es")),
  99. fr: () => merge(import("@/i18n/fr"), import("@opencode-ai/ui/i18n/fr")),
  100. da: () => merge(import("@/i18n/da"), import("@opencode-ai/ui/i18n/da")),
  101. ja: () => merge(import("@/i18n/ja"), import("@opencode-ai/ui/i18n/ja")),
  102. pl: () => merge(import("@/i18n/pl"), import("@opencode-ai/ui/i18n/pl")),
  103. ru: () => merge(import("@/i18n/ru"), import("@opencode-ai/ui/i18n/ru")),
  104. ar: () => merge(import("@/i18n/ar"), import("@opencode-ai/ui/i18n/ar")),
  105. no: () => merge(import("@/i18n/no"), import("@opencode-ai/ui/i18n/no")),
  106. br: () => merge(import("@/i18n/br"), import("@opencode-ai/ui/i18n/br")),
  107. th: () => merge(import("@/i18n/th"), import("@opencode-ai/ui/i18n/th")),
  108. bs: () => merge(import("@/i18n/bs"), import("@opencode-ai/ui/i18n/bs")),
  109. tr: () => merge(import("@/i18n/tr"), import("@opencode-ai/ui/i18n/tr")),
  110. }
  111. function loadDict(locale: Locale) {
  112. const hit = dicts.get(locale)
  113. if (hit) return Promise.resolve(hit)
  114. if (locale === "en") return Promise.resolve(base)
  115. const load = loaders[locale]
  116. return load().then((next: Dictionary) => {
  117. dicts.set(locale, next)
  118. return next
  119. })
  120. }
  121. export function loadLocaleDict(locale: Locale) {
  122. return loadDict(locale).then(() => undefined)
  123. }
  124. const localeMatchers: Array<{ locale: Locale; match: (language: string) => boolean }> = [
  125. { locale: "en", match: (language) => language.startsWith("en") },
  126. { locale: "zht", match: (language) => language.startsWith("zh") && language.includes("hant") },
  127. { locale: "zh", match: (language) => language.startsWith("zh") },
  128. { locale: "ko", match: (language) => language.startsWith("ko") },
  129. { locale: "de", match: (language) => language.startsWith("de") },
  130. { locale: "es", match: (language) => language.startsWith("es") },
  131. { locale: "fr", match: (language) => language.startsWith("fr") },
  132. { locale: "da", match: (language) => language.startsWith("da") },
  133. { locale: "ja", match: (language) => language.startsWith("ja") },
  134. { locale: "pl", match: (language) => language.startsWith("pl") },
  135. { locale: "ru", match: (language) => language.startsWith("ru") },
  136. { locale: "ar", match: (language) => language.startsWith("ar") },
  137. {
  138. locale: "no",
  139. match: (language) => language.startsWith("no") || language.startsWith("nb") || language.startsWith("nn"),
  140. },
  141. { locale: "br", match: (language) => language.startsWith("pt") },
  142. { locale: "th", match: (language) => language.startsWith("th") },
  143. { locale: "bs", match: (language) => language.startsWith("bs") },
  144. { locale: "tr", match: (language) => language.startsWith("tr") },
  145. ]
  146. function detectLocale(): Locale {
  147. if (typeof navigator !== "object") return "en"
  148. const languages = navigator.languages?.length ? navigator.languages : [navigator.language]
  149. for (const language of languages) {
  150. if (!language) continue
  151. const normalized = language.toLowerCase()
  152. const match = localeMatchers.find((entry) => entry.match(normalized))
  153. if (match) return match.locale
  154. }
  155. return "en"
  156. }
  157. export function normalizeLocale(value: string): Locale {
  158. return LOCALES.includes(value as Locale) ? (value as Locale) : "en"
  159. }
  160. function readStoredLocale() {
  161. if (typeof localStorage !== "object") return
  162. try {
  163. const raw = localStorage.getItem("opencode.global.dat:language")
  164. if (!raw) return
  165. const next = JSON.parse(raw) as { locale?: string }
  166. if (typeof next?.locale !== "string") return
  167. return normalizeLocale(next.locale)
  168. } catch {
  169. return
  170. }
  171. }
  172. const warm = readStoredLocale() ?? detectLocale()
  173. if (warm !== "en") void loadDict(warm)
  174. export const { use: useLanguage, provider: LanguageProvider } = createSimpleContext({
  175. name: "Language",
  176. init: (props: { locale?: Locale }) => {
  177. const initial = props.locale ?? readStoredLocale() ?? detectLocale()
  178. const [store, setStore, _, ready] = persisted(
  179. Persist.global("language", ["language.v1"]),
  180. createStore({
  181. locale: initial,
  182. }),
  183. )
  184. const locale = createMemo<Locale>(() => normalizeLocale(store.locale))
  185. const intl = createMemo(() => INTL[locale()])
  186. const [dict] = createResource(locale, loadDict, {
  187. initialValue: dicts.get(initial) ?? base,
  188. })
  189. const t = i18n.translator(() => dict() ?? base, i18n.resolveTemplate) as (
  190. key: keyof Dictionary,
  191. params?: Record<string, string | number | boolean>,
  192. ) => string
  193. const label = (value: Locale) => t(LABEL_KEY[value])
  194. createEffect(() => {
  195. if (typeof document !== "object") return
  196. document.documentElement.lang = locale()
  197. document.cookie = cookie(locale())
  198. })
  199. return {
  200. ready,
  201. locale,
  202. intl,
  203. locales: LOCALES,
  204. label,
  205. t,
  206. setLocale(next: Locale) {
  207. setStore("locale", normalizeLocale(next))
  208. },
  209. }
  210. },
  211. })