跳至主要內容

国际化

Ryan SU小于 1 分钟

项目支持国际化多语言,主要分为前端国际化和后端国际化

前端国际化

前端国际化文件位于 src/locales/* 中,前端静态文本翻译由前端进行国际化

在 vue 或 ts 文件中调用

const { t } = useI18n();

// 使用方法
const name = t("sys.dictionary.dictionaryList");

详细教程open in new window

后端国际化

国际化文件位于 i18n 文件夹,后端国际化用于翻译所有来自后端的动态数据,core 中的目录结构为

├── locale
│   ├── en.json
│   └── zh.json
├── translator.go
├── translator_test.go
└── var.go

只需在 en.json 和 zh.json 中添加翻译即可

初始化方法

trans := i18n.NewTranslator(i18n2.LocaleFS)

使用方法

l.svcCtx.Trans.Trans(l.ctx, i18n.Success)

读取外部文件

在 v1.0.7 版本之后支持读取外部 i18n 文件

    var trans *i18n.Translator
	if c.I18nConf.Dir != "" {
		trans = i18n.NewTranslatorFromFile(c.I18nConf)
	} else {
		trans = i18n.NewTranslator(i18n2.LocaleFS)
	}

使用 i18n.NewTranslatorFromFile(c.I18nConf) 即可读取外部文件