當前位置: 首頁>>代碼示例>>Golang>>正文


Golang i18n.IsExist函數代碼示例

本文整理匯總了Golang中github.com/beego/i18n.IsExist函數的典型用法代碼示例。如果您正苦於以下問題:Golang IsExist函數的具體用法?Golang IsExist怎麽用?Golang IsExist使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了IsExist函數的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: setLangVer

// setLangVer sets site language version.
func (this *baseController) setLangVer() bool {
	isNeedRedir := false
	hasCookie := false

	// 1. Check URL arguments.
	lang := this.Input().Get("lang")

	// 2. Get language information from cookies.
	if len(lang) == 0 {
		lang = this.Ctx.GetCookie("lang")
		hasCookie = true
	} else {
		isNeedRedir = true
	}

	// Check again in case someone modify by purpose.
	if !i18n.IsExist(lang) {
		lang = ""
		isNeedRedir = false
		hasCookie = false
	}

	// 3. Get language information from 'Accept-Language'.
	if len(lang) == 0 {
		al := this.Ctx.Request.Header.Get("Accept-Language")
		if len(al) > 4 {
			al = al[:5] // Only compare first 5 letters.
			if i18n.IsExist(al) {
				lang = al
			}
		}
	}

	// 4. Default language is (Chinese) English.
	if len(lang) == 0 {
		lang = "zh-CN" //"en-US"
		isNeedRedir = false
	}

	curLang := langType{
		Lang: lang,
	}

	// Save language information in cookies.
	if !hasCookie {
		this.Ctx.SetCookie("lang", curLang.Lang, 1<<31-1, "/")
	}

	restLangs := make([]*langType, 0, len(langTypes)-1)
	for _, v := range langTypes {
		restLangs = append(restLangs, v)
	}

	// Set language properties.
	this.Lang = lang
	this.Data["Lang"] = curLang.Lang
	this.Data["CurLang"] = curLang.Name
	this.Data["RestLangs"] = restLangs
	return isNeedRedir
}
開發者ID:yh453926638,項目名稱:website,代碼行數:61,代碼來源:controller.go

示例2: setLang

// setLang sets site language version.
func (this *baseRouter) setLang() bool {
	isNeedRedir := false
	hasCookie := false

	// get all lang names from i18n
	langs := utils.Langs

	// 1. Check URL arguments.
	lang := this.GetString("lang")

	// 2. Get language information from cookies.
	if len(lang) == 0 {
		lang = this.Ctx.GetCookie("lang")
		hasCookie = true
	} else {
		isNeedRedir = true
	}

	// Check again in case someone modify by purpose.
	if !i18n.IsExist(lang) {
		lang = ""
		isNeedRedir = false
		hasCookie = false
	}

	// 3. check if isLogin then use user setting
	if len(lang) == 0 && this.isLogin {
		lang = i18n.GetLangByIndex(this.user.Lang)
	}

	// 4. Get language information from 'Accept-Language'.
	if len(lang) == 0 {
		al := this.Ctx.Input.Header("Accept-Language")
		if len(al) > 4 {
			al = al[:5] // Only compare first 5 letters.
			if i18n.IsExist(al) {
				lang = al
			}
		}
	}

	// 4. DefaucurLang language is English.
	if len(lang) == 0 {
		lang = "en-US"
		isNeedRedir = false
	}

	// Save language information in cookies.
	if !hasCookie {
		this.setLangCookie(lang)
	}

	// Set language properties.
	this.Data["Lang"] = lang
	this.Data["Langs"] = langs

	this.Lang = lang

	return isNeedRedir
}
開發者ID:thuongdinh,項目名稱:beegowebapp,代碼行數:61,代碼來源:router.go

示例3: setLang

// 設置語言
func (this *BaseController) setLang() bool {
	isNeedRedir := false
	hasCookie := false

	langs := common.Langs

	lang := this.Input().Get("lang")

	if len(lang) == 0 {
		lang = this.Ctx.GetCookie("lang")
		hasCookie = true
	} else {
		isNeedRedir = true
	}

	if !i18n.IsExist(lang) {
		lang = ""
		isNeedRedir = false
		hasCookie = false
	}

	if len(lang) == 0 {
		al := this.Ctx.Request.Header.Get("Accept-Language")

		if len(al) > 4 {
			al := al[:5]

			if i18n.IsExist(al) {
				lang = al
			}
		}
	}

	if len(lang) == 0 {
		lang = "en-US"
		isNeedRedir = false
	}

	if !hasCookie {
		this.Ctx.SetCookie("lang", lang, 60*60*24*365, "/", nil, nil, false)
	}

	this.Data["Lang"] = lang
	this.Data["Langs"] = langs
	this.Data["CurrentLang"] = lang
	this.Lang = lang

	if lang == "zh-CN" {
		this.Data["CN"] = true
	}

	return isNeedRedir
}
開發者ID:wangroot,項目名稱:SCDht,代碼行數:54,代碼來源:base.go

