From d9b557f7f9f0db3d26f2651ced30eb3bb0cb1ebd Mon Sep 17 00:00:00 2001 From: Monke <85203534+Brougud@users.noreply.github.com> Date: Wed, 22 Feb 2023 18:50:44 -0500 Subject: [PATCH] v0.2.0 --- language/language.go | 22 ++++++++--- translator.go | 88 ++++++++++++++++++++++++++++++-------------- 2 files changed, 76 insertions(+), 34 deletions(-) diff --git a/language/language.go b/language/language.go index c0f0a2e..b4c76fd 100644 --- a/language/language.go +++ b/language/language.go @@ -37,13 +37,23 @@ func (l Language) WithTranslations(tra string) Language { return l } +// Name ... +func (l Language) Name() string { + return l.name +} + +// Icon ... +func (l Language) Icon() string { + return l.icon +} + // Translate will return the value of the given key unformatted // if the given key is not found then it will simply return the key func (l Language) Translate(key string) string { res := gjson.Get(l.translations, key) - if res.String() == "" { - return key - } + if res.String() == "" { + return key + } return res.String() } @@ -51,8 +61,8 @@ func (l Language) Translate(key string) string { // if the given key is not found then it will simply return the key func (l Language) Translatef(key string, args ...any) string { res := gjson.Get(l.translations, key) - if res.String() == "" { - return key - } + if res.String() == "" { + return key + } return fmt.Sprintf(res.String(), args...) } diff --git a/translator.go b/translator.go index 0fe6e39..f346aba 100644 --- a/translator.go +++ b/translator.go @@ -28,48 +28,80 @@ var languages = make(map[lng.Tag]language.Language) // an error while creating the directory then // it will panic with the error func Initalize(dir string) { - path = dir + 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) - } - } + // 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 +// 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 + 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 + 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 } -// FromString returns a language from a locale string -// if the language could not be found it will return +// FromLocaleString returns a language from a locale string +// if the language could not be found it will return // false as the second value -func FromString(lang string) (language.Language, bool) { - tag := lng.MustParse(lang) - lan, ok := languages[tag] - return lan, ok +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 }