From cc5c8b2f23854c7c787d553242818d4aaea0da71 Mon Sep 17 00:00:00 2001 From: nytpu Date: Tue, 11 May 2021 11:16:26 -0600 Subject: add getLang() to parse language --- comitium.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'comitium.go') diff --git a/comitium.go b/comitium.go index 41cd616..681b6fb 100644 --- a/comitium.go +++ b/comitium.go @@ -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 [FLAGS] [ARGUMENTS]\n\n", os.Args[0]) -- cgit v1.2.3