GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor(site): share locale helpers between site and data (#66772) · erickwill/opencode@9702130

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • bun.lock
  • packages
    • data/app
      • package.json
      • src/lib
        • locale.ts
    • locale
      • bunfig.toml
      • package.json
      • src
        • index.ts
      • test
        • locale.test.ts
      • tsconfig.json
  • services/site
    • package.json
    • src/lib
      • language.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎bun.lock‎

Copy file name to clipboardExpand all lines: bun.lock
+13Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎packages/data/app/package.json‎

Copy file name to clipboardExpand all lines: packages/data/app/package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dependencies": {
2424
"@kobalte/core": "catalog:",
2525
"@opencode/nav-menus": "workspace:*",
26+
"@opencode/locale": "workspace:*",
2627
"@opencode/data-core": "workspace:*",
2728
"@opencode/ui": "workspace:*",
2829
"@resvg/resvg-wasm": "2.6.2",
Collapse file

‎packages/data/app/src/lib/locale.ts‎

Copy file name to clipboard
+17-113Lines changed: 17 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,20 @@
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"
7318

7419
const DOCS = {
7520
en: "root",
@@ -176,16 +121,6 @@ function docs(locale: Locale, pathname: string) {
176121
return `/docs/${value}${next.path.slice("/docs".length)}${next.suffix}`
177122
}
178123

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-
189124
function fromDocsPathname(pathname: string) {
190125
const next = fix(pathname)
191126
const value = next.split("/")[2]?.toLowerCase()
@@ -195,16 +130,6 @@ function fromDocsPathname(pathname: string) {
195130
return DOCS_LOCALE[value as keyof typeof DOCS_LOCALE]
196131
}
197132

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-
208133
export function route(locale: Locale, pathname: string) {
209134
const next = strip(pathname)
210135
if (next.startsWith("/docs")) return docs(locale, next)
@@ -217,19 +142,6 @@ export function route(locale: Locale, pathname: string) {
217142
return `/${locale}${next}`
218143
}
219144

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-
233145
export function localeFromRequest(request: Request) {
234146
const fromHeader = parseLocale(request.headers.get(LOCALE_HEADER))
235147
if (fromHeader) return fromHeader
@@ -243,11 +155,3 @@ export function localeFromRequest(request: Request) {
243155
// Unprefixed pages are English, like the rest of the site.
244156
return "en" satisfies Locale
245157
}
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

‎packages/locale/bunfig.toml‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[test]
2+
root = "./test"
Collapse file

‎packages/locale/package.json‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.