-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
136 lines (109 loc) · 3.93 KB
/
get.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package translator
import (
"fmt"
)
// Tl translates a string based on the given language tag and key.
func (t *Translator) tl(loc Localizer, key string, args ...any) string {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return fmt.Sprintf(DefaultNoTranslationTL, key)
}
if !translator.IsTranslated(key) {
// check if translation is in the pot file, if not, add it
if _, ok := t.uniqueKeys[key]; !ok {
t.uniqueKeys[key] = uniqueKey{singular: key}
err := t.addToPotFileIfNotExists(translationKey{"", key, false})
if err != nil {
fmt.Println(err)
}
}
return fmt.Sprintf(DefaultNoTranslationTL, key)
}
translated := translator.Get(fmt.Sprintf("%s", key), args...) //nolint:gosimple
return t.removePrefix(translated)
}
func (t *Translator) ctl(loc Localizer, ctx, key string, args ...any) string {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return fmt.Sprintf(DefaultNoTranslationCTL, ctx, key)
}
if ctx == "" {
return t.tl(loc, key, args...)
}
if !translator.IsTranslatedC(key, ctx) {
if t.uniqueKeysCtx[ctx] == nil {
t.uniqueKeysCtx[ctx] = make(map[string]uniqueKey)
}
if _, ok := t.uniqueKeysCtx[ctx][key]; !ok {
t.uniqueKeysCtx[ctx][key] = uniqueKey{singular: key}
err := t.addToPotFileIfNotExists(translationKey{ctx, key, false})
if err != nil {
fmt.Println(err)
}
}
return fmt.Sprintf(DefaultNoTranslationCTL, ctx, key)
}
translated := translator.GetC(fmt.Sprintf("%s", key), ctx, args...) //nolint:gosimple
return t.removePrefix(translated)
}
// tn method for handling plurals
func (t *Translator) tn(loc Localizer, singular, plural string, n int, args ...any) string {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return fmt.Sprintf(DefaultNoTranslationTN, singular, plural)
}
if !translator.IsTranslatedN(singular, n) {
// check if translation is in the pot file, if not, add it
if _, ok := t.uniqueKeys[singular]; !ok {
t.uniqueKeys[singular] = uniqueKey{singular: singular, plural: plural}
err := t.addToPotFileIfNotExists(translationKey{"", singular, true})
if err != nil {
fmt.Println(err)
}
}
return fmt.Sprintf(DefaultNoTranslationTN, singular, plural)
}
translated := translator.GetN(singular, plural, n, args...)
return t.removePrefix(translated)
}
func (t *Translator) ctn(loc Localizer, ctx, singular, plural string, n int, args ...any) string {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return fmt.Sprintf(DefaultNoTranslationCTN, ctx, singular, plural)
}
if !translator.IsTranslatedNC(singular, n, ctx) {
if t.uniqueKeysCtx[ctx] == nil {
t.uniqueKeysCtx[ctx] = make(map[string]uniqueKey)
}
if _, ok := t.uniqueKeysCtx[ctx][singular]; !ok {
t.uniqueKeysCtx[ctx][singular] = uniqueKey{singular: singular, plural: plural}
err := t.addToPotFileIfNotExists(translationKey{ctx, singular, true})
if err != nil {
fmt.Println(err)
}
}
return fmt.Sprintf(DefaultNoTranslationCTN, ctx, singular, plural)
}
translated := translator.GetNC(singular, plural, n, ctx, args...)
return t.removePrefix(translated)
}
// Tl translates a string based on the given language tag and key.
func (t *Translator) Tl(loc Localizer, key string, args ...any) string {
return t.tl(loc, key, args...)
}
// Tn method for handling plurals
func (t *Translator) Tn(loc Localizer, singular, plural string, n int) string {
return t.tn(loc, singular, plural, n)
}
// Ctl method for handling string translation with context
func (t *Translator) Ctl(loc Localizer, ctx, key string, args ...any) string {
return t.ctl(loc, ctx, key, args...)
}
// Ctn method for handling plurals with context
func (t *Translator) Ctn(loc Localizer, ctx, singular, plural string, n int) string {
return t.ctn(loc, ctx, singular, plural, n)
}
// Details return the header of the language file
func (t *Translator) Details(loc Localizer) map[string][]string {
return t.languages[loc.GetLocale()].Headers
}