/
I18n.js
99 lines (81 loc) · 1.67 KB
/
I18n.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
var neolao = require('./index'),
Locale = require('./i18n/Locale');
/**
* Internationalization
*
* @class neolao/I18n
*/
function klass()
{
this._locales = {};
};
proto = klass.prototype;
/**
* The default locale string
*
* @type String
*/
proto.defaultLocaleString = null;
/**
* The singleton instance
*
* @type neolao/I18n
*/
proto._instance = null;
/**
* Locale list
*
* @type Object
*/
proto._locales = null;
/**
* Get the representation string
*
* @return String The representation string
*/
proto.toString = function()
{
return '[neolao/I18n]';
};
/**
* Get a message from the default locale
*
* @param String messageId The message id
* @param Array parameters The parameters
* @return String The translated message
*/
proto.getMessage = function(messageId, parameters)
{
var message = messageId,
locale = this._locales[this.defaultLocaleString];
if (locale instanceof Locale) {
message = locale.getMessage(messageId, parameters);
}
return message;
};
proto._ = proto.getMessage; // Alias
/**
* Add a locale
*
* @param neolao/i18n/Locale locale Locale instance
*/
proto.addLocale = function(locale)
{
this._locales[locale.localeString] = locale;
// Set this locale as default
this.defaultLocaleString = locale.localeString;
};
/**
* Get singleton instance
*
* @return neolao/I18n Singleton instance
*/
klass.getInstance = function()
{
if (!this._instance) {
this._instance = new klass();
}
return this._instance;
};
// Export the singleton instance
module.exports = klass.getInstance();