若是項目中須要有多語言的展現,相似網站中英文切換,能夠使用下面這個方法來實現html
主要思路就是,頁面html內容展現的時候,不能固定寫死在頁面上,須要從控制器部分分配過來變量,展現輸出這個變量網站
這個變量的內容來自一個結構體的成員,該結構體在建立實例的時候,能夠根據傳遞參數的不一樣,實例的成員內容不一樣spa
實際展現的地址是:3d
http://gofly.sopans.com/code
直達地址htm
控制器部分就是分配變量,在這裏是經過get傳遞lang這個參數cn就是中文,en就是英文blog
engine.GET("/index", tmpl.PageIndex)
//首頁 func PageIndex(c *gin.Context) { lang := c.Query("lang") if lang == "" ||lang!="cn"{ lang = "en" } language:=config.CreateLanguage(lang) c.HTML(http.StatusOK, "index.html", gin.H{ "Copyright":language.WebCopyRight, "WebDesc":language.MainIntro, "SubIntro":language.IndexSubIntro, "Document":language.IndexDocument, "VisitorBtn":language.IndexVisitors, "AgentBtn":language.IndexAgent, "OnlineChat":language.IndexOnlineChat, "IndexSend":language.Send, "Lang":lang, }) }
langguage這個結構體部分,根據不一樣的參數,建立不一樣的實例成員接口
package config type Language struct { WebCopyRight string MainIntro string Send string Notice string IndexSubIntro,IndexVisitors,IndexAgent,IndexDocument,IndexOnlineChat string } func CreateLanguage(lang string)*Language{ var language *Language if lang=="en"{ language=&Language{ WebCopyRight: "TaoShihan", MainIntro: "Simple and Powerful Go language online customer chat system", IndexSubIntro: "GO-FLY, a Vue 2.0-based online customer service instant messaging system for PHP engineers and Golang engineers", IndexDocument:"API Documents", IndexVisitors:"Visitors Here", IndexAgent:"Agents Here", IndexOnlineChat:"Let’s chat. - We're online", Send:"Send", Notice:"Hello and welcome to go-fly - how can we help?", } } if lang=="cn"{ language=&Language{ WebCopyRight: "陶士涵的菜地版權全部", MainIntro:"極簡強大的Go語言在線客服系統", IndexSubIntro:"GO-FLY,一套爲PHP工程師、Golang工程師準備的基於 Vue 2.0的在線客服即時通信系統", IndexVisitors:"訪客入口", IndexAgent:"客服入口", IndexDocument:"接口文檔", IndexOnlineChat:"在線諮詢", Send:"發送", Notice:"歡迎您訪問go-fly!有什麼我能幫助您的?", } } return language }