mirror of
https://github.com/flipped-aurora/gin-vue-admin.git
synced 2026-10-02 00:27:13 +00:00
+ Add a default language option so this language will be used as a fallback in case of no current language transaction exist for any string.
+ Add translation to "auto_code_package.go" in server module. + Add translation to "auto_code_plugin.go" in server module. + Add translation to "request.js" in web module. + Add translation to "sysDictionary.vue" in web module. + Rearrange translations file for web module.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
|
||||
@@ -8,7 +10,6 @@ import (
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AutoCodePackageApi struct{}
|
||||
@@ -30,16 +31,16 @@ func (a *AutoCodePackageApi) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if strings.Contains(info.PackageName, "\\") || strings.Contains(info.PackageName, "/") || strings.Contains(info.PackageName, "..") {
|
||||
response.FailWithMessage("包名不合法", c)
|
||||
response.FailWithMessage(global.Translate("system.auto_code_package.packageNameInvalid"), c)
|
||||
return
|
||||
} // PackageName可能导致路径穿越的问题 / 和 \ 都要防止
|
||||
err := autoCodePackageService.Create(c.Request.Context(), &info)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
||||
response.FailWithMessage("创建失败", c)
|
||||
global.GVA_LOG.Error(global.Translate("gernal.creationFail"), zap.Error(err))
|
||||
response.FailWithMessage(global.Translate("gernal.creationFailErr"), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage("创建成功", c)
|
||||
response.OkWithMessage(global.Translate("gernal.createSuccss"), c)
|
||||
}
|
||||
|
||||
// Delete
|
||||
@@ -56,11 +57,11 @@ func (a *AutoCodePackageApi) Delete(c *gin.Context) {
|
||||
_ = c.ShouldBindJSON(&info)
|
||||
err := autoCodePackageService.Delete(c.Request.Context(), info)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("删除失败!", zap.Error(err))
|
||||
response.FailWithMessage("删除失败", c)
|
||||
global.GVA_LOG.Error(global.Translate("genral.deleteFail"), zap.Error(err))
|
||||
response.FailWithMessage(global.Translate("general.deletFailErr"), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage("删除成功", c)
|
||||
response.OkWithMessage(global.Translate("general.deleteSuccess"), c)
|
||||
}
|
||||
|
||||
// All
|
||||
@@ -74,11 +75,11 @@ func (a *AutoCodePackageApi) Delete(c *gin.Context) {
|
||||
func (a *AutoCodePackageApi) All(c *gin.Context) {
|
||||
data, err := autoCodePackageService.All(c.Request.Context())
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取失败", c)
|
||||
global.GVA_LOG.Error(global.Translate("global.getDataFail"), zap.Error(err))
|
||||
response.FailWithMessage(global.Translate("global.getDataFailErr"), c)
|
||||
return
|
||||
}
|
||||
response.OkWithDetailed(gin.H{"pkgs": data}, "获取成功", c)
|
||||
response.OkWithDetailed(gin.H{"pkgs": data}, global.Translate("general.getDataSuccess"), c)
|
||||
}
|
||||
|
||||
// Templates
|
||||
@@ -92,9 +93,9 @@ func (a *AutoCodePackageApi) All(c *gin.Context) {
|
||||
func (a *AutoCodePackageApi) Templates(c *gin.Context) {
|
||||
data, err := autoCodePackageService.Templates(c.Request.Context())
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取失败", c)
|
||||
global.GVA_LOG.Error(global.Translate("general.getDataFail"), zap.Error(err))
|
||||
response.FailWithMessage(global.Translate("general.getDataFailErr"), c)
|
||||
return
|
||||
}
|
||||
response.OkWithDetailed(data, "获取成功", c)
|
||||
response.OkWithDetailed(data, global.Translate("general.getDataSuccess"), c)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
||||
@@ -27,13 +28,13 @@ func (a *AutoCodePluginApi) Install(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
web, server, err := autoCodePluginService.Install(header)
|
||||
webStr := "web插件安装成功"
|
||||
serverStr := "server插件安装成功"
|
||||
webStr := global.Translate("system.auto_code_plugin.webPluginInstalledSuccess")
|
||||
serverStr := global.Translate("system.auto_code_plugin.serverPluginInstalledSuccess")
|
||||
if web == -1 {
|
||||
webStr = "web端插件未成功安装,请按照文档自行解压安装,如果为纯后端插件请忽略此条提示"
|
||||
webStr = global.Translate("system.auto_code_plugin.webPluginInstallFail")
|
||||
}
|
||||
if server == -1 {
|
||||
serverStr = "server端插件未成功安装,请按照文档自行解压安装,如果为纯前端插件请忽略此条提示"
|
||||
serverStr = global.Translate("system.auto_code_plugin.serverPluginInstallFail")
|
||||
}
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
@@ -50,7 +51,6 @@ func (a *AutoCodePluginApi) Install(c *gin.Context) {
|
||||
}}, c)
|
||||
}
|
||||
|
||||
// Packaged
|
||||
// @Tags AutoCodePlugin
|
||||
// @Summary 打包插件
|
||||
// @Security ApiKeyAuth
|
||||
@@ -63,11 +63,11 @@ func (a *AutoCodePluginApi) Packaged(c *gin.Context) {
|
||||
plugName := c.Query("plugName")
|
||||
zipPath, err := autoCodePluginService.PubPlug(plugName)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("打包失败!", zap.Error(err))
|
||||
response.FailWithMessage("打包失败"+err.Error(), c)
|
||||
global.GVA_LOG.Error(global.Translate("system.auto_code_plugin.packingFailed"), zap.Error(err))
|
||||
response.FailWithMessage(global.Translate("system.auto_code_plugin.packingFailedError")+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage(fmt.Sprintf("打包成功,文件路径为:%s", zipPath), c)
|
||||
response.OkWithMessage(fmt.Sprintf(global.Translate("system.auto_code_plugin.packingSuccess")+"%s", zipPath), c)
|
||||
}
|
||||
|
||||
// Packaged
|
||||
@@ -87,11 +87,11 @@ func (a *AutoCodePluginApi) InitMenu(c *gin.Context) {
|
||||
}
|
||||
err = autoCodePluginService.InitMenu(menuInfo)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建初始化Menu失败!", zap.Error(err))
|
||||
response.FailWithMessage("创建初始化Menu失败"+err.Error(), c)
|
||||
global.GVA_LOG.Error(global.Translate("system.auto_code_plugin.initMenuCreationFail"), zap.Error(err))
|
||||
response.FailWithMessage(global.Translate("system.auto_code_plugin.initMenuCreationFailError")+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage("文件变更成功", c)
|
||||
response.OkWithMessage(global.Translate("system.auto_code_plugin.fileChangedSuccessfully"), c)
|
||||
}
|
||||
|
||||
// Packaged
|
||||
@@ -111,9 +111,9 @@ func (a *AutoCodePluginApi) InitAPI(c *gin.Context) {
|
||||
}
|
||||
err = autoCodePluginService.InitAPI(apiInfo)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建初始化API失败!", zap.Error(err))
|
||||
response.FailWithMessage("创建初始化API失败"+err.Error(), c)
|
||||
global.GVA_LOG.Error(global.Translate("system.auto_code_plugin.initApiCreationFail"), zap.Error(err))
|
||||
response.FailWithMessage(global.Translate("system.auto_code_plugin.initApiCreationFailError")+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage("文件变更成功", c)
|
||||
response.OkWithMessage(global.Translate("system.auto_code_plugin.fileChangedSuccessfully"), c)
|
||||
}
|
||||
|
||||
+3
-2
@@ -266,5 +266,6 @@ cors:
|
||||
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
|
||||
allow-credentials: true # 布尔值
|
||||
language:
|
||||
language: en
|
||||
dir: ./resource/lang/
|
||||
language: en
|
||||
defualt-languge: zh # default language to be used as a fullback in case of translation to current language was not exist.
|
||||
dir: ./resource/lang/
|
||||
@@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
type Language struct {
|
||||
Language string `mapstructure:"language" json:"language" yaml:"language"`
|
||||
Dir string `mapstructure:"dir" json:"dir" yaml:"dir"`
|
||||
Language string `mapstructure:"language" json:"language" yaml:"language"`
|
||||
DefaultLanguage string `mapstructure:"defualt-languge" json:"defualt-languge" yaml:"defualt-languge"`
|
||||
Dir string `mapstructure:"dir" json:"dir" yaml:"dir"`
|
||||
}
|
||||
|
||||
@@ -64,6 +64,9 @@ func MustGetGlobalDBByDBName(dbname string) *gorm.DB {
|
||||
func Translate(msg string) string {
|
||||
if GVA_TRANSLATOR.IsInit {
|
||||
message := GVA_TRANSLATOR.TranslateMessage(msg)
|
||||
if message == "" {
|
||||
return msg
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -32,7 +32,8 @@ func main() {
|
||||
initialize.DBList()
|
||||
// added by mohamed hassan to support multilanguage
|
||||
global.GVA_TRANSLATOR = translate.Translator{} // create translator inestance here
|
||||
global.GVA_TRANSLATOR.InitTranslator(global.GVA_CONFIG.Language.Language, global.GVA_CONFIG.Language.Dir)
|
||||
//global.GVA_TRANSLATOR.InitTranslator(global.GVA_CONFIG.Language.Language, global.GVA_CONFIG.Language.Dir)
|
||||
global.GVA_TRANSLATOR.InitTranslatorEx(global.GVA_CONFIG.Language.Language, global.GVA_CONFIG.Language.DefaultLanguage, global.GVA_CONFIG.Language.Dir)
|
||||
// end of adding
|
||||
if global.GVA_DB != nil {
|
||||
initialize.RegisterTables() // Initializing database tables
|
||||
|
||||
@@ -95,6 +95,23 @@
|
||||
"batchDeleteSuccess": "Batch delete successfully"
|
||||
},
|
||||
"system": {
|
||||
"auto_code_package" : {
|
||||
"packageNameInvalid" : "The package name is invalid"
|
||||
},
|
||||
"auto_code_plugin" : {
|
||||
"webPluginInstalledSuccess" : "The web plugin was successfully installed",
|
||||
"serverPluginIstalledSuccess" : "Server plugin installed successfully",
|
||||
"webPluginInstallFail" : "The web-side plug-in was not installed successfully. Please follow the documentation to decompress and install it yourself. If it is a pure backend plug-in, please ignore this prompt",
|
||||
"serverPluginInstallFail" : "The server-side plug-in was not installed successfully. Please follow the documentation to decompress and install it yourself. If it is a pure front-end plug-in, please ignore this prompt",
|
||||
"packingFailed" : "Packing Failed!",
|
||||
"packingFailedError" : "Packing Failed ",
|
||||
"packingSuccess" : "The packaging is successful, the file path is: ",
|
||||
"initMenuCreationFail" : "Failed to create initialization Menu!",
|
||||
"initMenuCreationFailError" : "Failed to create initialization Menu ",
|
||||
"fileChangedSuccessfully" : "File changed successfully",
|
||||
"initApiCreationFail" : "Failed to create initialization API!",
|
||||
"initApiCreationFailError" : "Failed to create initialization API "
|
||||
},
|
||||
"api": {
|
||||
"group": {
|
||||
"systemUser": "System User",
|
||||
|
||||
@@ -95,8 +95,26 @@
|
||||
"batchDeleteSuccess": "批量删除成功"
|
||||
},
|
||||
"system": {
|
||||
"api": {
|
||||
"group": {
|
||||
"auto_code_package" : {
|
||||
"packageNameInvalid" : "包名不合法"
|
||||
},
|
||||
"auto_code_plugin" : {
|
||||
"webPluginInstalledSuccess" : "web插件安装成功",
|
||||
"serverPluginIstalledSuccess" : "server插件安装成功",
|
||||
"webPluginInstallFail" : "web端插件未成功安装,请按照文档自行解压安装,如果为纯后端插件请忽略此条提示",
|
||||
"serverPluginInstallFail" : "server端插件未成功安装,请按照文档自行解压安装,如果为纯前端插件请忽略此条提示",
|
||||
"packingFailed" : "打包失败!",
|
||||
"packingFailedError" : "打包失败",
|
||||
"packingSuccess" : "打包成功,文件路径为:",
|
||||
"initMenuCreationFail" : "创建初始化Menu失败!",
|
||||
"initMenuCreationFailError" : "创建初始化Menu失败",
|
||||
"fileChangedSuccessfully" : "文件变更成功",
|
||||
"initApiCreationFail" : "创建初始化API失败!",
|
||||
"initApiCreationFailError" : "创建初始化API失败"
|
||||
|
||||
},
|
||||
"api" : {
|
||||
"group" : {
|
||||
"systemUser": "系统用户",
|
||||
"api":"API",
|
||||
"role": "角色",
|
||||
|
||||
@@ -10,9 +10,10 @@ import (
|
||||
)
|
||||
|
||||
type Translator struct {
|
||||
IsInit bool // check if translator initialized or not
|
||||
bundle *i18n.Bundle
|
||||
localizer *i18n.Localizer
|
||||
IsInit bool // check if translator initialized or not
|
||||
bundle *i18n.Bundle
|
||||
localizer *i18n.Localizer
|
||||
defaultLocalizer *i18n.Localizer
|
||||
}
|
||||
|
||||
// func (t *Translator) InitTranslator(initLang string) {
|
||||
@@ -51,14 +52,51 @@ func (t *Translator) InitTranslator(initLang string, langPath string) {
|
||||
// end of adding
|
||||
}
|
||||
|
||||
func (t *Translator) InitTranslatorEx(initLang string, defaultLang string, langPath string) {
|
||||
|
||||
langFiles, err := ioutil.ReadDir(langPath)
|
||||
if err != nil {
|
||||
fmt.Printf("InitTranslator() Error: %v", err)
|
||||
}
|
||||
|
||||
t.bundle = i18n.NewBundle(language.English)
|
||||
t.bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
|
||||
|
||||
for _, langFile := range langFiles {
|
||||
if !langFile.IsDir() {
|
||||
langFilePath := langPath + langFile.Name()
|
||||
fmt.Printf("Language file: %s loaded.\r\n", langFilePath)
|
||||
t.bundle.MustLoadMessageFile(langFilePath)
|
||||
}
|
||||
}
|
||||
|
||||
t.localizer = i18n.NewLocalizer(t.bundle, initLang) // should add additionl check here
|
||||
|
||||
if defaultLang != "" {
|
||||
t.defaultLocalizer = i18n.NewLocalizer(t.bundle, defaultLang) // should add additionl check here
|
||||
} else {
|
||||
t.defaultLocalizer = i18n.NewLocalizer(t.bundle, "zh") // set default langauge as chinese in case of there is no transalation found
|
||||
}
|
||||
|
||||
t.IsInit = true
|
||||
// end of adding
|
||||
}
|
||||
|
||||
func (t *Translator) SetTranslatorLanguage(lang string) {
|
||||
t.localizer = i18n.NewLocalizer(t.bundle, lang)
|
||||
}
|
||||
|
||||
func (t *Translator) SetTranslatorDefaultLanguage(lang string) {
|
||||
t.defaultLocalizer = i18n.NewLocalizer(t.bundle, lang)
|
||||
}
|
||||
|
||||
func (t *Translator) TranslateMessage(messageID string) string {
|
||||
translatedMsg, err := t.localizer.LocalizeMessage(&i18n.Message{ID: messageID})
|
||||
if err != nil {
|
||||
return messageID
|
||||
if err != nil || translatedMsg == "" { // if translation fail use default language transalator
|
||||
translatedMsg, err = t.defaultLocalizer.LocalizeMessage(&i18n.Message{ID: messageID})
|
||||
if err != nil {
|
||||
return messageID
|
||||
}
|
||||
}
|
||||
//return t.localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: messageID})
|
||||
return translatedMsg
|
||||
|
||||
Reference in New Issue
Block a user