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