mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-13 20:24:53 +00:00
This commit is contained in:
@@ -108,6 +108,26 @@ const DOCS_SEGMENT = new Set([
|
|||||||
"zh-tw",
|
"zh-tw",
|
||||||
])
|
])
|
||||||
|
|
||||||
|
const DOCS_LOCALE = {
|
||||||
|
ar: "ar",
|
||||||
|
da: "da",
|
||||||
|
de: "de",
|
||||||
|
en: "en",
|
||||||
|
es: "es",
|
||||||
|
fr: "fr",
|
||||||
|
it: "it",
|
||||||
|
ja: "ja",
|
||||||
|
ko: "ko",
|
||||||
|
nb: "no",
|
||||||
|
"pt-br": "br",
|
||||||
|
root: "en",
|
||||||
|
ru: "ru",
|
||||||
|
th: "th",
|
||||||
|
tr: "tr",
|
||||||
|
"zh-cn": "zh",
|
||||||
|
"zh-tw": "zht",
|
||||||
|
} as const satisfies Record<string, Locale>
|
||||||
|
|
||||||
function suffix(pathname: string) {
|
function suffix(pathname: string) {
|
||||||
const index = pathname.search(/[?#]/)
|
const index = pathname.search(/[?#]/)
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
@@ -130,7 +150,12 @@ export function docs(locale: Locale, pathname: string) {
|
|||||||
return `${next.path}${next.suffix}`
|
return `${next.path}${next.suffix}`
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value === "root") return `${next.path}${next.suffix}`
|
if (value === "root") {
|
||||||
|
if (next.path === "/docs/en") return `/docs${next.suffix}`
|
||||||
|
if (next.path === "/docs/en/") return `/docs/${next.suffix}`
|
||||||
|
if (next.path.startsWith("/docs/en/")) return `/docs/${next.path.slice("/docs/en/".length)}${next.suffix}`
|
||||||
|
return `${next.path}${next.suffix}`
|
||||||
|
}
|
||||||
|
|
||||||
if (next.path === "/docs") return `/docs/${value}${next.suffix}`
|
if (next.path === "/docs") return `/docs/${value}${next.suffix}`
|
||||||
if (next.path === "/docs/") return `/docs/${value}/${next.suffix}`
|
if (next.path === "/docs/") return `/docs/${value}/${next.suffix}`
|
||||||
@@ -154,6 +179,15 @@ export function fromPathname(pathname: string) {
|
|||||||
return parseLocale(fix(pathname).split("/")[1])
|
return parseLocale(fix(pathname).split("/")[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function fromDocsPathname(pathname: string) {
|
||||||
|
const next = fix(pathname)
|
||||||
|
const value = next.split("/")[2]?.toLowerCase()
|
||||||
|
if (!value) return null
|
||||||
|
if (!next.startsWith("/docs/")) return null
|
||||||
|
if (!(value in DOCS_LOCALE)) return null
|
||||||
|
return DOCS_LOCALE[value as keyof typeof DOCS_LOCALE]
|
||||||
|
}
|
||||||
|
|
||||||
export function strip(pathname: string) {
|
export function strip(pathname: string) {
|
||||||
const locale = fromPathname(pathname)
|
const locale = fromPathname(pathname)
|
||||||
if (!locale) return fix(pathname)
|
if (!locale) return fix(pathname)
|
||||||
@@ -272,6 +306,9 @@ export function localeFromRequest(request: Request) {
|
|||||||
const fromPath = fromPathname(new URL(request.url).pathname)
|
const fromPath = fromPathname(new URL(request.url).pathname)
|
||||||
if (fromPath) return fromPath
|
if (fromPath) return fromPath
|
||||||
|
|
||||||
|
const fromDocsPath = fromDocsPathname(new URL(request.url).pathname)
|
||||||
|
if (fromDocsPath) return fromDocsPath
|
||||||
|
|
||||||
return (
|
return (
|
||||||
localeFromCookieHeader(request.headers.get("cookie")) ??
|
localeFromCookieHeader(request.headers.get("cookie")) ??
|
||||||
detectFromAcceptLanguage(request.headers.get("accept-language"))
|
detectFromAcceptLanguage(request.headers.get("accept-language"))
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { APIEvent } from "@solidjs/start/server"
|
import type { APIEvent } from "@solidjs/start/server"
|
||||||
import { Resource } from "@opencode-ai/console-resource"
|
import { Resource } from "@opencode-ai/console-resource"
|
||||||
import { docs, localeFromRequest, tag } from "~/lib/language"
|
import { cookie, docs, localeFromRequest, tag } from "~/lib/language"
|
||||||
|
|
||||||
async function handler(evt: APIEvent) {
|
async function handler(evt: APIEvent) {
|
||||||
const req = evt.request.clone()
|
const req = evt.request.clone()
|
||||||
@@ -17,7 +17,9 @@ async function handler(evt: APIEvent) {
|
|||||||
headers,
|
headers,
|
||||||
body: req.body,
|
body: req.body,
|
||||||
})
|
})
|
||||||
return response
|
const next = new Response(response.body, response)
|
||||||
|
next.headers.append("set-cookie", cookie(locale))
|
||||||
|
return next
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = handler
|
export const GET = handler
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { APIEvent } from "@solidjs/start/server"
|
import type { APIEvent } from "@solidjs/start/server"
|
||||||
import { Resource } from "@opencode-ai/console-resource"
|
import { Resource } from "@opencode-ai/console-resource"
|
||||||
import { docs, localeFromRequest, tag } from "~/lib/language"
|
import { cookie, docs, localeFromRequest, tag } from "~/lib/language"
|
||||||
|
|
||||||
async function handler(evt: APIEvent) {
|
async function handler(evt: APIEvent) {
|
||||||
const req = evt.request.clone()
|
const req = evt.request.clone()
|
||||||
@@ -17,7 +17,9 @@ async function handler(evt: APIEvent) {
|
|||||||
headers,
|
headers,
|
||||||
body: req.body,
|
body: req.body,
|
||||||
})
|
})
|
||||||
return response
|
const next = new Response(response.body, response)
|
||||||
|
next.headers.append("set-cookie", cookie(locale))
|
||||||
|
return next
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = handler
|
export const GET = handler
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { APIEvent } from "@solidjs/start/server"
|
import type { APIEvent } from "@solidjs/start/server"
|
||||||
import { Resource } from "@opencode-ai/console-resource"
|
import { Resource } from "@opencode-ai/console-resource"
|
||||||
import { docs, localeFromRequest, tag } from "~/lib/language"
|
import { cookie, docs, localeFromRequest, tag } from "~/lib/language"
|
||||||
|
|
||||||
async function handler(evt: APIEvent) {
|
async function handler(evt: APIEvent) {
|
||||||
const req = evt.request.clone()
|
const req = evt.request.clone()
|
||||||
@@ -17,7 +17,9 @@ async function handler(evt: APIEvent) {
|
|||||||
headers,
|
headers,
|
||||||
body: req.body,
|
body: req.body,
|
||||||
})
|
})
|
||||||
return response
|
const next = new Response(response.body, response)
|
||||||
|
next.headers.append("set-cookie", cookie(locale))
|
||||||
|
return next
|
||||||
}
|
}
|
||||||
|
|
||||||
export const GET = handler
|
export const GET = handler
|
||||||
|
|||||||
Reference in New Issue
Block a user