internationalization (I18n)

Internationalization with RealizeJS

I18n is one of the many implemented functionalities in RealizeJS. Right bellow will be presented all of your properties and functions. If you want to see right on the source code, go to i18n.js file on GitHub.

Properties

locales Object

Object with all supported locales. By default, it’s added en and pt-BR.

currentLocale String

Current system locale.

default: 'en'

Functions

registerLocale Function

Function used to add or override one of properties object locale. Receive the following parameters: a properties object locale and his locale (example: 'en-US').

i18n.js
registerLocale: function(newLocaleObj, locale) {
  if(!$.isPlainObject(newLocaleObj)) {
    throw 'Invalid Locale Object.'
  }

  if(!locale) {
    throw 'Invalid Locale Name.';
  }

  var currentLocaleObj = this.locales[locale] || {};
  this.locales[locale] = $.extend(true, {}, currentLocaleObj, newLocaleObj);
}

setLocale Function

Changes currentLocale property with received one.

i18n.js
setLocale: function(locale) {
  this.currentLocale = locale;
}

translate Function

Gets the locale resource. In order to do that, receives the key of this resource and a boolean to inform if is or isn’t to throw an exception.

i18n.js
translate: function(key, throwsException) {
  if(throwsException === undefined) {
    throwsException = false;
  }

  if(typeof key !== "string") {
    if(throwsException) {
      throw 'Key is not a string';
    }

    return '';
  }

  var currentLocale = this.currentLocale;
  var localeObj = this.locales[currentLocale];

  var translation = utils.getProp(key, localeObj);
  if(!translation) {
    if(throwsException) {
      throw 'Key not found in locale object';
    }

    translation = key;
  }

  return translation;
}

t Function

With the purpose to help, was included this function that returns a call to translate function.

i18n.js
t: function(key, throwsException) {
  return this.translate(key, throwsException);
}