Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bd534eb

Browse filesBrowse files
committed
Refactor Timeago Template Func
1 parent bed7bff commit bd534eb
Copy full SHA for bd534eb

File tree

1 file changed

+76
-27
lines changed
Filter options

1 file changed

+76
-27
lines changed

‎template_functions.go

Copy file name to clipboardExpand all lines: template_functions.go
+76-27Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
//"github.com/revel/config"
78
"github.com/xeonx/timeago"
89
"html"
910
"html/template"
@@ -148,33 +149,7 @@ var (
148149
"even": func(a int) bool { return (a % 2) == 0 },
149150

150151
// 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,
178153
"i18ntemplate": func(args ...interface{}) (template.HTML, error) {
179154
templateName, lang := "", ""
180155
var viewArgs interface{}
@@ -273,3 +248,77 @@ func Slug(text string) string {
273248
text = strings.Trim(text, separator)
274249
return text
275250
}
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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.