express-request-language
express-request-language copied to clipboard
Add feature to set a default language
It would be nice if have some way to pass a param like queryName
to set a default language.
So, if there isn't a cookie already set and if you pass a prop like: defaultLanguage
, then set the default language. I think it could be a very nice feature.
Example:
const languages = ['en', 'pt']
server.use(
requestLanguage({
languages: languages,
queryName: 'lang',
defaultLanguage: 'en',
cookie: {
name: 'DEFAULT_LANGUAGE',
options: { maxAge: 24 * 3600 * 1000 }
}
})
)
The first time if there is no cookie already set, it would be 'en', ignoring the accept language...
WDYT about it?
Just checked the source code of accept-language
, looks like the first value set in the languages
-array is automatically set as default if nothing else matches.
But indeed, it would be a lot clearer if it would just be a seperate param.
@visurel is right. The first language is the default one. I'm not sure if adding it as a separate property makes sense. Because the languages array is sort of a priority list.
I was saying to ignore the accept-language
if the default language param is set and there's no DEFAULT_LANGUAGE
cookie. In this way, no matter which accept-language
matches, it would return the default language. I know that I can do it already by myself, but I think it would be nice to flexibilize and improve the package with this feature.
Okay you want to turn off the accept language negotiation? And always default to whatever is being set as default except when the language cookie is set?
Yes, you got it