Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Brougud committed Feb 22, 2023
1 parent 9301752 commit d9b557f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 34 deletions.
22 changes: 16 additions & 6 deletions language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,32 @@ 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()
}

// Translatef will return a formatted version of the 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...)
}
88 changes: 60 additions & 28 deletions translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit d9b557f

Please sign in to comment.