new Messages(locales [, defaultLocale])
Parameters:
| Name | Type | Argument | 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 |
- 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 Messages = require('messageformat/messages');
var msgData = require('./messages');
var messages = new Messages(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.'
Members
-
<static, readonly> availableLocales :Array.<string>
-
List of available locales
Type:
- Array.<string>
- Source:
-
<static> defaultLocale :string|null
-
Default fallback locale
One of Messages#availableLocales or
null. Partial matches of language tags are supported, so e.g. with anenlocale defined, it will be selected bymessages.defaultLocale = 'en-US'and vice versa.Type:
- string | null
- Source:
-
<static> locale :string|null
-
Current locale
One of Messages#availableLocales or
null. Partial matches of language tags are supported, so e.g. with anenlocale defined, it will be selected bymessages.locale = 'en-US'and vice versa.Type:
- string | null
- Source:
Methods
-
addMessages(data [, lc] [, keypath])
-
Add new messages to the accessor; useful if loading data dynamically
The locale code
lcshould 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
keypathis empty, adds or sets the complete message object for the corresponding locale. If any keys inkeypathdo not exist, a new object will be created at that key.Parameters:
Name Type Argument Description datafunction | object Hierarchical map of keys to functions, or a single message function
lcstring <optional>
If empty or undefined, defaults to
this.localekeypathArray.<string> <optional>
The keypath being added
- Source:
Returns:
The Messages instance, to allow for chaining
- Type
- Messages
-
get(key [, props] [, lc])
-
Get the message or object corresponding to
keykeymay be astringfor functions at the root level, orstring[]for accessing hierarchical objects. If an exact match is not found, the fallback locales are checked for the first match.If
keymaps to a message function, it will be called withprops. If it maps to an object, the object is returned directly.Parameters:
Name Type Argument Description keystring | Array.<string> The key or keypath being sought
propsobject <optional>
Optional properties passed to the function
lcstring <optional>
If empty or undefined, defaults to
this.locale- Source:
Returns:
- Type
- string | Object.<string, (function()|object)>
-
getFallback( [lc])
-
Get the list of fallback locales
Parameters:
Name Type Argument Description lcstring <optional>
If empty or undefined, defaults to
this.locale- Source:
Returns:
- Type
- Array.<string>
-
hasMessage(key [, lc] [, fallback])
-
Check if
keyis a message function for the localekeymay be astringfor functions at the root level, orstring[]for accessing hierarchical objects. If an exact match is not found andfallbackis true, the fallback locales are checked for the first match.Parameters:
Name Type Argument Default Description keystring | Array.<string> The key or keypath being sought
lcstring <optional>
If empty or undefined, defaults to
this.localefallbackboolean <optional>
false If true, also checks fallback locales
- Source:
Returns:
- Type
- boolean
-
hasObject(key [, lc] [, fallback])
-
Check if
keyis a message object for the localekeymay be astringfor functions at the root level, orstring[]for accessing hierarchical objects. If an exact match is not found andfallbackis true, the fallback locales are checked for the first match.Parameters:
Name Type Argument Default Description keystring | Array.<string> The key or keypath being sought
lcstring <optional>
If empty or undefined, defaults to
this.localefallbackboolean <optional>
false If true, also checks fallback locales
- Source:
Returns:
- Type
- boolean
-
resolveLocale(lc)
-
Resolve
lcto the key of an available locale ornull, allowing for partial matches. For example, with anenlocale defined, it will be selected bymessages.defaultLocale = 'en-US'and vice versa.Parameters:
Name Type Description lcstring Locale code
- Source:
Returns:
- Type
- string | null
-
setFallback(lc, fallback)
-
Set the fallback locale or locales for
lcTo disable fallback for the locale, use
setFallback(lc, []). To use the default fallback, usesetFallback(lc, null).Parameters:
Name Type Description lcstring fallbackArray.<string> | null - Source:
Returns:
The Messages instance, to allow for chaining
- Type
- Messages