/
language.js
52 lines (45 loc) · 959 Bytes
/
language.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
/*!
* Lingo - Language
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var lingo = require('./lingo');
/**
* Initialize a new `Language` with the given `code` and `name`.
*
* @param {String} code
* @param {String} name
* @api public
*/
var Language = module.exports = function Language(code, name) {
this.code = code;
this.name = name;
this.translations = {};
this.rules = {
plural: []
, singular: []
, uncountable: {}
, irregular: { plural: {}, singular: {}}
};
lingo[code] = this;
};
/**
* Translate the given `str` with optional `params`.
*
* @param {String} str
* @param {Object} params
* @return {String}
* @api public
*/
Language.prototype.translate = function(str, params){
str = this.translations[str] || str;
if (params) {
str = str.replace(/\{([^}]+)\}/g, function(_, key){
return params[key];
});
}
return str;
};