-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocale.go
137 lines (106 loc) · 2.76 KB
/
locale.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
137
package locale
import (
"encoding/json"
"io/fs"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"gopkg.in/yaml.v3"
)
// Singleton instance of i18n.Bundle
var bundle *i18n.Bundle
// A list of localizers for different languages
var localizers = make(map[language.Tag]*i18n.Localizer, 0)
// Fallback language to be used when no suitable
// localization string is not found
var fallbackLanguage language.Tag
var mocking = false
func makeBundle(defaultLanguage language.Tag) *i18n.Bundle {
bundle = i18n.NewBundle(defaultLanguage)
bundle.RegisterUnmarshalFunc("yml", yaml.Unmarshal)
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
return bundle
}
// Load localization files from directory specified by the <directoryPath>
// and prepare them for global usage.
//
// //go:embed locale/*.yml
// var localeFS embed.FS
// err := locale.Initialize(languages.English, localeFS, "locale")
func Initialize(
defaultLanguage language.Tag,
filesystem fs.ReadDirFS,
directoryPath string,
) error {
fallbackLanguage = defaultLanguage
bundle = makeBundle(defaultLanguage)
if directoryPath == "" {
directoryPath = "."
}
// Read all the files from the directory
entries, err := filesystem.ReadDir(directoryPath)
if err != nil {
return err
}
for _, entry := range entries {
// Load each file into the bundle
_, err = bundle.LoadMessageFileFS(
filesystem,
directoryPath+"/"+entry.Name(),
)
if err != nil {
return err
}
// Deduce language from the file name (without extension)
tag := language.Make(
strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name())),
)
localizers[tag] = i18n.NewLocalizer(bundle, tag.String())
}
return nil
}
func InitializeMock() error {
defaultLanguage := language.English
fallbackLanguage = defaultLanguage
bundle = makeBundle(defaultLanguage)
localizers[defaultLanguage] = i18n.NewLocalizer(
bundle,
defaultLanguage.String(),
)
mocking = true
return nil
}
func GetLanguages() []language.Tag {
checkIfInitializaed()
keys := []language.Tag{}
for k := range localizers {
keys = append(keys, k)
}
return keys
}
func GetDefaultLanguage() language.Tag {
checkIfInitializaed()
return fallbackLanguage
}
func GetMessage(id string, tag language.Tag, args []any, count interface{}, fallback string) string {
data := parseArgs(args)
if data["Count"] == nil {
data["Count"] = count
}
if fallback == "" {
fallback = id
}
message, _ := getLocalizer(tag).Localize(&i18n.LocalizeConfig{
TemplateData: data,
PluralCount: count,
DefaultMessage: &i18n.Message{
ID: id,
Other: fallback,
},
})
return message
}