4
4
"bytes"
5
5
"errors"
6
6
"fmt"
7
+ //"github.com/revel/config"
7
8
"github.com/xeonx/timeago"
8
9
"html"
9
10
"html/template"
@@ -148,33 +149,7 @@ var (
148
149
"even" : func (a int ) bool { return (a % 2 ) == 0 },
149
150
150
151
// Using https://github.com/xeonx/timeago
151
- "timeago" : func (viewArgs map [string ]interface {}, datetime time.Time ) string {
152
-
153
- localeStr , ok := viewArgs [CurrentLocaleViewArg ].(string )
154
- if ! ok || strings .Contains (localeStr , "en" ) {
155
- return timeago .English .Format (datetime )
156
- }
157
- // internalization by current locale
158
- var customLanguage = timeago.Config {
159
- PastPrefix : "" ,
160
- PastSuffix : " " + MessageFunc (localeStr , "ago" ),
161
- FuturePrefix : MessageFunc (localeStr , "in" ) + " " ,
162
- FutureSuffix : "" ,
163
- Periods : []timeago.FormatPeriod {
164
- timeago.FormatPeriod {time .Second , MessageFunc (localeStr , "about a second" ), MessageFunc (localeStr , "%d seconds" )},
165
- timeago.FormatPeriod {time .Minute , MessageFunc (localeStr , "about a minute" ), MessageFunc (localeStr , "%d minutes" )},
166
- timeago.FormatPeriod {time .Hour , MessageFunc (localeStr , "about an hour" ), MessageFunc (localeStr , "%d hours" )},
167
- timeago.FormatPeriod {timeago .Day , MessageFunc (localeStr , "one day" ), MessageFunc (localeStr , "%d days" )},
168
- timeago.FormatPeriod {timeago .Month , MessageFunc (localeStr , "one month" ), MessageFunc (localeStr , "%d months" )},
169
- timeago.FormatPeriod {timeago .Year , MessageFunc (localeStr , "one year" ), MessageFunc (localeStr , "%d years" )},
170
- },
171
-
172
- Zero : MessageFunc (localeStr , "about a second" ),
173
- Max : 73 * time .Hour ,
174
- DefaultLayout : "2006-01-02" ,
175
- }
176
- return customLanguage .Format (datetime )
177
- },
152
+ "timeago" : TimeAgo ,
178
153
"i18ntemplate" : func (args ... interface {}) (template.HTML , error ) {
179
154
templateName , lang := "" , ""
180
155
var viewArgs interface {}
@@ -273,3 +248,77 @@ func Slug(text string) string {
273
248
text = strings .Trim (text , separator )
274
249
return text
275
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