Beispiel #1
0
func Example() {
	i18n.MustLoadTranslationFile("../goi18n/testdata/expected/en-US.all.json")

	T, _ := i18n.Tfunc("en-US")

	fmt.Println(T("program_greeting"))
	fmt.Println(T("person_greeting", map[string]interface{}{
		"Person": "Bob",
	}))

	fmt.Println(T("your_unread_email_count", 0))
	fmt.Println(T("your_unread_email_count", 1))
	fmt.Println(T("your_unread_email_count", 2))
	fmt.Println(T("my_height_in_meters", "1.7"))

	fmt.Println(T("person_unread_email_count", 0, map[string]interface{}{
		"Person": "Bob",
	}))
	fmt.Println(T("person_unread_email_count", 1, map[string]interface{}{
		"Person": "Bob",
	}))
	fmt.Println(T("person_unread_email_count", 2, map[string]interface{}{
		"Person": "Bob",
	}))

	fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": T("d_days", 0),
	}))
	fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": T("d_days", 1),
	}))
	fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": T("d_days", 2),
	}))

	// Output:
	// Hello world
	// Hello Bob
	// You have 0 unread emails.
	// You have 1 unread email.
	// You have 2 unread emails.
	// I am 1.7 meters tall.
	// Bob has 0 unread emails.
	// Bob has 1 unread email.
	// Bob has 2 unread emails.
	// Bob has 3 unread emails in the past 0 days.
	// Bob has 3 unread emails in the past 1 day.
	// Bob has 3 unread emails in the past 2 days.
}
func Example_template() {
	i18n.MustLoadTranslationFile("../goi18n/testdata/expected/en-US.all.json")

	T, _ := i18n.Tfunc("en-US")
	tmpl.Funcs(map[string]interface{}{
		"T": T,
	})

	tmpl.Execute(os.Stdout, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": T("d_days", 1),
	})

	// Output:
	// Hello world
	// Hello Bob
	// You have 0 unread emails.
	// You have 1 unread email.
	// You have 2 unread emails.
	// Bob has 0 unread emails.
	// Bob has 1 unread email.
	// Bob has 2 unread emails.
}
Beispiel #3
0
// LoadFiles looks for i18n folders inside the current directory and the GOPATH
// to find translation files to load
func LoadFiles(userLocale string, defaultLocal string) error {
	gopath := os.Getenv("GOPATH")
	pwd, err := os.Getwd()
	if err != nil {
		tracelog.CompletedError(err, "localize", "LoadFiles")
		return err
	}

	tracelog.Info("localize", "LoadFiles", "PWD[%s] GOPATH[%s]", pwd, gopath)

	// Load any translation files we can find
	searchDirectory(pwd, pwd)
	if gopath != "" {
		searchDirectory(gopath, pwd)
	}

	// Create a translation function for use
	T, err = i18n.Tfunc(userLocale, defaultLocal)
	if err != nil {
		return err
	}

	return nil
}
Beispiel #4
0
// NewTranslation obtains a translation function object for the
// specified locales
func NewTranslation(userLocale string, defaultLocale string) (i18n.TranslateFunc, error) {
	return i18n.Tfunc(userLocale, userLocale)
}