Total Lines | 45 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | package timeago |
||
2 | |||
3 | import "strings" |
||
4 | |||
5 | // overwriteOutput takes the final result, for example "4 days", |
||
6 | // and modifies the output if user added custom translations |
||
7 | // via config.Translations configuration. |
||
8 | func overwriteOutput(result string) string { |
||
9 | 1 | if !translationsAreSet() { |
|
10 | 1 | return result + " " + trans().Ago |
|
11 | } |
||
12 | |||
13 | 1 | suffix := trans().Ago |
|
14 | |||
15 | 1 | for _, translationObj := range config.Translations { |
|
16 | 1 | if translationObj.Language != config.Language { |
|
17 | 1 | continue |
|
18 | } |
||
19 | |||
20 | 1 | result, suffix = modifyResult(result, translationObj.Translations) |
|
21 | } |
||
22 | |||
23 | 1 | if suffix == "" { |
|
24 | 1 | return result |
|
25 | } |
||
26 | |||
27 | 1 | return result + " " + suffix |
|
28 | } |
||
29 | |||
30 | func modifyResult(result string, translations map[string]string) (string, string) { |
||
31 | 1 | suffix := trans().Ago |
|
32 | |||
33 | 1 | for key, translation := range translations { |
|
34 | 1 | parts := strings.Split(result, " ") |
|
35 | |||
36 | 1 | if key == trans().Ago { |
|
37 | 1 | suffix = translation |
|
38 | } |
||
39 | |||
40 | 1 | if parts[1] == key { |
|
41 | 1 | result = strings.Replace(result, key, translation, 1) |
|
42 | } |
||
43 | } |
||
44 | |||
45 | 1 | return result, suffix |
|
46 | } |
||
47 |