diff options
| author | Adam <[email protected]> | 2026-02-06 11:30:40 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-06 11:30:40 -0600 |
| commit | 24cd84cda5522e4607e8e3fb3626f289d7e348f4 (patch) | |
| tree | 81dec57e565c24255e5e6ad2dfac58ea0e770018 /packages/console/app/src/lib | |
| parent | 8069197329d2d1b958d8e7f63daaf9662a97027d (diff) | |
| download | opencode-24cd84cda5522e4607e8e3fb3626f289d7e348f4.tar.gz opencode-24cd84cda5522e4607e8e3fb3626f289d7e348f4.zip | |
feat(www): locale specific urls (#12508)
Diffstat (limited to 'packages/console/app/src/lib')
| -rw-r--r-- | packages/console/app/src/lib/language.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/console/app/src/lib/language.ts b/packages/console/app/src/lib/language.ts index e1e62d8fd..7506857ce 100644 --- a/packages/console/app/src/lib/language.ts +++ b/packages/console/app/src/lib/language.ts @@ -21,6 +21,12 @@ export const LOCALES = [ export type Locale = (typeof LOCALES)[number] export const LOCALE_COOKIE = "oc_locale" as const +export const LOCALE_HEADER = "x-opencode-locale" as const + +function fix(pathname: string) { + if (pathname.startsWith("/")) return pathname + return `/${pathname}` +} const LABEL = { en: "English", @@ -68,6 +74,28 @@ export function parseLocale(value: unknown): Locale | null { return null } +export function fromPathname(pathname: string) { + return parseLocale(fix(pathname).split("/")[1]) +} + +export function strip(pathname: string) { + const locale = fromPathname(pathname) + if (!locale) return fix(pathname) + + const next = fix(pathname).slice(locale.length + 1) + if (!next) return "/" + if (next.startsWith("/")) return next + return `/${next}` +} + +export function route(locale: Locale, pathname: string) { + const next = strip(pathname) + if (next.startsWith("/docs")) return next + if (locale === "en") return next + if (next === "/") return `/${locale}` + return `/${locale}${next}` +} + export function label(locale: Locale) { return LABEL[locale] } @@ -160,6 +188,12 @@ export function localeFromCookieHeader(header: string | null) { } export function localeFromRequest(request: Request) { + const fromHeader = parseLocale(request.headers.get(LOCALE_HEADER)) + if (fromHeader) return fromHeader + + const fromPath = fromPathname(new URL(request.url).pathname) + if (fromPath) return fromPath + return ( localeFromCookieHeader(request.headers.get("cookie")) ?? detectFromAcceptLanguage(request.headers.get("accept-language")) |
