Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Doc: Setting the Locale

Elie edited this page Oct 5, 2018 · 12 revisions

Setting the Locale

Set the default locale in config/environment.js:

ENV.i18n = {
  defaultLocale: 'zh'
};

Override the default by setting locale on the i18n service:

// app/routes/application.js

export default Ember.Route.extend({
  i18n: Ember.inject.service(),
  afterModel: function(user) {
    this.set('i18n.locale', user.get('locale'));
  }
});

Note: if you do this in an initializer and use FastBoot, you probably want to use an instance-initializer:

// app/instance-initializers/i18n.js

export default {
  name: 'i18n',
  initialize: function({ container }) {
    let i18n = container.lookup('service:i18n');
    
    i18n.set('locale', calculateLocale(i18n.get('locales')));
  }
}

function calculateLocale(locales) {
  // whatever you do to pick a locale for the user:
  const language = navigator.languages[0] || navigator.language || navigator.userLanguage;

  return locales.includes(language.toLowerCase()) ? language : 'en-GB';
}

Note: after deprecation of container, to make it work on Ember >= 3.3, you will need to replace:

  initialize: function({ container }) {
    let i18n = container.lookup('service:i18n');

By:

  initialize: function(appInstance) {
    let i18n = appInstance.lookup('service:i18n');