-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
126 lines (101 loc) · 2.97 KB
/
set.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
package translator
import (
"fmt"
"github.com/donseba/gotext"
"os"
"path/filepath"
"time"
)
func (t *Translator) SetTL(loc Localizer, key string, value string) error {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return ErrorLanguageNotFound
}
translator.Set(key, value)
return nil
}
func (t *Translator) SetTLN(loc Localizer, key, plural string, n int, value string) error {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return ErrorLanguageNotFound
}
translator.SetN(key, plural, n, value)
return nil
}
func (t *Translator) SetCTL(loc Localizer, key, ctx, value string) error {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return ErrorLanguageNotFound
}
translator.SetC(key, ctx, value)
return nil
}
func (t *Translator) SetCTN(loc Localizer, key, plural, ctx string, n int, value string) error {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return ErrorLanguageNotFound
}
translator.SetNC(key, plural, ctx, n, value)
return nil
}
func (t *Translator) SetRefs(loc Localizer, key string, refs []string) error {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return ErrorLanguageNotFound
}
translator.SetRefs(key, refs)
return nil
}
func (t *Translator) SetDetails(loc Localizer, key, value string) error {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return ErrorLanguageNotFound
}
translator.GetDomain().Headers.Set(key, value)
return nil
}
func (t *Translator) AddDetails(loc Localizer, key, value string) error {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return ErrorLanguageNotFound
}
translator.GetDomain().Headers.Add(key, value)
return nil
}
func (t *Translator) Write(loc Localizer) error {
translator, exists := t.languages[loc.GetLocale()]
if !exists {
return ErrorLanguageNotFound
}
if translator.GetDomain().Headers.Get("PO-Revision-Date") == "" {
translator.GetDomain().Headers.Set("PO-Revision-Date", time.Now().Format(time.RFC3339))
}
data, err := translator.MarshalText()
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(t.translationsDir, fmt.Sprintf("%s.po", loc.GetLocale())), data, 0644)
if err != nil {
return err
}
return nil
}
func (t *Translator) NewLanguage(loc Localizer, headers ...map[string]string) error {
_, exists := t.languages[loc.GetLocale()]
if exists {
return ErrorLanguageAlreadyExists
}
if t.languages == nil {
t.languages = make(map[string]*gotext.Po)
}
t.languages[loc.GetLocale()] = gotext.NewPo()
t.languages[loc.GetLocale()].GetDomain().Headers.Set("Language", loc.GetLocale())
t.languages[loc.GetLocale()].GetDomain().Headers.Set("Content-Type", "text/plain; charset=UTF-8")
t.languages[loc.GetLocale()].GetDomain().Headers.Set("X-Generator:", "github.com/donseba/go-translator")
for _, header := range headers {
for key, value := range header {
t.languages[loc.GetLocale()].GetDomain().Headers.Set(key, value)
}
}
return nil
}