/acceptedlanguages.js

A browser abstraction JavaScript library that tells you page and visitor language information like the accepted languages of the visitor and the current, alternate and relevant languages of the current page.

Primary LanguageJavaScriptISC LicenseISC

acceptedlanguages.js

Build Status

A simple JavaScript library that exposes the accepted languages of the visitor configured in their browser, and the languages relevant given the languages that the current page is alternatively available in.

Install

Bower

bower install acceptedlanguages

NPM

npm install acceptedlanguages --save

Manual

Download acceptedlanguages.min.js.

Usage

Globals

Include acceptedlanguages.js into the page and call any of the global properties.

<head>
  <link rel="alternate" hreflang="pt-BR" href="..." />
  <link rel="alternate" hreflang="es" href="..." />
  <link rel="alternate" hreflang="en" href="..." />
  <script src="acceptedlanguages.js"></script>
  <script type="text/javascript">
    console.log('Accepted:  ' + window.acceptedlanguages.accepted);
    console.log('Alternate: ' + window.acceptedlanguages.alternate);
    console.log('Relevant:  ' + window.acceptedlanguages.relevant);
  </script>
</head>

If the browser is configured with the languages pt, zh-Hans, en-US, the above would output:

Accepted:  pt-BR,pt,zh-Hans,en-US
Alternate: pt-BR,es,en
Relevant:  pt-BR,en

RequireJS

Include the library and then use as a dependency:

define(['acceptedlanguages'], function(acceptedLanguages) {
  console.log('Accepted:  ' + acceptedlanguages.accepted);
  console.log('Alternate: ' + acceptedlanguages.alternate);
  console.log('Relevant:  ' + acceptedlanguages.relevant);
});

Browser Compatibility

All browsers supporting ECMAScript 5: Chrome, Safari, Firefox, IE9, etc. If you need to support older browsers use es5-shims to get the following:

  • Array.forEach
  • Array.filter
  • Array.map

Expect that Chrome and Firefox may return multiple languages while Safari and IE will return only a single language.