@@ -108,6 +108,26 @@ const DOCS_SEGMENT = new Set([
108108 "zh-tw" ,
109109] )
110110
111+ const DOCS_LOCALE = {
112+ ar : "ar" ,
113+ da : "da" ,
114+ de : "de" ,
115+ en : "en" ,
116+ es : "es" ,
117+ fr : "fr" ,
118+ it : "it" ,
119+ ja : "ja" ,
120+ ko : "ko" ,
121+ nb : "no" ,
122+ "pt-br" : "br" ,
123+ root : "en" ,
124+ ru : "ru" ,
125+ th : "th" ,
126+ tr : "tr" ,
127+ "zh-cn" : "zh" ,
128+ "zh-tw" : "zht" ,
129+ } as const satisfies Record < string , Locale >
130+
111131function suffix ( pathname : string ) {
112132 const index = pathname . search ( / [ ? # ] / )
113133 if ( index === - 1 ) {
@@ -130,7 +150,12 @@ export function docs(locale: Locale, pathname: string) {
130150 return `${ next . path } ${ next . suffix } `
131151 }
132152
133- if ( value === "root" ) return `${ next . path } ${ next . suffix } `
153+ if ( value === "root" ) {
154+ if ( next . path === "/docs/en" ) return `/docs${ next . suffix } `
155+ if ( next . path === "/docs/en/" ) return `/docs/${ next . suffix } `
156+ if ( next . path . startsWith ( "/docs/en/" ) ) return `/docs/${ next . path . slice ( "/docs/en/" . length ) } ${ next . suffix } `
157+ return `${ next . path } ${ next . suffix } `
158+ }
134159
135160 if ( next . path === "/docs" ) return `/docs/${ value } ${ next . suffix } `
136161 if ( next . path === "/docs/" ) return `/docs/${ value } /${ next . suffix } `
@@ -154,6 +179,15 @@ export function fromPathname(pathname: string) {
154179 return parseLocale ( fix ( pathname ) . split ( "/" ) [ 1 ] )
155180}
156181
182+ export function fromDocsPathname ( pathname : string ) {
183+ const next = fix ( pathname )
184+ const value = next . split ( "/" ) [ 2 ] ?. toLowerCase ( )
185+ if ( ! value ) return null
186+ if ( ! next . startsWith ( "/docs/" ) ) return null
187+ if ( ! ( value in DOCS_LOCALE ) ) return null
188+ return DOCS_LOCALE [ value as keyof typeof DOCS_LOCALE ]
189+ }
190+
157191export function strip ( pathname : string ) {
158192 const locale = fromPathname ( pathname )
159193 if ( ! locale ) return fix ( pathname )
@@ -272,6 +306,9 @@ export function localeFromRequest(request: Request) {
272306 const fromPath = fromPathname ( new URL ( request . url ) . pathname )
273307 if ( fromPath ) return fromPath
274308
309+ const fromDocsPath = fromDocsPathname ( new URL ( request . url ) . pathname )
310+ if ( fromDocsPath ) return fromDocsPath
311+
275312 return (
276313 localeFromCookieHeader ( request . headers . get ( "cookie" ) ) ??
277314 detectFromAcceptLanguage ( request . headers . get ( "accept-language" ) )
0 commit comments