Localization API

EBA has a client side module called L10n. This module provides globalization support. It exposes a few top level functions which can be used in client side assets, and, additionally, it extends a global variable called L10N with a set of useful functions.

Global globalization functions:

  • trySetLocale(locale) -- tries to set the current locale, defaults to 'en' in case of failure.
  • setLocale(locale) -- called by trySetLocale which sets the locale and extends L10N with a set of useful formatting, parsing, and other globalized utility functions.
  • renderDate(x) -- uses moment library to convert javascript dates using moment's calendar function. Converts numbers to dates and returns 'N/A' in case value is neither date, string, nor number. Note that moment's locale is set during call to setLocale.
  • renderTags(x, color, key) -- renders a string 'x' or a prop 'tag' of object 'x' as a colored span. This is used by EBA to render tags within its knowledge graph.
  • renderBoolean(x) -- renders boolean according to locale.
  • renderDecimal(x) -- renders decimal value according to locale.
  • renderPercent(x) -- renders percent value according to locale.
  • renderCurrency(x, currency) -- renders currency value according to locale, defaults to USD if currency is not provided as second argument.
  • renderDuration(duration) -- renders duration according as microsecond, millisecond, second, or minute.
  • formatLanguage(locale, displayLocale) -- given a locale and optional displayLocale, displays the language in English as well as the native locale, e.g. `formatLanguage('de') returns "German – Deutsch".

L10N global object API. Note that this api utilizes globalize.js to apply the appropriate locale normalizations.

  • renderDate(x) -- renders date value 'x' as a medium date, e.g. L10N.renderDate(new Date()) can return "Feb 4, 2019"
  • renderDateShort(x) -- renders date value 'x' as a short date, e.g. L10N.renderDateShort(new Date()) can return "2/4/19"
  • renderTime(x) -- renders date value 'x' as a medium time value, e.g. L10N.renderTime(new Date()) can return "4:23:00 PM"
  • renderDateTime(x) -- renders date value 'x' as a date time value, e.g. L10N.renderDateTime(new Date()) can return "Feb 4, 2019, 4:23:16 PM"
  • formatNumber(x) -- returns the string representation of the number 'x', e.g. L10N.formatNumber(10) return "10"
  • formatNumberSigned(x) -- returns the string representation of the signed number 'x', e.g. L10N.formatNumberSigned(10) return "+10"
  • formatNumberScaled(x) -- returns the string representation of the scaled number 'x'
  • formatNumberPrecise(x) -- returns the string representation of the precise number 'x', e.g. L10N.formatNumberPrecise(10.1) return "10.1"
  • formatNumberRounded(x) -- returns the string representation of the rounded number 'x', e.g. L10N.formatNumberRounded(10.1) return "10"
  • formatPercent(x) -- returns the string representation of the 'x' as a percent, e.g. L10N.formatPercent(0.27) returns "27%"
  • formatPercentWith(options) -- like formatPercent, but with options
  • formatPercentSigned(x) -- returns the string representation of the 'x' as a signed percent, e.g. L10N.formatPercentSigned(0.27) returns "+27%"
  • formatPercentNacked(x) -- returns the string representation of the 'x' as a percent without percentege sign, e.g. L10N.formatPercentNacked(0.27) returns "27"
  • formatPercentNackedSigned -- returns the string representation of the 'x' as a signed percent without percentege sign, e.g. L10N.formatPercentNackedSigned(0.27) returns "+27" Note: formatPercentWith, parseDate, formatNumberScaled
  • formatCurrency(currency) -- returns a function which applies the appropriate currency formatting, e.g. L10N.formatCurrency('USD')(42) returns "$42.00"
  • formatCurrencySigned(currency) -- returns a function which applies the appropriate currency formatting using signed value, e.g. L10N.formatCurrency('USD')(42) returns "+$42.00"
  • formatCurrencyScaled(currency) -- returns a function which applies the appropriate currency formatting using scaled value, e.g. L10N.formatCurrencyScaled('USD')(42.4) returns "$42.4"
  • formatCurrencyNacked(currency) -- returns a function which applies the appropriate currency formatting using nacked value, e.g. L10N.formatCurrencyNacked('USD')(42.4) returns "42.40"
  • formatCurrencyNackedSigned(currency) -- returns a function which applies the appropriate currency formatting using nacked value, e.g. L10N.formatCurrencyNackedSigned('USD')(42.4) returns "+42.40"
  • formatCurrencyRounded(currency) -- returns a function which applies the appropriate currency formatting using nacked value, e.g. L10N.formatCurrencyRounded('USD')(42.555) returns "$42.56"
  • formatBoolean -- returns the 'yes' or 'no' string equivalent of a boolean, e.g. L10N.formatBoolean(true) returns "yes"
    • parseDate(x) -- parses date string x as date)
  • parseNumber(x) -- parses a string representation of a number and returns the number, e.g. L10N.parseNumber("10") returns 10
  • parsePercent(x) -- parses a string representation of a percent and returns the percent, e.g. L10N.parsePercent("10%") returns 0.1
  • parsePercentNacked(x) -- parses a string representation of a nacked percent and returns the percent, e.g. L10N.parsePercentNacked("10") returns 0.1
  • currencySymbol -- property which returns the currency symbol
  • percentSign -- property which returns the percent sign
  • weekDays -- property returns the week days
  • weekDaysKeys -- property returns the week days keys
  • direction -- property returns the script direction, e.g. rtl for right to left and ltr for left to right