-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.go
107 lines (94 loc) · 2.8 KB
/
translator.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
package translator
import (
"fmt"
"os"
"github.com/Brougud/translator/language"
lng "golang.org/x/text/language"
)
// path is where the translation files are located
var path string
// languages is a map containing the locale of the language
// and maps that to a string of the json value
var languages = make(map[lng.Tag]language.Language)
// Initalize will initalize the library, this is where
// you put all of your settings that you want, and
// how you want to use your languages
//
// dir is the directory containing all of the translation
// files
//
// if the directory does not exist then it will
// create the directory for you, if there is
// an error while creating the directory then
// it will panic with the error
func Initalize(dir string) {
path = dir
// the directory could not be found then make
// it for the user
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
panic(err)
}
}
}
// Register takes in the locale of the language,
// this should be the naming convention of your
// files
//
// ex: if your file name is "en_US.json" then
// your locale should be "en_US"
func Register(locale string, lang language.Language) (language.Language, error) {
dat, err := os.ReadFile(fmt.Sprintf("%v/%v.json", path, locale))
if err != nil {
return language.Language{}, err
}
lang = lang.WithTranslations(string(dat))
languages[lng.MustParse(locale)] = lang
return lang, nil
}
// Refresh will refresh all of the translations
// that have been registers, if the file can
// not be found, then it will remove it from
// the list of translations
func Refresh() {
for t, l := range languages {
dat, err := os.ReadFile(fmt.Sprintf("%v/%v.json", path, t.String()))
if err != nil {
delete(languages, t)
}
l = l.WithTranslations(string(dat))
languages[t] = l
}
}
// All will return all of the languages that are registered
func All() []lng.Tag {
final := []lng.Tag{}
for l := range languages {
final = append(final, l)
}
return final
}
// Languages will return a map of the language tags
// along with the language contents
func Languages() map[lng.Tag]language.Language {
return languages
}
// FromLocaleString returns a language from a locale string
// if the language could not be found it will return
// false as the second value
func FromLocaleString(lang string) (language.Language, bool) {
tag := lng.MustParse(lang)
lan, ok := languages[tag]
return lan, ok
}
// FromLanguageName will return a language, based on the name
// that it was given at registration
func FromLanguageName(name string) (language.Language, bool) {
for _, l := range languages {
if l.Name() == name {
return l, true
}
}
return language.Language{}, false
}