示例4: Prepare

func (this *BaseController) Prepare() {
	//Accept-Language
	acceptLanguage := this.Ctx.Request.Header.Get("Accept-Language")
	if len(acceptLanguage) > 4 {
		acceptLanguage = acceptLanguage[:5] // Only compare first 5 letters.
		if i18n.IsExist(acceptLanguage) {
			Lang = acceptLanguage
		}
	}

	if len(Lang) == 0 {
		Lang = "en-US"
	}

	// Initialized language type list.
	langs := strings.Split(beego.AppConfig.String("lang::types"), "|")
	names := strings.Split(beego.AppConfig.String("lang::names"), "|")
	langTypes := make([]*langType, 0, len(langs))
	for i, v := range langs {
		langTypes = append(langTypes, &langType{
			Lang: v,
			Name: names[i],
		})
	}

	for _, lang := range langs {
		beego.Trace("Loading language: " + lang)
		if err := i18n.SetMessage(lang, "lang/"+"locale_"+lang+".ini"); err != nil {
			beego.Error("Fail to set message file: " + err.Error())
			return
		}
	}
}
開發者ID:JREAMLU,項目名稱:jkernel_old,代碼行數:33,代碼來源:base.go

示例5: setLangVer

/**
 * 設定當前語言
 */
func (this *BaseController) setLangVer() bool {

	hasCookie := false

	// 參數傳入語言類型
	lang := this.Input().Get("lang")

	// 叢cokkie獲取語言類型
	if len(lang) == 0 {
		lang = this.Ctx.GetCookie("lang")
		hasCookie = true
	}

	// 如果語言不在設定內,重置語言類型
	if !i18n.IsExist(lang) {
		lang = ""
		hasCookie = false
	}

	// 從 'Accept-Language' 響應頭部獲取已設定的語言類型
	if len(lang) == 0 {
		al := this.Ctx.Request.Header.Get("Accept-Language")
		if len(al) > 4 {
			al = al[:5] // Only compare first 5 letters.
			if i18n.IsExist(al) {
				lang = al
			}
		}
	}

	// 無法獲取語言類型,取配置文件默認
	if len(lang) == 0 {
		lang = beego.AppConfig.String("lang")
	}

	// 設置cookie
	if !hasCookie {
		this.Ctx.SetCookie("lang", lang, 1<<31-1, "/")
	}

	this.Data["Lang"] = lang
	return true
}
開發者ID:lvzhongyi,項目名稱:canku,代碼行數:46,代碼來源:base.go

示例6: DocsStatic

func DocsStatic(ctx *context.Context) {
	uri := ctx.Input.Params[":all"]
	if len(uri) > 0 {
		lang := ctx.GetCookie("lang")
		if !i18n.IsExist(lang) {
			lang = "en-US"
		}
		http.ServeFile(ctx.ResponseWriter, ctx.Request, "docs/"+lang+"/"+"images/"+uri)
	}
}
開發者ID:hiproz,項目名稱:beeweb,代碼行數:10,代碼來源:docs.go

示例7: setLang

func (this *BaseController) setLang() {

	// get all lang names from i18n
	langs := models.Langs

	// Get language information from cookies.
	lang := this.Ctx.GetCookie("lang")

	// Check again in case someone modify by purpose.
	if !i18n.IsExist(lang) {
		lang = ""
	}

	// check if isLogin then use user setting
	if len(lang) == 0 && this.IsLogin {
		lang = i18n.GetLangByIndex(this.User.Lang)
	}

	// Get language information from 'Accept-Language'.
	if len(lang) == 0 {
		al := this.Ctx.Input.Header("Accept-Language")
		if len(al) > 4 {
			al = al[:5] // Only compare first 5 letters.
			if i18n.IsExist(al) {
				lang = al
			}
		}
	}

	// Default language is English.
	if len(lang) == 0 {
		lang = "en-US"
	}

	// Save language information in cookies.
	// this.Ctx.SetCookie("lang", lang, 60*60*24*365, "/", nil, nil, false)

	// Set language properties.
	this.Locale.Lang = lang
	this.Data["Lang"] = lang
	this.Data["Langs"] = langs
}
開發者ID:codeshredder,項目名稱:infoboard,代碼行數:42,代碼來源:base.go

