Skip to content
Myr

fix(language-detector): Detect language from path after getPath changed (#4369)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Detect language from path after getPath changed Use the source URL from the request to detect language from path. Because `c.req.path` will be updated by the `getPath` method. ```javascript const app = new Hono({ getPath: (req) => { const url = new URL(req.url) let pathname = url.pathname for (let idx = 0; idx < languages.length; idx++) { if (pathname.startsWith('/' + languages[idx] + '/')) { pathname = pathname.replaceAll('/' + languages[idx] + '/', '/') break } } return pathname } }) ``` Detect language from the path: ```javascript app.use(languageDetector({ debug: true, order: ['querystring', 'path', 'cookie', 'header'], lookupFromPathIndex: 0, // /en/profile → index 0 = 'en' convertDetectedLanguage: (lang) => lang.split('-')[0], supportedLanguages: languages, // Must include fallback fallbackLanguage: 'en', // Required })) ``` ```javascript app.get('/home', (c) => {...}) ``` Then you can visit '/home' route with this url `https://example.com/en/home` . * add test ---------

authored by New Bing · committed by GitHub · parent 0e3db67 · Browse files · 2 files+31 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

2 files+31 added−1 removed

  • tests:+29 added−0 removed
  • other:+2 added−1 removed

2 files+31 added−1 removed

modified:src/middleware/language/index.test.ts+29 added
114 unmodified lines
115116 const res = await app.request('/fr/page')117 expect(await res.text()).toBe('en')118 })119120 it('should detect language from original URL when getPath modifies the path', async () => {121 const app = new Hono({122 getPath: (req) => {123 const url = new URL(req.url)124 let pathname = url.pathname125 // Remove language prefix /fr/126 if (pathname.startsWith('/fr/')) {127 pathname = pathname.replace('/fr/', '/')128 }129 return pathname130 },131 })132133 app.use(134 '*',135 languageDetector({136 order: ['path'],137 supportedLanguages: ['en', 'fr'],138 fallbackLanguage: 'en',139 lookupFromPathIndex: 0,140 })141 )142143 app.get('/home', (c) => c.text(c.get('language')))144145 const res = await app.request('/fr/home')146 expect(await res.text()).toBe('fr')147 })148 })149150 describe('Detection Order', () => {151 it('should respect detection order', async () => {
153 unmodified lines
modified:src/middleware/language/language.ts+2 added-1 removed
157 unmodified lines
158 * Detects language from URL path159 */160export function detectFromPath(c: Context, options: DetectorOptions): string | undefined {161 try {162 const url = new URL(c.req.url)162 const pathSegments = c.req.path.split('/').filter(Boolean)163 const pathSegments = url.pathname.split('/').filter(Boolean)164 const langSegment = pathSegments[options.lookupFromPathIndex]165 return normalizeLanguage(langSegment, options)166 } catch {167 return undefined
123 unmodified lines