本文整理匯總了Golang中github.com/nicksnyder/go-i18n/i18n.Tfunc函數的典型用法代碼示例。如果您正苦於以下問題:Golang Tfunc函數的具體用法?Golang Tfunc怎麽用?Golang Tfunc使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Tfunc函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Init
func Init(config core_config.ReadWriter) go_i18n.TranslateFunc {
var T go_i18n.TranslateFunc
var err error
locale := config.Locale()
if locale != "" {
pieces := strings.Split(locale, "_")
err = loadFromAsset(locale, pieces[1])
if err == nil {
T, err = go_i18n.Tfunc(config.Locale(), DEFAULT_LOCALE)
}
} else {
var userLocale string
userLocale, err = initWithUserLocale()
if err != nil {
userLocale = mustLoadDefaultLocale()
}
T, err = go_i18n.Tfunc(userLocale, DEFAULT_LOCALE)
}
if err != nil {
panic(err)
}
return T
}
示例2: Init
func Init(config core_config.ReadWriter, detector detection.Detector) go_i18n.TranslateFunc {
var T go_i18n.TranslateFunc
var err error
locale := config.Locale()
if locale != "" {
err = loadFromAsset(locale)
if err == nil {
T, err = go_i18n.Tfunc(config.Locale(), DEFAULT_LOCALE)
}
} else {
var userLocale string
userLocale, err = initWithUserLocale(detector)
if err != nil {
userLocale = mustLoadDefaultLocale()
}
T, err = go_i18n.Tfunc(userLocale, DEFAULT_LOCALE)
}
if err != nil {
panic(err)
}
return T
}
示例3: LoadLocale
func LoadLocale(locale string) i18n.TranslateFunc {
if locale != "" {
T, _ = i18n.Tfunc(locale)
} else {
T, _ = i18n.Tfunc("en-US")
}
return T
}
示例4: GetTranslationsAndLocale
func GetTranslationsAndLocale(w http.ResponseWriter, r *http.Request) (i18n.TranslateFunc, string) {
headerLocale := strings.Split(strings.Split(r.Header.Get("Accept-Language"), ",")[0], "-")[0]
if locales[headerLocale] != "" {
translations, _ := i18n.Tfunc(headerLocale)
return translations, headerLocale
}
translations, _ := i18n.Tfunc(model.DEFAULT_LOCALE)
return translations, model.DEFAULT_LOCALE
}
示例5: TfuncWithFallback
func TfuncWithFallback(pref string) i18n.TranslateFunc {
t, _ := i18n.Tfunc(pref)
return func(translationID string, args ...interface{}) string {
if translated := t(translationID, args...); translated != translationID {
return translated
}
t, _ := i18n.Tfunc(model.DEFAULT_LOCALE)
return t(translationID, args...)
}
}
示例6: lookup
/**
* Request that the LCS start a lookup process for a particular URL.
* @param {string} lookupURL - The URL to try to find in the distributed cache
*/
func lookup(lookupURL string) Result {
response, err := http.Get(BundleLookupURL(Configuration, lookupURL))
T, _ := i18n.Tfunc(os.Getenv("CENOLANG"), "en-us")
if err != nil {
fmt.Println(T("error_cli", map[string]interface{}{
"Message": err.Error(),
}))
return Result{ERR_NO_CONNECT_LCS, err.Error(), false, false, ""}
} else if response == nil || response.StatusCode != 200 {
errMsg := T("lcs_not_ready_cli")
fmt.Println(errMsg)
return Result{ERR_LCS_NOT_READY, errMsg, false, false, ""}
}
decoder := json.NewDecoder(response.Body)
var result Result
err = decoder.Decode(&result)
if err != nil {
decodeErrorMessage := T("decode_error_cli", map[string]interface{}{
"Message": err.Error(),
})
fmt.Println(decodeErrorMessage)
reachedLCS := HandleCCError(ERR_MALFORMED_LCS_RESPONSE, err.Error(), ErrorState{
"requestURL": DecodeErrReportURL(Configuration),
})
if reachedLCS {
return Result{ERR_MALFORMED_LCS_RESPONSE, decodeErrorMessage, false, false, ""}
} else {
errMsg := T("no_reach_lcs_cli")
return Result{ERR_NO_CONNECT_LCS, errMsg, false, false, ""}
}
}
return result
}
示例7: init
func init() {
cmd, err := notify.NewCmd("telegram-cli", "-C")
if err != nil {
log.Fatal("NewCmd error", err)
}
botConf = Conf{
Limit: 10,
Url: "localhost",
Rpms: 500,
Cmd: cmd,
}
//Init Data Mapper
c, err := beeconf.NewConfig("ini", "../../conf/app.conf")
if err != nil {
log.Fatal(err)
}
TestConfig, err := conf.Initialize("test", c)
if err != nil {
log.Fatal(err)
}
if err := M.DbOpen(TestConfig.Db); err != nil {
log.Fatal(err)
}
M.PrepareTables(&M.Cand{})
Tfn, _ := i18n.Tfunc("en-us", "en-us", "en-us")
M.T = Tfn
}
示例8: main
func main() {
// Configure the i18n library to use the preferred language set in the CENOLANG environement variable
setLanguage := os.Getenv("CENOLANG")
if setLanguage == "" {
os.Setenv("CENOLANG", "en-us")
setLanguage = "en-us"
}
i18n.MustLoadTranslationFile("./translations/" + setLanguage + ".all.json")
T, _ := i18n.Tfunc(setLanguage, "en-us")
// Read an existing configuration file or have the user supply settings
if conf, err := ReadConfigFile(CONFIG_FILE); err != nil {
fmt.Println(T("no_config_cli", map[string]interface{}{"Location": CONFIG_FILE}))
Configuration = GetConfigFromUser()
} else {
Configuration = conf
}
// Create an HTTP proxy server
http.HandleFunc("/lookup", directHandler)
http.HandleFunc("/", proxyHandler)
fmt.Println(T("listening_msg_cli", map[string]interface{}{"Port": Configuration.PortNumber}))
err := http.ListenAndServe(Configuration.PortNumber, nil)
if err != nil {
panic(err)
}
}
示例9: SendEmailPasswordResetToken
// SendEmailPasswordResetToken sends a password reset token via email.
func SendEmailPasswordResetToken(to string, token string, locale string) error {
T, _ := i18n.Tfunc(locale)
err := email.SendEmailFromAdmin(to,
T("reset_password_title"),
T("link")+" : "+config.HostURL+"/reset/password/"+token,
T("reset_password_content")+"<br/><a href=\""+config.HostURL+"/reset/password/"+token+"\" target=\"_blank\">"+config.HostURL+"/reset/password/"+token+"</a>")
return err
}
示例10: SendEmailVerfication
// SendEmailVerfication sends an email verification token via email.
func SendEmailVerfication(to string, token string, locale string) error {
T, _ := i18n.Tfunc(locale)
err := email.SendEmailFromAdmin(to,
T("verify_email_title"),
T("link")+" : "+config.HostURL+"/verify/email/"+token,
T("verify_email_content")+"<br/><a href=\""+config.HostURL+"/verify/email/"+token+"\" target=\"_blank\">"+config.HostURL+"/verify/email/"+token+"</a>")
return err
}
示例11: GetUserTranslations
func GetUserTranslations(locale string) i18n.TranslateFunc {
if _, ok := locales[locale]; !ok {
locale = model.DEFAULT_LOCALE
}
translations, _ := i18n.Tfunc(locale)
return translations
}
示例12: NewTranslation
// NewTranslation obtains a translation function object for the
// specified locales
func NewTranslation(userLocale string, defaultLocale string) (t i18n.TranslateFunc, err error) {
t, err = i18n.Tfunc(userLocale, userLocale)
if err != nil {
return t, err
}
return t, err
}
示例13: init
func init() {
// TO DO : find another way maybe with assets, go bind-data
out1, _ := Asset("en-us.all.yaml")
out2, _ := Asset("fr-be.all.yaml")
i18n.ParseTranslationFileBytes("en-us.all.yaml", out1)
i18n.ParseTranslationFileBytes("fr-be.all.yaml", out2)
Tr, _ = i18n.Tfunc("fr-be")
}
示例14: Example
func Example() {
i18n.MustLoadTranslationFile("../goi18n/testdata/expected/en-us.all.json")
T, _ := i18n.Tfunc("en-US")
bobMap := map[string]interface{}{"Person": "Bob"}
bobStruct := struct{ Person string }{Person: "Bob"}
fmt.Println(T("program_greeting"))
fmt.Println(T("person_greeting", bobMap))
fmt.Println(T("person_greeting", bobStruct))
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, bobMap))
fmt.Println(T("person_unread_email_count", 1, bobMap))
fmt.Println(T("person_unread_email_count", 2, bobMap))
fmt.Println(T("person_unread_email_count", 0, bobStruct))
fmt.Println(T("person_unread_email_count", 1, bobStruct))
fmt.Println(T("person_unread_email_count", 2, bobStruct))
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
// 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 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.
}
示例15: validateURL
/**
* Check if a provided URL is well-formed. If not, serve an error page.
* This call terminates requests when the return value is false (i.e. invalid URL).
* @param {string} URL - The URL being requested
* @param {ResponseWriter} w - The object handling writing responses to the client
* @param {*Request} r - Information about the request
*/
func validateURL(URL string, w http.ResponseWriter, r *http.Request) bool {
isValid, err := regexp.MatchString(URL_REGEX, URL)
T, _ := i18n.Tfunc(os.Getenv("CENOLANG"), "en-us")
if !isValid || err != nil {
HandleCCError(ERR_MALFORMED_URL, T("malformed_url_cli", map[string]interface{}{
"URL": URL,
}), ErrorState{"responseWriter": w, "request": r})
return false
}
return true
}