Passed
Push — v3.0.0 ( c00ed6...da2d19 )
by Serhii
01:12
created

langset.go   A

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 67
dl 0
loc 117
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
F timeago.applyCustomTranslations 0 70 22
A timeago.newLangSet 0 20 3
1
package timeago
2
3
import (
4
	"fmt"
5
	"path"
6
	"runtime"
7
)
8
9
type LangForms map[string]string
10
11
type LangSet struct {
12
	Lang    string    `json:"lang"`
13
	Format  string    `json:"format"`
14
	Ago     string    `json:"ago"`
15
	Online  string    `json:"online"`
16
	JustNow string    `json:"justnow"`
17
	Second  LangForms `json:"second"`
18
	Minute  LangForms `json:"minute"`
19
	Hour    LangForms `json:"hour"`
20
	Day     LangForms `json:"day"`
21
	Week    LangForms `json:"week"`
22
	Month   LangForms `json:"month"`
23
	Year    LangForms `json:"year"`
24
}
25
26
func newLangSet() (*LangSet, error) {
27
	_, filename, _, ok := runtime.Caller(0)
28
29
	if !ok {
30
		return nil, createError("No called information")
31
	}
32
33
	rootPath := path.Dir(filename)
34
	filePath := fmt.Sprintf(rootPath+"/langs/%s.json", conf.Language)
35
36
	if cache, ok := cachedJsonRes[filePath]; ok {
37
		return cache, nil
38
	}
39
40
	langSet := parseLangSet(filePath)
41
	langSet = applyCustomTranslations(langSet)
42
43
	cachedJsonRes[filePath] = langSet
44
45
	return langSet, nil
46
}
47
48
func applyCustomTranslations(langSet *LangSet) *LangSet {
49
	if len(conf.Translations) == 0 {
50
		return langSet
51
	}
52
53
	for _, trans := range conf.Translations {
54
		if trans.Lang != langSet.Lang {
55
			continue
56
		}
57
58
		if trans.Format != "" {
59
			langSet.Format = trans.Format
60
		}
61
62
		if trans.Ago != "" {
63
			langSet.Ago = trans.Ago
64
		}
65
66
		if trans.Online != "" {
67
			langSet.Online = trans.Online
68
		}
69
70
		if trans.JustNow != "" {
71
			langSet.JustNow = trans.JustNow
72
		}
73
74
		if trans.Second != nil {
75
			for k, v := range trans.Second {
76
				langSet.Second[k] = v
77
			}
78
		}
79
80
		if trans.Minute != nil {
81
			for k, v := range trans.Minute {
82
				langSet.Minute[k] = v
83
			}
84
		}
85
86
		if trans.Hour != nil {
87
			for k, v := range trans.Hour {
88
				langSet.Hour[k] = v
89
			}
90
		}
91
92
		if trans.Day != nil {
93
			for k, v := range trans.Day {
94
				langSet.Day[k] = v
95
			}
96
		}
97
98
		if trans.Week != nil {
99
			for k, v := range trans.Week {
100
				langSet.Week[k] = v
101
			}
102
		}
103
104
		if trans.Month != nil {
105
			for k, v := range trans.Month {
106
				langSet.Month[k] = v
107
			}
108
		}
109
110
		if trans.Year != nil {
111
			for k, v := range trans.Year {
112
				langSet.Year[k] = v
113
			}
114
		}
115
	}
116
117
	return langSet
118
}
119