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

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

要实现多语言内容的展示,可以通过以下技术实现:页面内容通过控制器传递动态语言参数,从而实现多语言切换。这种方式的核心思想是在展示页面时,通过控制器传播语言参数,决定最终展示的内容。

主要实现方式是在前端页面中使用占位符,通过后端控制器传递语言参数,例如获取lang参数,如果未传递则默认为英文。接下来通过调用配置文件生成相应语言的内容实例,最终将内容通过gin框架渲染到页面上。

具体实现如下:

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,        "Send":      language.Send,        "Lang":      lang,    })}

该实现的关键点在于语言配置文件的处理,配置文件应定义不同的语言内容,通过传递不同的语言代码(如encn)来获取相应的语言内容。比如:

type Language struct {    WebCopyRight string    MainIntro    string    Send        string    Notice      string    IndexSubIntro string    IndexVisitors string    IndexAgent   string    IndexDocument string    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?",        }    } else if lang == "cn" {        language = &Language{            WebCopyRight: "陶士涵的菜地版权所有",            MainIntro:   "极简强大的Go语言在线客服系统",            IndexSubIntro: "GO-FLY,一套为PHP工程师、Golang工程师准备的基于 Vue 2.0的在线客服即时通讯系统",            Index_visitors: "访客入口",            IndexAgent:   "客服入口",            IndexDocument: "接口文档",            IndexOnlineChat: "在线咨询",            Send:        "发送",            Notice:       "欢迎您访问go-fly!有什么我能帮助您的?",        }    }    return language}

图片部分可替换为适当的描述性文字内容:

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

你可能感兴趣的文章
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>