[Go] GO语言实战-gin框架项目实现中英文切换

若是项目中须要有多语言的展现,相似网站中英文切换,能够使用下面这个方法来实现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
}
相关文章
相关标签/搜索