博客
关于我
[Go] GO语言实战-gin框架项目实现中英文切换
阅读量:663 次
发布时间:2019-03-15

本文共 2148 字,大约阅读时间需要 7 分钟。

如果项目中需要有多语言的展示,类似网站中英文切换,可以使用下面这个方法来实现

主要思路就是,页面html内容展示的时候,不能固定写死在页面上,需要从控制器部分分配过来变量,展示输出这个变量

这个变量的内容来自一个结构体的成员,该结构体在创建实例的时候,可以根据传递参数的不同,实例的成员内容不同

 

实际展示的地址是:

http://gofly.sopans.com/

 

 

 

控制器部分就是分配变量,在这里是通过get传递lang这个参数cn就是中文,en就是英文

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 configtype 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}

 

  

 

 

转载地址:http://hjimz.baihongyu.com/

你可能感兴趣的文章
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>