UNPKG

3.34 kBJavaScriptView Raw
1var Attributes = require('./attributes');
2
3var Messages = function(lang, messages) {
4 this.lang = lang;
5 this.messages = messages;
6 this.customMessages = {};
7 this.attributeNames = {};
8};
9
10Messages.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 * Set custom attribute names.
25 *
26 * @param {object} attributes
27 */
28 _setAttributeNames: function(attributes) {
29 this.attributeNames = attributes;
30 },
31
32 /**
33 * Set the attribute formatter.
34 *
35 * @param {fuction} func
36 * @return {void}
37 */
38 _setAttributeFormatter: function(func) {
39 this.attributeFormatter = func;
40 },
41
42 /**
43 * Get attribute name to display.
44 *
45 * @param {string} attribute
46 * @return {string}
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 * Get all messages
65 *
66 * @return {object}
67 */
68 all: function() {
69 return this.messages;
70 },
71
72 /**
73 * Render message
74 *
75 * @param {Rule} rule
76 * @return {string}
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 * Get the template to use for given rule
96 *
97 * @param {Rule} rule
98 * @return {string}
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 * Replace placeholders in the template using the data object
127 *
128 * @param {Rule} rule
129 * @param {string} template
130 * @param {object} data
131 * @return {string}
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
152module.exports = Messages;