express-request-language icon indicating copy to clipboard operation
express-request-language copied to clipboard

Add feature to set a default language

Open guiihlopes opened this issue 5 years ago • 5 comments

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?

guiihlopes avatar Oct 22 '18 13:10 guiihlopes

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 avatar Oct 31 '18 15:10 visurel

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

tinganho avatar Oct 31 '18 16:10 tinganho

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.

guiihlopes avatar Oct 31 '18 16:10 guiihlopes

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?

tinganho avatar Oct 31 '18 17:10 tinganho

Yes, you got it

guiihlopes avatar Oct 31 '18 19:10 guiihlopes