-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline_edit.go
51 lines (45 loc) · 1.42 KB
/
inline_edit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package inline_edit
import (
"fmt"
"html/template"
"github.com/qor/admin"
"github.com/qor/i18n"
)
func init() {
admin.RegisterViewPath("github.com/qor/i18n/inline_edit/views")
}
// FuncMap generate func map for inline edit
func FuncMap(I18n *i18n.I18n, locale string, enableInlineEdit bool) template.FuncMap {
return template.FuncMap{
"t": InlineEdit(I18n, locale, enableInlineEdit),
}
}
// InlineEdit enable inline edit
func InlineEdit(I18n *i18n.I18n, locale string, isInline bool) func(string, ...interface{}) template.HTML {
return func(key string, args ...interface{}) template.HTML {
// Get Translation Value
var value template.HTML
var defaultValue string
if len(args) > 0 {
if args[0] == nil {
defaultValue = key
} else {
defaultValue = fmt.Sprint(args[0])
}
value = I18n.Default(defaultValue).T(locale, key, args[1:]...)
} else {
value = I18n.T(locale, key)
}
// Append inline-edit script/tag
if isInline {
var editType string
if len(value) > 25 {
editType = "data-type=\"textarea\""
}
prefix := I18n.Resource.GetAdmin().GetRouter().Prefix
assetsTag := fmt.Sprintf("<script data-prefix=\"%v\" src=\"%v/assets/javascripts/i18n-checker.js?theme=i18n\"></script>", prefix, prefix)
return template.HTML(fmt.Sprintf("%s<span class=\"qor-i18n-inline\" %s data-locale=\"%s\" data-key=\"%s\">%s</span>", assetsTag, editType, locale, key, string(value)))
}
return value
}
}