| 1 | package tr |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "os" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/leonelquinteros/gotext" |
| 9 | ) |
| 10 | |
| 11 | //go:generate go run ../tr/trgen/trgen.go |
| 12 | |
| 13 | var Tr = gotext.NewLocale("/usr/share/locale", "en") |
| 14 | |
| 15 | var locales = make(map[string]string) |
| 16 | |
| 17 | func findLocale() string { |
| 18 | vars := []string{"LC_ALL", "LC_MESSAGES", "LANG"} |
| 19 | for _, varname := range vars { |
| 20 | if val, ok := os.LookupEnv(varname); ok { |
| 21 | return val |
| 22 | } |
| 23 | } |
| 24 | return "" |
| 25 | } |
| 26 | |
| 27 | func processLocale(locale string) []string { |
| 28 | options := make([]string, 0, 2) |
| 29 | // For example, split "en_DK.UTF-8" into "en_DK" and "UTF-8". |
| 30 | pieces := strings.Split(locale, ".") |
| 31 | options = append(options, pieces[0]) |
| 32 | // For example, split "en_DK" into "en" and "DK". |
| 33 | pieces = strings.Split(pieces[0], "_") |
| 34 | if len(pieces) > 1 { |
| 35 | options = append(options, pieces[0]) |
| 36 | } |
| 37 | return options |
| 38 | } |
| 39 | |
| 40 | func InitializeLocale() { |
| 41 | locale := findLocale() |
| 42 | if len(locale) == 0 { |
| 43 | return |
| 44 | } |
| 45 | Tr = gotext.NewLocale("/usr/share/locale", locale) |
| 46 | Tr.AddDomain("git-lfs") |
| 47 | for _, loc := range processLocale(locale) { |
| 48 | if moData, ok := locales[loc]; ok { |
| 49 | mo := gotext.NewMo() |
| 50 | decodedData, err := base64.StdEncoding.DecodeString(moData) |
| 51 | if err != nil { |
| 52 | continue |
| 53 | } |
| 54 | mo.Parse(decodedData) |
| 55 | Tr.AddTranslator("git-lfs", mo) |
| 56 | return |
| 57 | } |
| 58 | } |
| 59 | } |