Giter Site home page Giter Site logo

ember-i18n-loader's Introduction

ember-i18n-loader

This Ember.js addon offers a wrapper service i18n-loader on top of the ember-i18n addon to load the translation on demand or live using the jQuery AJAX. It extends the sample code available at ember-i18n:Fetching-Translations-Live.

Loading translations on demand

Multilingual large scale apps do not require to load translations for all supported languages as the app uses only one language at a time. In some cases, the complete set of translations for one language can be a heavy load task for initial load, as only current/initial screen/route translations are necessary, which can cause more delays on app load time. So its preferable to load the translations for only one language or in chunks (for single language) as they are required.

The translations can be loaded in the beforeModel() hook of the respective routes.

i18nLoader: Ember.inject.service('i18n-loader'),
beforeModel() {
  return this.get('i18nLoader).load('locales/en/translations.json');
}

.load(ajaxSettings)

i18n-loader service exposes a method load(ajaxSettings) to load the translations.

ajaxSettings

A JSON object holding the AJAX settings for the i18n resource. This object must have at least url property set pointing to the location of i18n resource. The JSON object passed gets merged into the default AJAX settings and therefore the default AJAX settings can be overridden.

Default AJAX settings which can be overridden:

{
  cache: true,
  dataType: 'json',
  type: 'GET'
}

The caller can pass:

{
  url: '/locales/en/translations.json',
  headers: {
    "Authorization": "bearer aXVmZ2hpdzg3eWZ1ZndmdWdiNDU1c29pdmpic29nNDU2NGZzYmVpZmhzaWY2NnVzZGhiaWpzaGI0NTZmdWdo"
  }
}

Promise Based Response

The service i18n-loader returns a promise, which gets resolved if the translations are loaded successfully otherwise rejected on any error.

Caching

The service i18n-loader remembers the translation urls to avoid loading the same translations for consequent requests for same url. Note: The service does not remember the translations key already loaded, instead it remembers the urls of the translation files like /locales/en/abc.route.translations.json.

Example

The locales are stored under the public/locales directory of the app.

public
|__locales
   |__de
      |__translations.json
   |__en
      |__translations.json	
   |__fr
      |__translations.json

Or further dividing them into smaller chunks.

public
|__locales
   |__de
      |__common.translations.json
      |__product-management.translations.json
      |__user-administration.translations.json
   |__en
      |__common.translations.json
      |__product-management.translations.json
      |__user-administration.translations.json
   |__fr
      |__common.translations.json
      |__product-management.translations.json
      |__user-administration.translations.json

The app can detect users's expected language in the application route and loads the translations.

routes/application.js

i18n: Ember.inject.service(),
i18nLoader: Ember.inject.service('i18n-loader'),
beforeModel() {
  // find the locale
  const locale = navigator.language;
  // set the i18n locale
  this.set('i18n.locale', locale);
  // load translations
  return this.get('i18nLoader').load(`/locales/${locale}/translations.json`);
}

Caution

AJAX Approach: Browsers do not allow cross domain resource downloads through AJAX unless the server allows the requests through CORS settings.

Installation

  • git clone <repository-url> this repository
  • cd ember-i18n-loader
  • npm install

Running

Running Tests

  • npm test (Runs ember try:each to test your 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/.

ember-i18n-loader's People

Contributors

ember-tomster avatar vivekmunde avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.