Go+Wails 标题中文乱码问题

前置条件

wails 能跑起来
参考链接

修改go文件 设置标题名称

设置英文标题

  • 把go的title标题改成 helloword
  • wails build
  • 运行build 文件夹的exe 程序 没有问题

设置纯中文标题

  • 把go的title标题改成纯中文标题 也没问题 运行完美
  • 把go的title标题改成 你好
  • wails build
  • 运行build 文件夹的exe 程序 没有问题

设置中英文混用 中文乱码

上代码

package main

import (
	"github.com/leaanthony/mewn"
	"github.com/wailsapp/wails"
	
)

func basic() string {
	return "Hello World!"
}



func main() {

	js := mewn.String("./frontend/dist/app.js")
	css := mewn.String("./frontend/dist/app.css")

	app := wails.CreateApp(&wails.AppConfig{
		Width:  1024,
		Height: 768,
		Title:  "cpu状态"                                   
		JS:     js,
		CSS:    css,
		Colour: "#131313",
	})
	app.Bind(basic)
	app.Run()
}
  • wails build
  • 我把点击提示和显示label默认值改了
  • 标题显示中文时乱码 在这里插入图片描述

解决

标题乱码原因是 go的默认编码时utf8 在非window情况下显示中文没问题 在windows下显示中英文就显示乱码, 查看了一下资料,把编码改成gbk 就能解决中文乱码问题
修改后代码

/*
 * @Description:
 * @Author: JYS
 * @EMAIL: 18010181191@163.COM
 * @Date: 2020-07-10 11:06:57
 * @LastEditors: JYS
 * @LastEditTime: 2020-07-14 11:31:43
 */
package main

import (
	"github.com/leaanthony/mewn"
	"github.com/wailsapp/wails"
	"golang.org/x/text/encoding/simplifiedchinese"
)

func basic() string {
	return "Hello World!"
}

//ConvertStr2GBK 将utf-8编码的字符串转换为GBK编码
func ConvertStr2GBK(str string) string {
	ret, _ := simplifiedchinese.GBK.NewEncoder().String(str)
	return ret //如果转换失败返回空字符串

	//如果是[]byte格式的字符串,可以使用Bytes方法
	b, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str))
	return string(b)
}

//ConvertGBK2Str 将GBK编码的字符串转换为utf-8编码
func ConvertGBK2Str(gbkStr string) string {
	ret, _ := simplifiedchinese.GBK.NewDecoder().String(gbkStr)
	return ret //如果转换失败返回空字符串

	//如果是[]byte格式的字符串,可以使用Bytes方法
	b, _ := simplifiedchinese.GBK.NewDecoder().Bytes([]byte(gbkStr))
	return string(b)
}

func main() {

	js := mewn.String("./frontend/dist/app.js")
	css := mewn.String("./frontend/dist/app.css")

	app := wails.CreateApp(&wails.AppConfig{
		Width:  1024,
		Height: 768,
		Title:  ConvertStr2GBK("cpu状态"), // 转换为gbk 才能中英文显示,否则纯中文也没有问题 但是中英文混用中文乱码
		JS:     js,
		CSS:    css,
		Colour: "#131313",
	})
	app.Bind(basic)
	app.Run()
}

wails build 之后 完美解决
在这里插入图片描述

  • 这种办法适合中英文混合标题
  • 纯中文和纯英文标题 都没有乱码问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值