1 | var Attributes = require('./attributes');
|
2 |
|
3 | var Messages = function(lang, messages) {
|
4 | this.lang = lang;
|
5 | this.messages = messages;
|
6 | this.customMessages = {};
|
7 | this.attributeNames = {};
|
8 | };
|
9 |
|
10 | Messages.prototype = {
|
11 | constructor: Messages,
|
12 |
|
13 | /**
|
14 | * Set custom messages
|
15 | *
|
16 | * @param {object} customMessages
|
17 | * @return {void}
|
18 | */
|
19 | _setCustom: function(customMessages) {
|
20 | this.customMessages = customMessages || {};
|
21 | },
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | _setAttributeNames: function(attributes) {
|
29 | this.attributeNames = attributes;
|
30 | },
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | _setAttributeFormatter: function(func) {
|
39 | this.attributeFormatter = func;
|
40 | },
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | _getAttributeName: function(attribute) {
|
49 | var name = attribute;
|
50 | if (this.attributeNames.hasOwnProperty(attribute)) {
|
51 | return this.attributeNames[attribute];
|
52 | } else if (this.messages.attributes.hasOwnProperty(attribute)) {
|
53 | name = this.messages.attributes[attribute];
|
54 | }
|
55 |
|
56 | if (this.attributeFormatter) {
|
57 | name = this.attributeFormatter(name);
|
58 | }
|
59 |
|
60 | return name;
|
61 | },
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | all: function() {
|
69 | return this.messages;
|
70 | },
|
71 |
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | render: function(rule) {
|
79 | if (rule.customMessage) {
|
80 | return rule.customMessage;
|
81 | }
|
82 | var template = this._getTemplate(rule);
|
83 |
|
84 | var message;
|
85 | if (Attributes.replacements[rule.name]) {
|
86 | message = Attributes.replacements[rule.name].apply(this, [template, rule]);
|
87 | } else {
|
88 | message = this._replacePlaceholders(rule, template, {});
|
89 | }
|
90 |
|
91 | return message;
|
92 | },
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | _getTemplate: function(rule) {
|
101 |
|
102 | var messages = this.messages;
|
103 | var template = messages.def;
|
104 | var customMessages = this.customMessages;
|
105 | var formats = [rule.name + '.' + rule.attribute, rule.name];
|
106 |
|
107 | for (var i = 0, format; i < formats.length; i++) {
|
108 | format = formats[i];
|
109 | if (customMessages.hasOwnProperty(format)) {
|
110 | template = customMessages[format];
|
111 | break;
|
112 | } else if (messages.hasOwnProperty(format)) {
|
113 | template = messages[format];
|
114 | break;
|
115 | }
|
116 | }
|
117 |
|
118 | if (typeof template === 'object') {
|
119 | template = template[rule._getValueType()];
|
120 | }
|
121 |
|
122 | return template;
|
123 | },
|
124 |
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | _replacePlaceholders: function(rule, template, data) {
|
134 | var message, attribute;
|
135 |
|
136 | data.attribute = this._getAttributeName(rule.attribute);
|
137 | data[rule.name] = data[rule.name] || rule.getParameters().join(',');
|
138 |
|
139 | if (typeof template === 'string' && typeof data === 'object') {
|
140 | message = template;
|
141 |
|
142 | for (attribute in data) {
|
143 | message = message.replace(new RegExp(':' + attribute, 'g'), data[attribute]);
|
144 | }
|
145 | }
|
146 |
|
147 | return message;
|
148 | }
|
149 |
|
150 | };
|
151 |
|
152 | module.exports = Messages;
|