-
-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Language Detector Middleware for Hono #3785
Comments
Hi @lord007tn Thank you for the proposal. It will be helpful. You can do a similar thing with the Accepts Helper: import { accepts } from 'hono/accepts'
import { Hono } from 'hono'
type Env = {
Variables: {
language: string
}
}
const app = new Hono<Env>()
app.use(async (c, next) => {
const language = accepts(c, {
header: 'Accept-Language',
supports: ['en', 'ja', 'zh'],
default: 'en'
})
c.set('language', language)
await next()
})
app.get('/', (c) => {
const lang = c.get('language')
return c.text(`Current language: ${lang}`)
})
export default app But, your suggestion and the middleware with your PR have other convenient features, such as language normalizer. Looks good. We have to work on considering whether to add the middleware or not. I will also think about it. Thanks! |
accepts only limited to header in my case it is a series of fallbacks that you configure to get the user language If it is only header, I am okay with you but in the case of path query cookies we need to implement custom logic each time also if hono is planning to add a feature proof i18n ( its a crucial part of mid-size to large applications, and multinational Saas ) they can depend on that kind of middleware also the full use of the middleware options is: app.use(
'*',
languageDetector({
// Detection strategies and their order
order: ['path', 'querystring', 'cookie', 'header'],
// Supported languages and fallback
supportedLanguages: ['en', 'ar'],
fallbackLanguage: 'en',
// Query parameter settings
lookupQuerystring: 'lang', // ?lang=en
// Path settings
lookupFromPathIndex: 0, // /en/about -> takes 'en'
// Cookie settings
lookupCookie: 'user-language',
caches: false, // Set to false to disable caching
// Header settings
lookupFromHeaderKey: 'accept-language',
// Language code handling
ignoreCase: true,
convertDetectedLanguage: (lang: string) => {
// Optional: Convert language codes
// e.g., 'en-US' -> 'en'
return lang?.split('-')[0]?.toLowerCase() ?? '';
},
// Debugging
debug: process.env.NODE_ENV !== 'production',
}),
); |
What is the feature you are proposing?
Background
While building an i18n system for Hono, I noticed the lack of a standardized way to detect user language preferences across different sources (URL, cookies, headers, etc.). This is a crucial component for internationalization, and currently, each developer needs to implement their own solution.
Current Situation
Currently in Hono:
Proposed Solution
A language detector middleware that:
c.get('language')
Basic usage example:
The text was updated successfully, but these errors were encountered: