mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-10-14 14:30:26 +00:00
feat: initial official i18n support for backend
This commit is contained in:
72
common/i18n/i18n.go
Normal file
72
common/i18n/i18n.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
//go:embed locales/*.json
|
||||
var localesFS embed.FS
|
||||
|
||||
var (
|
||||
translations = make(map[string]map[string]string)
|
||||
defaultLang = "en"
|
||||
ContextKey = "i18n"
|
||||
)
|
||||
|
||||
// Init loads all translation files from embedded filesystem
|
||||
func Init() error {
|
||||
entries, err := localesFS.ReadDir("locales")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".json") {
|
||||
continue
|
||||
}
|
||||
|
||||
langCode := strings.TrimSuffix(entry.Name(), ".json")
|
||||
content, err := localesFS.ReadFile("locales/" + entry.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var translation map[string]string
|
||||
if err := json.Unmarshal(content, &translation); err != nil {
|
||||
return err
|
||||
}
|
||||
translations[langCode] = translation
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetLang(c *gin.Context) string {
|
||||
rawLang, ok := c.Get(ContextKey)
|
||||
if !ok {
|
||||
return defaultLang
|
||||
}
|
||||
lang, _ := rawLang.(string)
|
||||
if lang != "" {
|
||||
return lang
|
||||
}
|
||||
return defaultLang
|
||||
}
|
||||
|
||||
func Translate(c *gin.Context, message string) string {
|
||||
lang := GetLang(c)
|
||||
return translateHelper(lang, message)
|
||||
}
|
||||
|
||||
func translateHelper(lang, message string) string {
|
||||
if trans, ok := translations[lang]; ok {
|
||||
if translated, exists := trans[message]; exists {
|
||||
return translated
|
||||
}
|
||||
}
|
||||
return message
|
||||
}
|
11
common/i18n/locales/en.json
Normal file
11
common/i18n/locales/en.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"success": "Success",
|
||||
"unauthorized": "Unauthorized",
|
||||
"forbidden": "Forbidden",
|
||||
"invalid_token": "Invalid token",
|
||||
"channel_not_found": "Channel not found",
|
||||
"invalid_request": "Invalid request",
|
||||
"internal_error": "Internal server error",
|
||||
"quota_exceeded": "Quota exceeded",
|
||||
"invalid_input": "Invalid input, please check your input"
|
||||
}
|
11
common/i18n/locales/zh-CN.json
Normal file
11
common/i18n/locales/zh-CN.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"success": "成功",
|
||||
"unauthorized": "未授权",
|
||||
"forbidden": "禁止访问",
|
||||
"invalid_token": "无效的令牌",
|
||||
"channel_not_found": "未找到渠道",
|
||||
"invalid_request": "无效的请求",
|
||||
"internal_error": "服务器内部错误",
|
||||
"quota_exceeded": "配额已用尽",
|
||||
"invalid_input": "无效的输入,请检查您的输入"
|
||||
}
|
Reference in New Issue
Block a user