API

class
 new API() 

Class API

class API {
  constructor(core) {
      log.debug(`class: ${API.name}`, `process: constructor`);
      this.options = core.options;
      this.context = core;
    }

set

method
 API.prototype.set() 

Sets the API

set() {
    log.debug(`class: ${API.name}`, `process: set`);
    var core = this.context;

i18n

function
 i18n() 

The i18n function

var i18n = function() {};

l10n

function
 l10n() 

The l10n function

var l10n = function() {};

// Set the options
var options = this.options.api;
_.assign((options.header = {}), this.options.header);
debug('api', 'info', 'options exists:', !(!options));
Option name Type Description
arg

The arguments to internationalize.

return String

Then i18ned string.

i18ns the arguments. Note: You can change ID for i18n. See Options.

i18n[options.global] = function(...args) {
  log.debug(`class: ${API.name}`, `process: i18n`);
  return core.parse.apply(core, args);
};

Example

The following applies only for gengojs-default-parser

Phrase notation with default parser.

// Assuming the locale === 'ja',
// a basic phrase returns 'こんにちは'
__('Hello');

// a basic phrase with sprintf returns 'Bob こんにちは'
__('Hello %s', 'Bob');

// a basic phrase with interpolation returns 'Bob こんにちは'
 __('Hello {{name}}', {name:'Bob'});

Bracket notation with default parser.

// Assuming the locale === 'ja',
// a basic bracket phrase returns 'おっす'
__('[Hello].informal');

// a basic bracket phrase with sprintf returns 'Bob おっす'
__('[Hello %].informal', 'Bob');

// a basic bracket phrase with interpolation returns 'Bob おっす'
__('[Hello {{name}}].informal', {name:'Bob'});

Dot notation with default parser.

// Assuming the locale === 'ja',
// a basic dot phrase returns 'おっす'
__('greeting.hello.informal');

// a basic dot phrase with sprintf returns 'Bob おっす'
__('greeting.hello.person.informal', 'Bob');

//a basic dot phrase with interpolation returns 'Bob おっす'
__('greeting.hello.person.informal', {name:'Bob'});

All notations with Message Format.

See message-format for documentation.

// Assuming the locale === 'en-us',
// a basic phrase with message formatting
// returns "You took 4,000 pictures since Jan 1, 2015 9:33:04 AM"
__('You took {n,number} pictures since 
        {d,date} {d,time}', { n:4000, d:new Date() });

// a basic bracket phrase with message formatting
// returns "You took 4,000 pictures since Jan 1, 2015 9:33:04 AM"
__('[You took {n, numbers} pictures].since.date', 
        { n:4000, d:new Date() });

// a basic dot phrase with message formatting
// returns "You took 4,000 pictures since Jan 1, 2015 9:33:04 AM"
__('pictures.since.date', { n:4000, d:new Date() });

language

method
 i18n.language() 

Option name Type Description
id string

The locale to change.

return String

Then i18ned string.

Returns the name of the current locale.

i18n.language = function(id) {
  log.debug(`class: ${API.name}`, `process: i18n.languge`);
  // de-normalize locale
  var locale = core.header.getLocale();
  locale = locale.toLowerCase().replace('-', '_');
  // denormalize id
  id = id ? id.toLowerCase().replace('_', '-') : locale;
  // store the languages
  return cldr.extractLanguageDisplayNames(locale)[id];
};

Example

Get the current language.

// assuming locale === 'en-us'
// returns 'American English'
__.languages();

Get the current language in another locale.

// assuming locale === 'en-us'
// returns 'English'
__.language('en');

// returns 'Japanese'
__.language('ja');

languages

property
 i18n.languages 

Option name Type Description
arg String, Array

The locale to change or the supported locales.

supported Array

The supported locales.

return String

Then i18ned string.

Returns the names of the supported locale.

i18n.languages = (arg, supported) => {
  log.debug(`class: ${API.name}`, `process: i18n.languges`);
  var _supported = [];
  supported = (_.isArray(arg) ? arg : supported) ||
    options.header.supported;
  arg = _.isArray(arg) ? undefined : arg;
  supported.forEach(locale => {
    arg = arg ? arg.toLowerCase() :
      core.header.getLocale();
    arg = arg.replace('_', '-');
    // de-normalize locales
    locale = locale.toLowerCase().replace('-', '_');
    // store the languages
    _supported.push(cldr.extractLanguageDisplayNames(arg)[locale]);
  }, core);
  return _supported;
};

Example

Get the supported languages.

// Assuming locale === 'en-us'
// returns ['American English', 'Japanese']
__.lanugages();

Get the current languages in another locale.

// Assuming locale === 'en-us'
// returns ['アメリカ英語', '日本語']
__.languages('ja');

Override the supported locales.

// Assuming locale === 'en-us'
// returns ['English', 'Japanese']
__.languages(['en', 'ja']);

Override the supported locales and get the languages in another locale.

// Assuming locale === 'en-us'
// returns ['英語', '日本語']
__.languages('ja', ['en', 'ja']);

locale

property
 i18n.locale 

Option name Type Description
locale String

The locale to set or get.

return String

The locale.

Sets or gets the locale.

i18n.locale = (locale) => {
  log.debug(`class: ${API.name}`, `process: i18n.locale`);
  return locale ?
    core.header.setLocale(locale) :
    core.header.detectLocale ?
    core.header.detectLocale() :
    core.header.getLocale();
};

cldr

property
 i18n.cldr 

Get the cldr.

i18n.cldr = () => {
  log.debug(`class: ${API.name}`, `process: i18n.cldr`);
  return cldr;
};

catalog

property
 i18n.catalog 

Option name Type Description
locale String

The locale to find

return Object

The catalog

Returns the catalog

i18n.catalog = (locale) => {
  log.debug(`class: ${API.name}`, `process: i18n.catalog`);
  return core.backend.catalog(locale);
};
Option name Type Description
locale String

The locale to override.

return Tokei

The instance of Tokei.

l10n

l10n[options.localize] = function(...args) {
  log.debug(`class: ${API.name}`, `process: i10n`);
  return core.localize.apply(core, args);
};

Example

Get the current locale.

// Assuming locale === 'en-us'
// returns 'en-us'
__.locale()

Set the locale.

// Asumming locale === 'en-us'
// sets and returns 'ja'
__.locale('ja')
return {
  i18n: i18n,
  l10n: l10n
};
    }

get

method
 get() 

Returns the API

get() {
    log.debug(`class: ${API.name}`, `process: get`);
    return this.apply({});
  }