Example #1
0
func init() {
	// bind app domain
	gettext.BindTextdomain("hello", "local", nil)
	gettext.Textdomain("hello")

	// $(LC_MESSAGES) or $(LANG) or empty
	fmt.Println(gettext.Gettext("Gettext in init."))
	fmt.Println(gettext.PGettext("main.init", "Gettext in init."))
	hi.SayHi()
	// Output(depends on local environment):
	// ?
	// ?
	// ?
	// ?

	// set simple chinese
	gettext.SetLocale("zh_CN")

	// simple chinese
	fmt.Println(gettext.Gettext("Gettext in init."))
	fmt.Println(gettext.PGettext("main.init", "Gettext in init."))
	hi.SayHi()
	// Output:
	// Init函数中的Gettext.
	// Init函数中的Gettext.
	// 来自"Hi"包的问候: 你好, 世界!
	// 来自"Hi"包的问候: 你好, 世界!
}
Example #2
0
func main() {
	// simple chinese
	fmt.Println(gettext.Gettext("Hello, world!"))
	fmt.Println(gettext.PGettext("main.main", "Hello, world!"))
	hi.SayHi()
	// Output:
	// 你好, 世界!
	// 你好, 世界!
	// 来自"Hi"包的问候: 你好, 世界!
	// 来自"Hi"包的问候: 你好, 世界!

	// set traditional chinese
	gettext.SetLocale("zh_TW")

	// traditional chinese
	func() {
		fmt.Println(gettext.Gettext("Gettext in func."))
		fmt.Println(gettext.PGettext("main.func", "Gettext in func."))
		hi.SayHi()
		// Output:
		// 閉包函數中的Gettext.
		// 閉包函數中的Gettext.
		// 來自"Hi"包的問候: 你好, 世界!
		// 來自"Hi"包的問候: 你好, 世界!
	}()

	fmt.Println()

	// translate resource
	gettext.SetLocale("zh_CN")
	fmt.Println("poems(simple chinese):")
	fmt.Println(string(gettext.Getdata("poems.txt")))
	gettext.SetLocale("zh_TW")
	fmt.Println("poems(traditional chinese):")
	fmt.Println(string(gettext.Getdata("poems.txt")))
	gettext.SetLocale("??")
	fmt.Println("poems(default is english):")
	fmt.Println(string(gettext.Getdata("poems.txt")))
	// Output: ...
}
Example #3
0
// LoadTranslations loads translation files. getLanguageFn should return a language
// string (e.g. 'en-US'). If getLanguageFn is nil, then the loadSystemLanguage function
// is used, which uses the 'LANG' environment variable.
func LoadTranslations(root string, getLanguageFn func() string) error {
	if getLanguageFn == nil {
		getLanguageFn = loadSystemLanguage
	}

	langStr := findLanguage(root, getLanguageFn)
	translationFiles := []string{
		fmt.Sprintf("%s/%s/LC_MESSAGES/k8s.po", root, langStr),
		fmt.Sprintf("%s/%s/LC_MESSAGES/k8s.mo", root, langStr),
	}

	glog.V(3).Infof("Setting language to %s", langStr)
	// TODO: list the directory and load all files.
	buf := new(bytes.Buffer)
	w := zip.NewWriter(buf)

	// Make sure to check the error on Close.
	for _, file := range translationFiles {
		filename := "translations/" + file
		f, err := w.Create(file)
		if err != nil {
			return err
		}
		data, err := generated.Asset(filename)
		if err != nil {
			return err
		}
		if _, err := f.Write(data); err != nil {
			return nil
		}
	}
	if err := w.Close(); err != nil {
		return err
	}
	gettext.BindTextdomain("k8s", root+".zip", buf.Bytes())
	gettext.Textdomain("k8s")
	gettext.SetLocale(langStr)
	return nil
}
Example #4
0
func setup(locale string, domain string, dir string) {
	gettext.SetLocale(locale)
	gettext.Textdomain(domain)

	gettext.BindTextdomain(domain, dir, nil)
}
Example #5
0
func changeLocale(locale string) {
	gettext.SetLocale(locale)
}