GeezerVison is showing github.com — refactor(site): share locale helpers between site and data (#66772) · erickwill/opencode@9702130
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bun.lock +13Lines changed: 13 additions & 0 deletions Collapse file Copy file name to clipboardExpand all lines: packages/data/app/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | "dependencies": { | 24 | 24 | "@kobalte/core": "catalog:", | 25 | 25 | "@opencode/nav-menus": "workspace:*", | | 26 | + "@opencode/locale": "workspace:*", | 26 | 27 | "@opencode/data-core": "workspace:*", | 27 | 28 | "@opencode/ui": "workspace:*", | 28 | 29 | "@resvg/resvg-wasm": "2.6.2", | |
Collapse file Copy file name to clipboard +17-113Lines changed: 17 additions & 113 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 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 | | - "uk", | 15 | | - "ar", | 16 | | - "no", | 17 | | - "br", | 18 | | - "th", | 19 | | - "tr", | 20 | | -] as const | 21 | | - | 22 | | -export type Locale = (typeof LOCALES)[number] | 23 | | - | 24 | | -export const LOCALE_COOKIE = "oc_locale" as const | 25 | | -export const LOCALE_HEADER = "x-opencode-locale" as const | 26 | | - | 27 | | -function fix(pathname: string) { | 28 | | - if (pathname.startsWith("/")) return pathname | 29 | | - return `/${pathname}` | 30 | | -} | 31 | | - | 32 | | -const LABEL = { | 33 | | - en: "English", | 34 | | - zh: "简体中文", | 35 | | - zht: "繁體中文", | 36 | | - ko: "한국어", | 37 | | - de: "Deutsch", | 38 | | - es: "Español", | 39 | | - fr: "Français", | 40 | | - it: "Italiano", | 41 | | - da: "Dansk", | 42 | | - ja: "日本語", | 43 | | - pl: "Polski", | 44 | | - ru: "Русский", | 45 | | - uk: "Українська", | 46 | | - ar: "العربية", | 47 | | - no: "Norsk", | 48 | | - br: "Português (Brasil)", | 49 | | - th: "ไทย", | 50 | | - tr: "Türkçe", | 51 | | -} satisfies Record<Locale, string> | 52 | | - | 53 | | -const TAG = { | 54 | | - en: "en", | 55 | | - zh: "zh-Hans", | 56 | | - zht: "zh-Hant", | 57 | | - ko: "ko", | 58 | | - de: "de", | 59 | | - es: "es", | 60 | | - fr: "fr", | 61 | | - it: "it", | 62 | | - da: "da", | 63 | | - ja: "ja", | 64 | | - pl: "pl", | 65 | | - ru: "ru", | 66 | | - uk: "uk", | 67 | | - ar: "ar", | 68 | | - no: "no", | 69 | | - br: "pt-BR", | 70 | | - th: "th", | 71 | | - tr: "tr", | 72 | | -} satisfies Record<Locale, string> | | 1 | +import { fix, fromPathname, LOCALE_HEADER, parseLocale, strip, type Locale } from "@opencode/locale" | | 2 | + | | 3 | +export { | | 4 | + LOCALES, | | 5 | + LOCALE_COOKIE, | | 6 | + LOCALE_HEADER, | | 7 | + clearCookie, | | 8 | + cookie, | | 9 | + dir, | | 10 | + fromPathname, | | 11 | + label, | | 12 | + parseLocale, | | 13 | + strip, | | 14 | + tag, | | 15 | +} from "@opencode/locale" | | 16 | + | | 17 | +export type { Locale } from "@opencode/locale" | 73 | 18 |
| 74 | 19 | const DOCS = { | 75 | 20 | en: "root", | @@ -176,16 +121,6 @@ function docs(locale: Locale, pathname: string) { | 176 | 121 | return `/docs/${value}${next.path.slice("/docs".length)}${next.suffix}` | 177 | 122 | } | 178 | 123 |
| 179 | | -export function parseLocale(value: unknown): Locale | null { | 180 | | - if (typeof value !== "string") return null | 181 | | - if ((LOCALES as readonly string[]).includes(value)) return value as Locale | 182 | | - return null | 183 | | -} | 184 | | - | 185 | | -export function fromPathname(pathname: string) { | 186 | | - return parseLocale(fix(pathname).split("/")[1]) | 187 | | -} | 188 | | - | 189 | 124 | function fromDocsPathname(pathname: string) { | 190 | 125 | const next = fix(pathname) | 191 | 126 | const value = next.split("/")[2]?.toLowerCase() | @@ -195,16 +130,6 @@ function fromDocsPathname(pathname: string) { | 195 | 130 | return DOCS_LOCALE[value as keyof typeof DOCS_LOCALE] | 196 | 131 | } | 197 | 132 |
| 198 | | -export function strip(pathname: string) { | 199 | | - const locale = fromPathname(pathname) | 200 | | - if (!locale) return fix(pathname) | 201 | | - | 202 | | - const next = fix(pathname).slice(locale.length + 1) | 203 | | - if (!next) return "/" | 204 | | - if (next.startsWith("/")) return next | 205 | | - return `/${next}` | 206 | | -} | 207 | | - | 208 | 133 | export function route(locale: Locale, pathname: string) { | 209 | 134 | const next = strip(pathname) | 210 | 135 | if (next.startsWith("/docs")) return docs(locale, next) | @@ -217,19 +142,6 @@ export function route(locale: Locale, pathname: string) { | 217 | 142 | return `/${locale}${next}` | 218 | 143 | } | 219 | 144 |
| 220 | | -export function label(locale: Locale) { | 221 | | - return LABEL[locale] | 222 | | -} | 223 | | - | 224 | | -export function tag(locale: Locale) { | 225 | | - return TAG[locale] | 226 | | -} | 227 | | - | 228 | | -export function dir(locale: Locale) { | 229 | | - if (locale === "ar") return "rtl" | 230 | | - return "ltr" | 231 | | -} | 232 | | - | 233 | 145 | export function localeFromRequest(request: Request) { | 234 | 146 | const fromHeader = parseLocale(request.headers.get(LOCALE_HEADER)) | 235 | 147 | if (fromHeader) return fromHeader | @@ -243,11 +155,3 @@ export function localeFromRequest(request: Request) { | 243 | 155 | // Unprefixed pages are English, like the rest of the site. | 244 | 156 | return "en" satisfies Locale | 245 | 157 | } | 246 | | - | 247 | | -export function cookie(locale: Locale) { | 248 | | - return `${LOCALE_COOKIE}=${encodeURIComponent(locale)}; Path=/; Max-Age=31536000; SameSite=Lax` | 249 | | -} | 250 | | - | 251 | | -export function clearCookie() { | 252 | | - return `${LOCALE_COOKIE}=; Path=/; Max-Age=0; SameSite=Lax` | 253 | | -} |
Collapse file Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +[test] | | 2 | +root = "./test" |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "$schema": "https://json.schemastore.org/package.json", | | 3 | + "name": "@opencode/locale", | | 4 | + "version": "2.4.1", | | 5 | + "private": true, | | 6 | + "type": "module", | | 7 | + "license": "MIT", | | 8 | + "exports": { | | 9 | + ".": "./src/index.ts" | | 10 | + }, | | 11 | + "scripts": { | | 12 | + "typecheck": "bun check --no-script", | | 13 | + "test": "bun test" | | 14 | + }, | | 15 | + "devDependencies": { | | 16 | + "@types/bun": "catalog:", | | 17 | + "@typescript/native": "catalog:", | | 18 | + "typescript": "catalog:" | | 19 | + } | | 20 | +} |
|