Class: Get

Get(locales, defaultLocaleopt)

new Get(locales, defaultLocaleopt)

Accessor for compiled MessageFormat functions

Parameters:
Name Type Attributes Description
locales object

A map of locale codes to their function objects

defaultLocale string | null <optional>

If not defined, default and initial locale is the first entry of locales

Source:
Example
var fs = require('fs');
var MessageFormat = require('messageformat');
var mf = new MessageFormat(['en', 'fi']);
var msgSet = {
  en: {
    a: 'A {TYPE} example.',
    b: 'This has {COUNT, plural, one{one user} other{# users}}.',
    c: {
      d: 'We have {P, number, percent} code coverage.'
    }
  },
  fi: {
    b: 'Tällä on {COUNT, plural, one{yksi käyttäjä} other{# käyttäjää}}.',
    e: 'Minä puhun vain suomea.'
  }
};
var cfStr = mf.compile(msgSet).toString('module.exports');
fs.writeFileSync('messages.js', cfStr);

...

var MessageFormatGet = require('messageformat/lib/get');
var msgData = require('./messages');
var messages = new MessageFormatGet(msgData, 'en');

messages.hasMessage('a')                // true
messages.hasObject('c')                 // true
messages.get('b', { COUNT: 3 })         // 'This has 3 users.'
messages.get(['c', 'd'], { P: 0.314 })  // 'We have 31% code coverage.'

messages.get('e')                       // 'e'
messages.setFallback('en', ['foo', 'fi'])
messages.get('e')                       // 'Minä puhun vain suomea.'

messages.locale = 'fi'
messages.hasMessage('a')                // false
messages.hasMessage('a', 'en')          // true
messages.hasMessage('a', null, true)    // true
messages.hasObject('c')                 // false
messages.get('b', { COUNT: 3 })         // 'Tällä on 3 käyttäjää.'
messages.get('c').d({ P: 0.628 })       // 'We have 63% code coverage.'

Methods

addMessages(data, lcopt, keypathopt) → {Get}

Add new messages to the accessor; useful if loading data dynamically

The locale code lc should be an exact match for the locale being updated, or empty to default to the current locale. Use #resolveLocale for resolving partial locale strings.

If keypath is empty, adds or sets the complete message object for the corresponding locale. If any keys in keypath do not exist, a new object will be created at that key.

Parameters:
Name Type Attributes Description
data function | object

Hierarchical map of keys to functions, or a single message function

lc string <optional>

If empty or undefined, defaults to this.locale

keypath Array.<string> <optional>

The keypath being added

Source:
Returns:

The Get instance, to allow for chaining

Type
Get

get(key, propsopt, lcopt) → {string|Object.<string, (function()|object)>}

Get the message or object corresponding to key

key may be a string for functions at the root level, or string[] for accessing hierarchical objects. If an exact match is not found, the fallback locales are checked for the first match.

If key maps to a message function, it will be called with props. If it maps to an object, the object is returned directly.

Parameters:
Name Type Attributes Description
key string | Array.<string>

The key or keypath being sought

props object <optional>

Optional properties passed to the function

lc string <optional>

If empty or undefined, defaults to this.locale

Source:
Returns:
Type
string | Object.<string, (function()|object)>

getFallback(lcopt) → {Array.<string>}

Get the list of fallback locales

Parameters:
Name Type Attributes Description
lc string <optional>

If empty or undefined, defaults to this.locale

Source:
Returns:
Type
Array.<string>

hasMessage(key, lcopt, fallbackopt) → {boolean}

Check if key is a message function for the locale

key may be a string for functions at the root level, or string[] for accessing hierarchical objects. If an exact match is not found and fallback is true, the fallback locales are checked for the first match.

Parameters:
Name Type Attributes Default Description
key string | Array.<string>

The key or keypath being sought

lc string <optional>

If empty or undefined, defaults to this.locale

fallback boolean <optional>
false

If true, also checks fallback locales

Source:
Returns:
Type
boolean

hasObject(key, lcopt, fallbackopt) → {boolean}

Check if key is a message object for the locale

key may be a string for functions at the root level, or string[] for accessing hierarchical objects. If an exact match is not found and fallback is true, the fallback locales are checked for the first match.

Parameters:
Name Type Attributes Default Description
key string | Array.<string>

The key or keypath being sought

lc string <optional>

If empty or undefined, defaults to this.locale

fallback boolean <optional>
false

If true, also checks fallback locales

Source:
Returns:
Type
boolean

resolveLocale(lc) → {string|null}

Resolve lc to the key of an available locale or null, allowing for partial matches. For example, with an en locale defined, it will be selected by messages.defaultLocale = 'en-US' and vice versa.

Parameters:
Name Type Description
lc string

Locale code

Source:
Returns:
Type
string | null

setFallback(lc, fallback) → {Get}

Set the fallback locale or locales for lc

To disable fallback for the locale, use setFallback(lc, []). To use the default fallback, use setFallback(lc, null).

Parameters:
Name Type Description
lc string
fallback Array.<string> | null
Source:
Returns:

The Get instance, to allow for chaining

Type
Get