示例8: settingLocales

/**
 * 設置語言包
 */
func (this *BaseController) settingLocales() {
	// load locales with locale_LANG.ini files
	this.langTypes = []langType{
		langType{Lang: "en-US", Name: "English"},
		langType{Lang: "zh-CN", Name: "中文"},
	}

	for _, langT := range this.langTypes {
		lang := langT.Lang

		if i18n.IsExist(lang) {
			continue
		}
		if err := i18n.SetMessage(lang, "conf/i18n/"+lang+".ini"); err != nil {
			beego.Error("Fail to set message file: " + err.Error())
			return
		}
	}
}
開發者ID:lvzhongyi,項目名稱:canku,代碼行數:22,代碼來源:base.go

示例9: Prepare

func (this *BaseController) Prepare() {
	lang := this.Input().Get("lang")
	if len(lang) == 0 {
		lang = this.Ctx.GetCookie("lang")
	}
	if len(lang) == 0 {
		al := this.Ctx.Request.Header.Get("Accept-Language")
		if len(al) > 4 {
			al = al[:5]
			if i18n.IsExist(al) {
				lang = al
			}
		}
	}
	if len(lang) == 0 {
		lang = "zh-CN"
		fmt.Println("zh-CN")
	}
	this.Data["Lang"] = lang
}
開發者ID:alex8023,項目名稱:citycloud.cf-deploy-ui,代碼行數:20,代碼來源:base.go

示例10: DocsStatic

func DocsStatic(ctx *context.Context) {
	if uri := ctx.Input.Param(":all"); len(uri) > 0 {
		lang := ctx.GetCookie("lang")
		if !i18n.IsExist(lang) {
			lang = "en-US"
		}

		f, err := os.Open("docs/" + lang + "/" + "images/" + uri)
		if err != nil {
			ctx.WriteString(err.Error())
			return
		}
		defer f.Close()

		_, err = io.Copy(ctx.ResponseWriter, f)
		if err != nil {
			ctx.WriteString(err.Error())
			return
		}
	}
}
開發者ID:flying99999,項目名稱:beeweb,代碼行數:21,代碼來源:docs.go

示例11: Prepare

// Prepare implemented Prepare() method for baseController.
// It's used for language option check and setting.
func (this *baseController) Prepare() {
	// Reset language option.
	this.Lang = "" // This field is from i18n.Locale.

	// 1. Get language information from 'Accept-Language'.
	al := this.Ctx.Request.Header.Get("Accept-Language")
	if len(al) > 4 {
		al = al[:5] // Only compare first 5 letters.
		if i18n.IsExist(al) {
			this.Lang = al
		}
	}

	// 2. Default language is English.
	if len(this.Lang) == 0 {
		this.Lang = "en-US"
	}

	// Set template level language option.
	this.Data["Lang"] = this.Lang
}
開發者ID:RaymondWilkinson,項目名稱:samples,代碼行數:23,代碼來源:app.go

示例12: Prepare

// Prepare implemented Prepare() method for baseController.
// It's used for language option check and setting.
func (this *baseController) Prepare() {
	// Reset language option.
	this.Lang = "" // This field is from i18n.Locale.

	// 1. Get language information from 'Accept-Language'.
	al := this.Ctx.Request.Header.Get("Accept-Language")
	if len(al) > 4 {
		al = al[:5] // Only compare first 5 letters.
		allAl := strings.Split(al, ",")
		fmt.Println("browser lang: ", allAl)
		for _, v := range allAl {
			//Compared the first one!
			if v == "en" {
				v = "en-US"
			} else if v == "zh" {
				v = "zh-CN"
			}
			//Choose the first one.
			if i18n.IsExist(v) {
				this.Lang = v
				fmt.Println("browser : ", this.Lang)
				break
			}
		}

	}

	// 2. Default language is Chinese.
	if len(this.Lang) == 0 {
		this.Lang = "zh-CN"
	}

	// Set template level language option.
	this.Data["Lang"] = this.Lang
	fmt.Println("Choose the first lang: ", this.Data["Lang"])
}
開發者ID:elitecodegroovy,項目名稱:GolangBestPractice,代碼行數:38,代碼來源:app.go

示例13: setLangVer

