Created
October 7, 2016 14:32
-
-
Save fujiwara/7c74bc68ce4eee13cccbd4c1cc6a3059 to your computer and use it in GitHub Desktop.
go-gettext i18n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
"github.com/samuel/go-gettext/gettext" | |
) | |
var domain *gettext.Domain | |
func main() { | |
var err error | |
domain, err = gettext.NewDomain("im", "./lang") | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(loc("logined as %1", os.Getenv("LOGNAME"))) | |
} | |
func loc(msgid string, args ...string) string { | |
tr := domain.GetText(os.Getenv("LANG"), msgid) | |
for i, v := range args { | |
p := "%" + strconv.Itoa(i+1) | |
tr = strings.Replace(tr, p, v, 1) | |
} | |
return tr | |
} |
Author
fujiwara
commented
Oct 7, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment