req.acceptsLanguages()
Return whether this request (req
) advertises that it understands any of the specified language(s), and if so, which one.
If more than one of the languages passed in to this method are considered acceptable, then the first one will be returned. If none of the languages are considered acceptable, this returns
false
. (By languages, we mean natural languages, like English or Japanese, not programming languages.)
req.acceptsLanguages(language);
Or:
req.acceptsLanguages(language1, language2, …);
This method can be useful as a complement to built-in internationalization and localization, which allows for automatically serving different content to different locales based on the request.
If a request is sent with "Accept-Language: da, en, en-gb, en-us;"
:
req.acceptsLanguages('en');
// -> 'en'
req.acceptsLanguages('es');
// -> false
req.acceptsLanguages('en-us', 'en', 'en-gb');
// -> 'en-us'
req.acceptsLanguages('en-gb', 'en', 'en-us');
// -> 'en-gb'
req.acceptsLanguages('es', 'fr');
// -> false
- You can expect the "Accept-Language" header to exist in most requests that originate in web browsers (see RFC-2616).
- Browsers send the "Accept-Language" header automatically, based on the user's language settings.
- See the
accepts
package for the finer details of the header-parsing algorithm used in Sails/Express.