// setLangVer sets site language version.
func (this *baseRouter) setLangVer() bool {

	// Initialized language type list.
	langs := strings.Split(beego.AppConfig.String("lang::types"), "|")
	names := strings.Split(beego.AppConfig.String("lang::names"), "|")
	langTypes := make([]*langType, 0, len(langs))
	for i, v := range langs {
		langTypes = append(langTypes, &langType{
			Lang: v,
			Name: names[i],
		})
	}

	isNeedRedir := false
	hasCookie := false

	// 1. Check URL arguments.
	lang := this.Input().Get("lang")

	// 2. Get language information from cookies.
	if len(lang) == 0 {
		lang = this.Ctx.GetCookie("lang")
		hasCookie = true
	} else {
		isNeedRedir = true
	}

	// Check again in case someone modify by purpose.
	if !i18n.IsExist(lang) {
		lang = ""
		isNeedRedir = false
		hasCookie = false
	}

	// 3. Get language information from 'Accept-Language'.
	if len(lang) == 0 {
		al := this.Ctx.Request.Header.Get("Accept-Language")
		if len(al) > 4 {
			al = al[:5] // Only compare first 5 letters.
			if i18n.IsExist(al) {
				lang = al
			}
		}
	}

	// 4. Default language is English.
	if len(lang) == 0 {
		lang = "en-US"
		isNeedRedir = false
	}

	curLang := langType{
		Lang: lang,
	}

	// Save language information in cookies.
	if !hasCookie {
		this.Ctx.SetCookie("lang", curLang.Lang, 1<<31-1, "/")
	}

	restLangs := make([]*langType, 0, len(langTypes)-1)
	for _, v := range langTypes {
		if lang != v.Lang {
			restLangs = append(restLangs, v)
		} else {
			curLang.Name = v.Name
		}
	}

	// Set language properties.
	this.Lang = lang
	this.Data["Lang"] = curLang.Lang
	this.Data["CurLang"] = curLang.Name
	this.Data["RestLangs"] = restLangs

	return isNeedRedir
}
開發者ID:shenyp09,項目名稱:juna,代碼行數:78,代碼來源:base.go

示例14: Prepare

// Prepare extracts the language information from request and populate data for rendering templates.
func (b *BaseController) Prepare() {

	var lang string

	langCookie, err := b.Ctx.Request.Cookie("language")
	if err != nil {
		log.Errorf("Error occurred in Request.Cookie: %v", err)
	}
	if langCookie != nil {
		lang = langCookie.Value
	}
	if len(lang) == 0 {
		sessionLang := b.GetSession("lang")
		if sessionLang != nil {
			b.SetSession("Lang", lang)
			lang = sessionLang.(string)
		} else {
			al := b.Ctx.Request.Header.Get("Accept-Language")
			if len(al) > 4 {
				al = al[:5] // Only compare first 5 letters.
				if i18n.IsExist(al) {
					lang = al
				}
			}
		}
	}

	if _, exist := supportLanguages[lang]; !exist { //Check if support the request language.
		lang = defaultLang //Set default language if not supported.
	}

	b.Ctx.SetCookie("language", lang, 0, "/")
	b.SetSession("Lang", lang)

	curLang := langType{
		Lang: lang,
	}

	restLangs := make([]*langType, 0, len(langTypes)-1)
	for _, v := range langTypes {
		if lang != v.Lang {
			restLangs = append(restLangs, v)
		} else {
			curLang.Name = v.Name
		}
	}

	// Set language properties.
	b.Lang = lang
	b.Data["Lang"] = curLang.Lang
	b.Data["CurLang"] = curLang.Name
	b.Data["RestLangs"] = restLangs

	authMode := strings.ToLower(os.Getenv("AUTH_MODE"))
	if authMode == "" {
		authMode = "db_auth"
	}
	b.AuthMode = authMode
	b.Data["AuthMode"] = b.AuthMode

	useCompressedJS := os.Getenv("USE_COMPRESSED_JS")
	if useCompressedJS == "on" {
		b.UseCompressedJS = true
	}

	if _, err := os.Stat(filepath.Join("static", "resources", "js", "harbor.app.min.js")); os.IsNotExist(err) {
		b.UseCompressedJS = false
	}

	selfRegistration := strings.ToLower(os.Getenv("SELF_REGISTRATION"))
	if selfRegistration == "on" {
		b.SelfRegistration = true
	}
	b.Data["SelfRegistration"] = b.SelfRegistration
}
開發者ID:vmware,項目名稱:harbor,代碼行數:76,代碼來源:base.go


注:本文中的github.com/beego/i18n.IsExist函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。