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 ---------
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