A FastBoot-enabled addon to detect the best language for your user.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
addon/services
app/services
config
tests
vendor
.editorconfig
.ember-cli
.eslintrc
.gitignore
.npmignore
.travis.yml
.watchmanconfig
LICENSE.md
README.md
ember-cli-build.js
index.js
package.json
testem.js
yarn.lock

README.md

ember-best-language

Build Status

An FastBoot-enabled addon to detect the best language for your user.

Installation

$ ember install ember-best-language

How does it work?

ember-best-language uses a scoring system to determine the best language to use. The scoring system is based on the Accept-Language header on the FastBoot-side. On the client side, we use navigator.languages and give a score to each language based on its order in the array.

ember-best-language also split language code from country code to make sure that if the user reads fr-CA and your system supports fr, you will have a match.

This addon is inspired by the work of Rémi Prévost in plug_best, you should check it out!

Usage

ember-best-language provides a service with two methods:

  • bestLanguage
  • bestLanguageOrFirst

To find out which language is the best one to use among a list of supported languages:

import Ember from 'ember';

const {inject} = Ember;

export default Ember.Route.extend({
  bestLanguage: inject.service('best-language'),

  beforeModel() {
    const bestLanguage = this.get('bestLanguage').bestLanguage(['en', 'fr']);
    // => {language: 'en-US', baseLanguage: 'en', score: 1}
  }
});

If none of the user’s languages are supported, ember-best-language will return null. However, you can use the bestLanguageOrFirst method to make it return the first supported language in those cases.

import Ember from 'ember';

const {inject} = Ember;

export default Ember.Route.extend({
  bestLanguage: inject.service('best-language'),

  beforeModel() {
    const bestLanguage = this.get('bestLanguage').bestLanguage(['fr', 'de']);
    // => null

    const bestLanguageOrFirst = this.get('bestLanguage').bestLanguageOrFirst(['fr', 'de']);
    // => {language: 'fr', baseLanguage: 'fr', score: 0}
  }
});

Note: for the time being, we only support an array of language codes without country code e.g. 'es'. PRs welcome! :)

Contributing

$ git clone https://github.com/mirego/ember-best-language
$ cd ember-best-language
$ yarn install

Running tests

$ npm test # Runs `ember try:each` to test the addon against multiple Ember versions
$ ember test
$ ember test --server

Building

$ ember build

For more information on using ember-cli, visit https://ember-cli.com/.

License

ember-best-language is © 2017 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.