Skip to content

Commit a24a584

Browse files
authored
Merge pull request #3 from donseba/set-write-update
getter and setter . now we can build the editor around it :)
2 parents 75359d1 + b51e4cc commit a24a584

File tree

7 files changed

+399
-129
lines changed

7 files changed

+399
-129
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919

2020
# Go workspace file
2121
go.work
22+
/test_translations/en_TL.po
23+
/test_translations/en_TN.po

get.go

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package translator
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Tl translates a string based on the given language tag and key.
8+
func (t *Translator) tl(loc Localizer, key string, args ...interface{}) string {
9+
translator, exists := t.languages[loc.GetLocale()]
10+
if !exists {
11+
return fmt.Sprintf(DefaultNoTranslationTL, key)
12+
}
13+
14+
if !translator.IsTranslated(key) {
15+
// check if translation is in the pot file, if not, add it
16+
if _, ok := t.uniqueKeys[key]; !ok {
17+
t.uniqueKeys[key] = uniqueKey{singular: key}
18+
19+
err := t.addToPotFileIfNotExists(translationKey{"", key, false})
20+
if err != nil {
21+
fmt.Println(err)
22+
}
23+
}
24+
return fmt.Sprintf(DefaultNoTranslationTL, key)
25+
}
26+
27+
translated := translator.Get(key, args...)
28+
return t.removePrefix(translated)
29+
}
30+
31+
func (t *Translator) ctl(loc Localizer, ctx, key string, args ...interface{}) string {
32+
translator, exists := t.languages[loc.GetLocale()]
33+
if !exists {
34+
return fmt.Sprintf(DefaultNoTranslationCTL, ctx, key)
35+
}
36+
37+
if !translator.IsTranslatedC(key, ctx) {
38+
if t.uniqueKeysCtx[ctx] == nil {
39+
t.uniqueKeysCtx[ctx] = make(map[string]uniqueKey)
40+
}
41+
42+
if _, ok := t.uniqueKeysCtx[ctx][key]; !ok {
43+
t.uniqueKeysCtx[ctx][key] = uniqueKey{singular: key}
44+
45+
err := t.addToPotFileIfNotExists(translationKey{ctx, key, false})
46+
if err != nil {
47+
fmt.Println(err)
48+
}
49+
}
50+
51+
return fmt.Sprintf(DefaultNoTranslationCTL, ctx, key)
52+
}
53+
54+
translated := translator.GetC(key, ctx, args...)
55+
return t.removePrefix(translated)
56+
}
57+
58+
// tn method for handling plurals
59+
func (t *Translator) tn(loc Localizer, singular, plural string, n int, args ...any) string {
60+
translator, exists := t.languages[loc.GetLocale()]
61+
if !exists {
62+
return fmt.Sprintf(DefaultNoTranslationTN, singular, plural)
63+
}
64+
65+
if !translator.IsTranslatedN(singular, n) {
66+
// check if translation is in the pot file, if not, add it
67+
if _, ok := t.uniqueKeys[singular]; !ok {
68+
t.uniqueKeys[singular] = uniqueKey{singular: singular, plural: plural}
69+
70+
err := t.addToPotFileIfNotExists(translationKey{"", singular, true})
71+
if err != nil {
72+
fmt.Println(err)
73+
}
74+
}
75+
return fmt.Sprintf(DefaultNoTranslationTN, singular, plural)
76+
}
77+
78+
translated := translator.GetN(singular, plural, n, args...)
79+
return t.removePrefix(translated)
80+
}
81+
82+
func (t *Translator) ctn(loc Localizer, ctx, singular, plural string, n int, args ...any) string {
83+
translator, exists := t.languages[loc.GetLocale()]
84+
if !exists {
85+
return fmt.Sprintf(DefaultNoTranslationCTN, ctx, singular, plural)
86+
}
87+
88+
if !translator.IsTranslatedNC(singular, n, ctx) {
89+
if t.uniqueKeysCtx[ctx] == nil {
90+
t.uniqueKeysCtx[ctx] = make(map[string]uniqueKey)
91+
}
92+
93+
if _, ok := t.uniqueKeysCtx[ctx][singular]; !ok {
94+
t.uniqueKeysCtx[ctx][singular] = uniqueKey{singular: singular, plural: plural}
95+
96+
err := t.addToPotFileIfNotExists(translationKey{ctx, singular, true})
97+
if err != nil {
98+
fmt.Println(err)
99+
}
100+
}
101+
102+
return fmt.Sprintf(DefaultNoTranslationCTN, ctx, singular, plural)
103+
}
104+
105+
translated := translator.GetNC(singular, plural, n, ctx, args...)
106+
return t.removePrefix(translated)
107+
}
108+
109+
// Tl translates a string based on the given language tag and key.
110+
func (t *Translator) Tl(loc Localizer, key string, args ...interface{}) string {
111+
return t.tl(loc, key, args...)
112+
}
113+
114+
// Tn method for handling plurals
115+
func (t *Translator) Tn(loc Localizer, singular, plural string, n int) string {
116+
return t.tn(loc, singular, plural, n)
117+
}
118+
119+
// Ctl method for handling string translation with context
120+
func (t *Translator) Ctl(loc Localizer, ctx, key string, args ...interface{}) string {
121+
return t.ctl(loc, ctx, key, args...)
122+
}
123+
124+
// Ctn method for handling plurals with context
125+
func (t *Translator) Ctn(loc Localizer, ctx, singular, plural string, n int) string {
126+
return t.ctn(loc, ctx, singular, plural, n)
127+
}
128+
129+
// Details return the header of the language file
130+
func (t *Translator) Details(loc Localizer) map[string][]string {
131+
return t.languages[loc.GetLocale()].Headers
132+
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/donseba/go-translator
22

33
go 1.21.6
44

5-
require github.com/donseba/gotext v1.5.6
5+
require github.com/donseba/gotext v1.5.7

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/donseba/gotext v1.5.6 h1:CCc+5KnIMTHCadoWzWv5R+dI0WfTWNqSGqYrtLV3ZaE=
2-
github.com/donseba/gotext v1.5.6/go.mod h1:LxKX7opw9kjMdEZqplR6sgDp0qFG6AFEvz4oYpNtCcY=
1+
github.com/donseba/gotext v1.5.7 h1:7aDhXXdB09GObC350IpJ5xADjgY83tGe5iCD+gUIi1U=
2+
github.com/donseba/gotext v1.5.7/go.mod h1:LxKX7opw9kjMdEZqplR6sgDp0qFG6AFEvz4oYpNtCcY=

set.go

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package translator
2+
3+
import (
4+
"fmt"
5+
"github.com/donseba/gotext"
6+
"os"
7+
"path/filepath"
8+
"time"
9+
)
10+
11+
func (t *Translator) SetTL(loc Localizer, key string, value string) error {
12+
translator, exists := t.languages[loc.GetLocale()]
13+
if !exists {
14+
return ErrorLanguageNotFound
15+
}
16+
17+
translator.Set(key, value)
18+
return nil
19+
}
20+
21+
func (t *Translator) SetTLN(loc Localizer, key, plural string, n int, value string) error {
22+
translator, exists := t.languages[loc.GetLocale()]
23+
if !exists {
24+
return ErrorLanguageNotFound
25+
}
26+
27+
translator.SetN(key, plural, n, value)
28+
return nil
29+
}
30+
31+
func (t *Translator) SetCTL(loc Localizer, key, ctx, value string) error {
32+
translator, exists := t.languages[loc.GetLocale()]
33+
if !exists {
34+
return ErrorLanguageNotFound
35+
}
36+
37+
translator.SetC(key, ctx, value)
38+
return nil
39+
}
40+
41+
func (t *Translator) SetCTN(loc Localizer, key, plural, ctx string, n int, value string) error {
42+
translator, exists := t.languages[loc.GetLocale()]
43+
if !exists {
44+
return ErrorLanguageNotFound
45+
}
46+
47+
translator.SetNC(key, plural, ctx, n, value)
48+
return nil
49+
}
50+
51+
func (t *Translator) SetRefs(loc Localizer, key string, refs []string) error {
52+
translator, exists := t.languages[loc.GetLocale()]
53+
if !exists {
54+
return ErrorLanguageNotFound
55+
}
56+
57+
translator.SetRefs(key, refs)
58+
return nil
59+
}
60+
61+
func (t *Translator) SetDetails(loc Localizer, key, value string) error {
62+
translator, exists := t.languages[loc.GetLocale()]
63+
if !exists {
64+
return ErrorLanguageNotFound
65+
}
66+
67+
translator.GetDomain().Headers.Set(key, value)
68+
return nil
69+
}
70+
71+
func (t *Translator) AddDetails(loc Localizer, key, value string) error {
72+
translator, exists := t.languages[loc.GetLocale()]
73+
if !exists {
74+
return ErrorLanguageNotFound
75+
}
76+
77+
translator.GetDomain().Headers.Add(key, value)
78+
return nil
79+
}
80+
81+
func (t *Translator) Write(loc Localizer) error {
82+
translator, exists := t.languages[loc.GetLocale()]
83+
if !exists {
84+
return ErrorLanguageNotFound
85+
}
86+
87+
if translator.GetDomain().Headers.Get("PO-Revision-Date") == "" {
88+
translator.GetDomain().Headers.Set("PO-Revision-Date", time.Now().Format(time.RFC3339))
89+
}
90+
91+
data, err := translator.MarshalText()
92+
if err != nil {
93+
return err
94+
}
95+
96+
err = os.WriteFile(filepath.Join(t.translationsDir, fmt.Sprintf("%s.po", loc.GetLocale())), data, 0644)
97+
if err != nil {
98+
return err
99+
}
100+
101+
return nil
102+
}
103+
104+
func (t *Translator) NewLanguage(loc Localizer, headers ...map[string]string) error {
105+
_, exists := t.languages[loc.GetLocale()]
106+
if exists {
107+
return ErrorLanguageAlreadyExists
108+
}
109+
110+
if t.languages == nil {
111+
t.languages = make(map[string]*gotext.Po)
112+
}
113+
114+
t.languages[loc.GetLocale()] = gotext.NewPo()
115+
t.languages[loc.GetLocale()].GetDomain().Headers.Set("Language", loc.GetLocale())
116+
t.languages[loc.GetLocale()].GetDomain().Headers.Set("Content-Type", "text/plain; charset=UTF-8")
117+
t.languages[loc.GetLocale()].GetDomain().Headers.Set("X-Generator:", "github.com/donseba/go-translator")
118+
119+
for _, header := range headers {
120+
for key, value := range header {
121+
t.languages[loc.GetLocale()].GetDomain().Headers.Set(key, value)
122+
}
123+
}
124+
125+
return nil
126+
}

0 commit comments

Comments
 (0)