Back to Sails

`req.acceptsLanguages()`

docs/reference/req/req.acceptsLanguages.md

12.12.20001.8 KB
Original Source

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

Usage

usage
req.acceptsLanguages(language);

Or:

  • req.acceptsLanguages(language1, language2, …);

Details

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.

Example

If a request is sent with "Accept-Language: da, en, en-gb, en-us;":

js
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

Notes

  • 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.
<docmeta name="displayName" value="req.acceptsLanguages()"> <docmeta name="pageType" value="method">