-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathmessage.js
115 lines (95 loc) · 2.96 KB
/
message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
define([
"cldr",
"messageformat",
"./common/create-error/plural-module-presence",
"./common/runtime-bind",
"./common/validate/default-locale",
"./common/validate/message-bundle",
"./common/validate/message-presence",
"./common/validate/message-type",
"./common/validate/parameter-presence",
"./common/validate/parameter-type",
"./common/validate/parameter-type/plain-object",
"./core",
"./message/formatter-fn",
"./message/formatter-runtime-bind",
"./util/always-array",
"cldr/event"
], function( Cldr, MessageFormat, createErrorPluralModulePresence, runtimeBind,
validateDefaultLocale, validateMessageBundle, validateMessagePresence, validateMessageType,
validateParameterPresence, validateParameterType, validateParameterTypePlainObject, Globalize,
messageFormatterFn, messageFormatterRuntimeBind, alwaysArray ) {
var slice = [].slice;
/**
* .loadMessages( json )
*
* @json [JSON]
*
* Load translation data.
*/
Globalize.loadMessages = function( json ) {
var locale,
customData = {
"globalize-messages": json,
"main": {}
};
validateParameterPresence( json, "json" );
validateParameterTypePlainObject( json, "json" );
// Set available bundles by populating customData main dataset.
for ( locale in json ) {
if ( json.hasOwnProperty( locale ) ) {
customData.main[ locale ] = {};
}
}
Cldr.load( customData );
};
/**
* .messageFormatter( path )
*
* @path [String or Array]
*
* Format a message given its path.
*/
Globalize.messageFormatter =
Globalize.prototype.messageFormatter = function( path ) {
var cldr, formatter, message, pluralGenerator, returnFn,
args = slice.call( arguments, 0 );
validateParameterPresence( path, "path" );
validateParameterType( path, "path", typeof path === "string" || Array.isArray( path ),
"a String nor an Array" );
path = alwaysArray( path );
cldr = this.cldr;
validateDefaultLocale( cldr );
validateMessageBundle( cldr );
message = cldr.get( [ "globalize-messages/{bundle}" ].concat( path ) );
validateMessagePresence( path, message );
// If message is an Array, concatenate it.
if ( Array.isArray( message ) ) {
message = message.join( " " );
}
validateMessageType( path, message );
// Is plural module present? Yes, use its generator. Nope, use an error generator.
pluralGenerator = this.plural !== undefined ?
this.pluralGenerator() :
createErrorPluralModulePresence;
formatter = new MessageFormat( cldr.locale, pluralGenerator ).compile( message );
returnFn = messageFormatterFn( formatter );
runtimeBind( args, cldr, returnFn,
[ messageFormatterRuntimeBind( cldr, formatter ), pluralGenerator ] );
return returnFn;
};
/**
* .formatMessage( path [, variables] )
*
* @path [String or Array]
*
* @variables [Number, String, Array or Object]
*
* Format a message given its path.
*/
Globalize.formatMessage =
Globalize.prototype.formatMessage = function( path /* , variables */ ) {
return this.messageFormatter( path ).apply( {}, slice.call( arguments, 1 ) );
};
return Globalize;
});