1
1
package revel
2
2
3
3
import (
4
+ "bytes"
5
+ "errors"
4
6
"fmt"
7
+ //"github.com/revel/config"
8
+ "github.com/xeonx/timeago"
5
9
"html"
6
10
"html/template"
7
11
"reflect"
8
12
"strings"
9
13
"time"
10
- "bytes"
11
- "errors"
12
14
)
13
15
14
16
var (
@@ -145,6 +147,9 @@ var (
145
147
},
146
148
"slug" : Slug ,
147
149
"even" : func (a int ) bool { return (a % 2 ) == 0 },
150
+
151
+ // Using https://github.com/xeonx/timeago
152
+ "timeago" : TimeAgo ,
148
153
"i18ntemplate" : func (args ... interface {}) (template.HTML , error ) {
149
154
templateName , lang := "" , ""
150
155
var viewArgs interface {}
@@ -159,16 +164,16 @@ var (
159
164
templateName = args [0 ].(string )
160
165
viewArgs = args [1 ]
161
166
// Try to extract language from the view args
162
- if viewargsmap ,ok := viewArgs .(map [string ]interface {});ok {
163
- lang ,_ = viewargsmap [CurrentLocaleViewArg ].(string )
167
+ if viewargsmap , ok := viewArgs .(map [string ]interface {}); ok {
168
+ lang , _ = viewargsmap [CurrentLocaleViewArg ].(string )
164
169
}
165
170
default :
166
171
// Assume third argument is the region
167
172
templateName = args [0 ].(string )
168
173
viewArgs = args [1 ]
169
174
lang , _ = args [2 ].(string )
170
- if len (args )> 3 {
171
- ERROR .Printf ("Received more parameters then needed for %s" ,templateName )
175
+ if len (args ) > 3 {
176
+ ERROR .Printf ("Received more parameters then needed for %s" , templateName )
172
177
}
173
178
}
174
179
@@ -178,12 +183,13 @@ var (
178
183
if err == nil {
179
184
err = tmpl .Render (& buf , viewArgs )
180
185
} else {
181
- ERROR .Printf ("Failed to render i18ntemplate %s %v" ,templateName ,err )
186
+ ERROR .Printf ("Failed to render i18ntemplate %s %v" , templateName , err )
182
187
}
183
188
return template .HTML (buf .String ()), err
184
189
},
185
190
}
186
191
)
192
+
187
193
/////////////////////
188
194
// Template functions
189
195
/////////////////////
@@ -208,13 +214,13 @@ func ReverseURL(args ...interface{}) (template.URL, error) {
208
214
209
215
// Look up the types.
210
216
211
- if pathData .TypeOfController == nil {
217
+ if pathData .TypeOfController == nil {
212
218
return "" , fmt .Errorf ("Failed reversing %s: controller not found %#v" , action , pathData )
213
219
}
214
220
215
221
// Note method name is case insensitive search
216
222
methodType := pathData .TypeOfController .Method (pathData .MethodName )
217
- if methodType == nil {
223
+ if methodType == nil {
218
224
return "" , errors .New ("revel/controller: In " + action + " failed to find function " + pathData .MethodName )
219
225
}
220
226
@@ -228,7 +234,7 @@ func ReverseURL(args ...interface{}) (template.URL, error) {
228
234
fixedParams := len (pathData .FixedParamsByName )
229
235
230
236
for i , argValue := range args [1 :] {
231
- Unbind (argsByName , methodType .Args [i + fixedParams ].Name , argValue )
237
+ Unbind (argsByName , methodType .Args [i + fixedParams ].Name , argValue )
232
238
}
233
239
234
240
return template .URL (MainRouter .Reverse (args [0 ].(string ), argsByName ).URL ), nil
@@ -242,3 +248,77 @@ func Slug(text string) string {
242
248
text = strings .Trim (text , separator )
243
249
return text
244
250
}
251
+
252
+ var timeAgoLangs = map [string ]timeago.Config {}
253
+
254
+ func TimeAgo (args ... interface {}) string {
255
+
256
+ datetime := time .Now ()
257
+ lang := ""
258
+ var viewArgs interface {}
259
+ switch len (args ) {
260
+ case 0 :
261
+ ERROR .Printf ("No arguements passed to timeago" )
262
+ case 1 :
263
+ // only the time is passed in
264
+ datetime = args [0 ].(time.Time )
265
+ case 2 :
266
+ // time and region is passed in
267
+ datetime = args [0 ].(time.Time )
268
+ switch v := reflect .ValueOf (args [1 ]); v .Kind () {
269
+ case reflect .String :
270
+ // second params type string equals region
271
+ lang , _ = args [1 ].(string )
272
+ case reflect .Map :
273
+ // second params type map equals viewArgs
274
+ viewArgs = args [1 ]
275
+ if viewargsmap , ok := viewArgs .(map [string ]interface {}); ok {
276
+ lang , _ = viewargsmap [CurrentLocaleViewArg ].(string )
277
+ }
278
+ default :
279
+ ERROR .Println ("pluralize: unexpected type: " , v )
280
+ }
281
+ default :
282
+ // Assume third argument is the region
283
+ datetime = args [0 ].(time.Time )
284
+ if reflect .ValueOf (args [1 ]).Kind () != reflect .Map {
285
+ ERROR .Println ("pluralize: unexpected type: " , args [1 ])
286
+ }
287
+ if reflect .ValueOf (args [2 ]).Kind () != reflect .String {
288
+ ERROR .Println ("unexpected type: " , args [2 ])
289
+ }
290
+ viewArgs = args [1 ]
291
+ lang , _ = args [2 ].(string )
292
+ if len (args ) > 3 {
293
+ ERROR .Printf ("Received more parameters then needed for timeago" )
294
+ }
295
+ }
296
+ if lang == "" {
297
+ lang , _ = Config .String (defaultLanguageOption )
298
+ if lang == "en" {
299
+ timeAgoLangs [lang ] = timeago .English
300
+ }
301
+ }
302
+ _ , ok := timeAgoLangs [lang ]
303
+ if ! ok {
304
+ timeAgoLangs [lang ] = timeago.Config {
305
+ PastPrefix : "" ,
306
+ PastSuffix : " " + MessageFunc (lang , "ago" ),
307
+ FuturePrefix : MessageFunc (lang , "in" ) + " " ,
308
+ FutureSuffix : "" ,
309
+ Periods : []timeago.FormatPeriod {
310
+ timeago.FormatPeriod {time .Second , MessageFunc (lang , "about a second" ), MessageFunc (lang , "%d seconds" )},
311
+ timeago.FormatPeriod {time .Minute , MessageFunc (lang , "about a minute" ), MessageFunc (lang , "%d minutes" )},
312
+ timeago.FormatPeriod {time .Hour , MessageFunc (lang , "about an hour" ), MessageFunc (lang , "%d hours" )},
313
+ timeago.FormatPeriod {timeago .Day , MessageFunc (lang , "one day" ), MessageFunc (lang , "%d days" )},
314
+ timeago.FormatPeriod {timeago .Month , MessageFunc (lang , "one month" ), MessageFunc (lang , "%d months" )},
315
+ timeago.FormatPeriod {timeago .Year , MessageFunc (lang , "one year" ), MessageFunc (lang , "%d years" )},
316
+ },
317
+ Zero : MessageFunc (lang , "about a second" ),
318
+ Max : 73 * time .Hour ,
319
+ DefaultLayout : "2006-01-02" ,
320
+ }
321
+
322
+ }
323
+ return timeAgoLangs [lang ].Format (datetime )
324
+ }
0 commit comments