An open API service indexing awesome lists of open source software.

https://github.com/leighmcculloch/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.
https://github.com/leighmcculloch/acceptedlanguages.js

Last synced: 20 days ago
JSON representation

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.

Awesome Lists containing this project

README

        

# acceptedlanguages.js

[![Build Status](https://travis-ci.org/leighmcculloch/acceptedlanguages.js.svg?branch=master)](https://travis-ci.org/leighmcculloch/acceptedlanguages.js)

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

```bash
bower install acceptedlanguages
```

### NPM

```bash
npm install acceptedlanguages --save
```

### Manual

Download [acceptedlanguages.min.js](dist/acceptedlanguages.min.js).

## Usage

### Globals

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

```html






console.log('Accepted: ' + window.acceptedlanguages.accepted);
console.log('Alternate: ' + window.acceptedlanguages.alternate);
console.log('Relevant: ' + window.acceptedlanguages.relevant);

```

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

```text
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:

```javascript
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](https://github.com/es-shims/es5-shim) 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.