github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/flag/locale.go (about) 1 package flag 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 flags "github.com/jessevdk/go-flags" 9 "github.com/liamawhite/cli-with-i18n/cf/i18n" 10 ) 11 12 type Locale struct { 13 Locale string 14 } 15 16 func (l Locale) Complete(prefix string) []flags.Completion { 17 return completions(l.listLocales(), l.sanitize(prefix), false) 18 } 19 20 func (l *Locale) UnmarshalFlag(val string) error { 21 sanitized := strings.ToLower(l.sanitize(val)) 22 for _, locale := range l.listLocales() { 23 if sanitized == strings.ToLower(locale) { 24 l.Locale = locale 25 return nil 26 } 27 } 28 29 return &flags.Error{ 30 Type: flags.ErrRequired, 31 Message: fmt.Sprintf("LOCALE must be %s", strings.Join(l.listLocales(), ", ")), 32 } 33 } 34 35 func (Locale) sanitize(val string) string { 36 return strings.Replace(val, "_", "-", -1) 37 } 38 39 func (Locale) listLocales() []string { 40 locals := append(i18n.SupportedLocales(), "CLEAR") 41 sort.Strings(locals) 42 return locals 43 }