Skip to content
This repository
Browse code

More i18n for model datatypes.

  • Loading branch information...
commit 0d2a4b45711252a084e4884377196b18dd897bb9 1 parent d65508c
Matthew Eernisse authored March 12, 2012
67  lib/model/datatypes.js
@@ -16,6 +16,8 @@
16 16
  *
17 17
 */
18 18
 
  19
+var i18n = require('../i18n');
  20
+
19 21
 /*
20 22
  * Datatype verification -- may modify the value by casting
21 23
  */
@@ -31,21 +33,21 @@ var datatypes = new (function () {
31 33
 
32 34
   this.string = function (name, val, locale) {
33 35
     return {
34  
-      err: null,
35  
-      val: String(val)
  36
+      err: null
  37
+    , val: String(val)
36 38
     };
37 39
   };
38 40
 
39 41
   this.number = function (name, val, locale) {
40 42
     if (isNaN(val)) {
41 43
       return {
42  
-        err: 'Field "' + name + '" must be a Number.',
43  
-        val: null
  44
+        err: i18n.getText('model.validatesNumber', {name: name}, locale)
  45
+      , val: null
44 46
       };
45 47
     };
46 48
     return {
47  
-      err: null,
48  
-      val: Number(val)
  49
+      err: null
  50
+    , val: Number(val)
49 51
     };
50 52
   };
51 53
 
@@ -53,13 +55,13 @@ var datatypes = new (function () {
53 55
     // Allow decimal values like 10.0 and 3.0
54 56
     if (Math.round(val) != val) {
55 57
       return {
56  
-        err: 'Field "' + name + '" must be an integer.',
57  
-        val: null
  58
+        err: i18n.getText('model.validatesInteger', {name: name}, locale)
  59
+      , val: null
58 60
       };
59 61
     };
60 62
     return {
61  
-      err: null,
62  
-      val: parseInt(val, 10)
  63
+      err: null
  64
+    , val: parseInt(val, 10)
63 65
     };
64 66
   };
65 67
 
@@ -91,30 +93,29 @@ var datatypes = new (function () {
91 93
 
92 94
     if (typeof validated != 'boolean') {
93 95
       return {
94  
-        err: 'Field "' + name + '" must be a Boolean.',
95  
-        val: null
  96
+        err: i18n.getText('model.validatesBoolean', {name: name}, locale)
  97
+      , val: null
96 98
       };
97 99
     };
98 100
     return {
99  
-      err: null,
100  
-      val: validated
  101
+      err: null
  102
+      , val: validated
101 103
     };
102 104
   };
103 105
 
104 106
   this.object = function (name, val, locale) {
105 107
     // Sure, Arrays are technically Objects, but we're treating Array as a
106 108
     // separate datatype. Remember, instanceof Array fails across window
107  
-    // boundaries, so let's also make sure the Object doesn't have a 'length'
108  
-    // property.
  109
+    // boundaries, so let's also make sure the Object isn't Array-ish
109 110
     if (typeof val != 'object' || _isArray(val)) {
110 111
       return {
111  
-        err: 'Field "' + name + '" must be an Object.',
112  
-        val: null
  112
+        err: i18n.getText('model.validatesObject', {name: name}, locale)
  113
+      , val: null
113 114
       };
114 115
     };
115 116
     return {
116  
-      err: null,
117  
-      val: val
  117
+      err: null
  118
+    , val: val
118 119
     };
119 120
   };
120 121
 
@@ -123,13 +124,13 @@ var datatypes = new (function () {
123 124
     // to make sure there's a length property
124 125
     if (!_isArray(val)) {
125 126
       return {
126  
-        err: 'Field "' + name + '" must be an Array.',
127  
-        val: null
  127
+        err: i18n.getText('model.validatesArray', {name: name}, locale)
  128
+      , val: null
128 129
       };
129 130
     };
130 131
     return {
131  
-      err: null,
132  
-      val: val
  132
+      err: null
  133
+    , val: val
133 134
     };
134 135
   };
135 136
 
@@ -137,14 +138,14 @@ var datatypes = new (function () {
137 138
     var dt = geddy.date.parse(val);
138 139
     if (dt) {
139 140
       return {
140  
-        err: null,
141  
-        val: dt
  141
+        err: null
  142
+      , val: dt
142 143
       };
143 144
     }
144 145
     else {
145 146
       return {
146  
-        err: 'Field "' + name + '" must be in a valid date format.',
147  
-        val: null
  147
+        err: i18n.getText('model.validatesDate', {name: name}, locale)
  148
+      , val: null
148 149
       };
149 150
     }
150 151
   };
@@ -153,14 +154,14 @@ var datatypes = new (function () {
153 154
     var dt = geddy.date.parse(val);
154 155
     if (dt) {
155 156
       return {
156  
-        err: null,
157  
-        val: dt
  157
+        err: null
  158
+      , val: dt
158 159
       };
159 160
     }
160 161
     else {
161 162
       return {
162  
-        err: 'Field "' + name + '" must be in a valid datetime format.',
163  
-        val: null
  163
+        err: i18n.getText('model.validatesDatetime', {name: name}, locale)
  164
+      , val: null
164 165
       };
165 166
     }
166 167
   };
@@ -177,7 +178,7 @@ var datatypes = new (function () {
177 178
     }
178 179
     else {
179 180
       return {
180  
-        err: 'Field "' + name + '" must be in a valid time format.',
  181
+        err: i18n.getText('model.validatesTime', {name: name}, locale)
181 182
         val: null
182 183
       };
183 184
     }
7  templates/locales/en-us.json
@@ -7,4 +7,11 @@
7 7
 , "model.validatesMinLength": "\"{name}\" must be at least {min} characters long."
8 8
 , "model.validatesMaxLength": "\"{name}\" may not be more than {max} characters long."
9 9
 , "model.validatesWithFunction": "\"{name}\" is not valid."
  10
+, "model.validatesNumber": "\"{name}\" must be a number."
  11
+, "model.validatesInteger": "\"{name}\" must be an integer."
  12
+, "model.validatesObject": "\"{name}\" must be an object."
  13
+, "model.validatesArray": "\"{name}\" must be an array."
  14
+, "model.validatesDate": "\"{name}\" must be in a valid date format."
  15
+, "model.validatesDatetime": "\"{name}\" must be in a valid datetime format."
  16
+, "model.validatesTime": "\"{name}\" must be in a valid time format."
10 17
 }

0 notes on commit 0d2a4b4

Please sign in to comment.
Something went wrong with that request. Please try again.