add getLang() to parse language

This commit is contained in:
nytpu
2021-05-11 11:16:26 -06:00
parent 244746dff7
commit cc5c8b2f23
3 changed files with 24 additions and 7 deletions
+21
View File
@@ -13,11 +13,17 @@ import (
"path/filepath"
"github.com/mitchellh/go-homedir"
"golang.nytpu.com/comitium/localizations"
"golang.org/x/text/language"
"golang.nytpu.com/comitium/core"
"golang.nytpu.com/comitium/fetch"
)
var supportedLangs = []language.Tag{
language.AmericanEnglish, // en-US (fallback)
}
func main() {
argCheck(os.Args, 2, true, "Please provide a command.")
@@ -41,6 +47,9 @@ func main() {
c.AddFlagSet(globalFs)
c.Parse(os.Args[2:])
// figure out what language to use
localizer := getLang()
// dataPath is the location where all the data (list of subs, cached hashes,
// etcetera) are stored
dataPath := getDataPath()
@@ -152,6 +161,18 @@ func getDataPath() string {
return verifyPath("comitium")
}
// getLang will return the localizer for the user's lang
func getLang() *localizations.Localizer {
matcher := language.NewMatcher(supportedLangs)
var lang language.Tag
if *langFlag != "" {
lang, _ = language.MatchStrings(matcher, *langFlag)
} else if env := os.Getenv("LANG"); env != "" {
lang, _ = language.MatchStrings(matcher, env)
}
return localizations.New(lang.String(), supportedLangs[0].String())
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, " %v <command> [FLAGS] [ARGUMENTS]\n\n", os.Args[0])