feat: add i18n support

This commit is contained in:
uubulb
2024-11-01 05:07:04 +08:00
parent 482d787a56
commit 5114fc2854
30 changed files with 930 additions and 91 deletions

105
pkg/i18n/i18n.go Normal file
View File

@@ -0,0 +1,105 @@
package i18n
import (
"embed"
"fmt"
"sync"
"github.com/chai2010/gettext-go"
)
//go:embed translations
var Translations embed.FS
var Languages = map[string]string{
"zh_CN": "简体中文",
"zh_TW": "繁體中文",
"en_US": "English",
"es_ES": "Español",
}
type Localizer struct {
intlMap map[string]gettext.Gettexter
lang string
mu sync.RWMutex
}
func NewLocalizer(lang, domain, path string, data any) *Localizer {
intl := gettext.New(domain, path, data)
intl.SetLanguage(lang)
intlMap := make(map[string]gettext.Gettexter)
intlMap[lang] = intl
return &Localizer{intlMap: intlMap, lang: lang}
}
func (l *Localizer) SetLanguage(lang string) {
l.mu.Lock()
defer l.mu.Unlock()
l.lang = lang
}
func (l *Localizer) Exists(lang string) bool {
l.mu.RLock()
defer l.mu.RUnlock()
if _, ok := l.intlMap[lang]; ok {
return ok
}
return false
}
func (l *Localizer) AppendIntl(lang, domain, path string, data any) {
intl := gettext.New(domain, path, data)
intl.SetLanguage(lang)
l.mu.Lock()
defer l.mu.Unlock()
l.intlMap[lang] = intl
}
// Modified from k8s.io/kubectl/pkg/util/i18n
func (l *Localizer) T(orig string) string {
l.mu.RLock()
intl, ok := l.intlMap[l.lang]
l.mu.RUnlock()
if !ok {
return orig
}
return intl.PGettext("", orig)
}
// N translates a string, possibly substituting arguments into it along
// the way. If len(args) is > 0, args1 is assumed to be the plural value
// and plural translation is used.
func (l *Localizer) N(orig string, args ...int) string {
l.mu.RLock()
intl, ok := l.intlMap[l.lang]
l.mu.RUnlock()
if !ok {
return orig
}
if len(args) == 0 {
return intl.PGettext("", orig)
}
return fmt.Sprintf(intl.PNGettext("", orig, orig+".plural", args[0]),
args[0])
}
// ErrorT produces an error with a translated error string.
// Substitution is performed via the `T` function above, following
// the same rules.
func (l *Localizer) ErrorT(defaultValue string, args ...any) error {
return fmt.Errorf(l.T(defaultValue), args...)
}
func (l *Localizer) Tf(defaultValue string, args ...any) string {
return fmt.Sprintf(l.T(defaultValue), args...)
}

220
pkg/i18n/template.pot Normal file
View File

@@ -0,0 +1,220 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-11-01 05:06+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: cmd/dashboard/controller/alertrule.go:100
#, c-format
msgid "alert id %d does not exist"
msgstr ""
#: cmd/dashboard/controller/alertrule.go:155
msgid "duration need to be at least 3"
msgstr ""
#: cmd/dashboard/controller/alertrule.go:159
msgid "cycle_interval need to be at least 1"
msgstr ""
#: cmd/dashboard/controller/alertrule.go:162
msgid "cycle_start is not set"
msgstr ""
#: cmd/dashboard/controller/alertrule.go:165
msgid "cycle_start is a future value"
msgstr ""
#: cmd/dashboard/controller/alertrule.go:170
msgid "need to configure at least a single rule"
msgstr ""
#: cmd/dashboard/controller/controller.go:188
msgid "database error"
msgstr ""
#: cmd/dashboard/controller/cron.go:63 cmd/dashboard/controller/cron.go:122
msgid "scheduled tasks cannot be triggered by alarms"
msgstr ""
#: cmd/dashboard/controller/cron.go:161
#, c-format
msgid "task id %d does not exist"
msgstr ""
#: cmd/dashboard/controller/ddns.go:56 cmd/dashboard/controller/ddns.go:120
msgid "the retry count must be an integer between 1 and 10"
msgstr ""
#: cmd/dashboard/controller/ddns.go:79 cmd/dashboard/controller/ddns.go:148
msgid "error parsing %s: %v"
msgstr ""
#: cmd/dashboard/controller/ddns.go:125 cmd/dashboard/controller/nat.go:95
#, c-format
msgid "profile id %d does not exist"
msgstr ""
#: cmd/dashboard/controller/fm.go:45 cmd/dashboard/controller/terminal.go:43
msgid "server not found or not connected"
msgstr ""
#: cmd/dashboard/controller/notification.go:67
#: cmd/dashboard/controller/notification.go:125
msgid "a test message"
msgstr ""
#: cmd/dashboard/controller/notification.go:106
#, c-format
msgid "notification id %d does not exist"
msgstr ""
#: cmd/dashboard/controller/notification_group.go:80
#: cmd/dashboard/controller/notification_group.go:142
msgid "have invalid notification id"
msgstr ""
#: cmd/dashboard/controller/notification_group.go:131
#: cmd/dashboard/controller/server_group.go:130
#, c-format
msgid "group id %d does not exist"
msgstr ""
#: cmd/dashboard/controller/server.go:59
#, c-format
msgid "server id %d does not exist"
msgstr ""
#: cmd/dashboard/controller/server_group.go:78
#: cmd/dashboard/controller/server_group.go:139
msgid "have invalid server id"
msgstr ""
#: cmd/dashboard/controller/service.go:79
#: cmd/dashboard/controller/service.go:155
msgid "server not found"
msgstr ""
#: cmd/dashboard/controller/service.go:86
msgid "unauthorized"
msgstr ""
#: cmd/dashboard/controller/service.go:247
#, c-format
msgid "service id %d does not exist"
msgstr ""
#: cmd/dashboard/controller/user.go:45
msgid "password length must be greater than 6"
msgstr ""
#: cmd/dashboard/controller/user.go:48
msgid "username can't be empty"
msgstr ""
#: service/rpc/io_stream.go:122
msgid "timeout: no connection established"
msgstr ""
#: service/rpc/io_stream.go:125
msgid "timeout: user connection not established"
msgstr ""
#: service/rpc/io_stream.go:128
msgid "timeout: agent connection not established"
msgstr ""
#: service/rpc/nezha.go:57
msgid "Scheduled Task Executed Successfully"
msgstr ""
#: service/rpc/nezha.go:61
msgid "Scheduled Task Executed Failed"
msgstr ""
#: service/rpc/nezha.go:156
msgid "IP Changed"
msgstr ""
#: service/singleton/alertsentinel.go:159
msgid "Incident"
msgstr ""
#: service/singleton/alertsentinel.go:169
msgid "Resolved"
msgstr ""
#: service/singleton/crontask.go:52
msgid "Tasks failed to register: ["
msgstr ""
#: service/singleton/crontask.go:59
msgid ""
"] These tasks will not execute properly. Fix them in the admin dashboard."
msgstr ""
#: service/singleton/crontask.go:150 service/singleton/crontask.go:175
#, c-format
msgid "[Task failed] %s: server %s is offline and cannot execute the task"
msgstr ""
#: service/singleton/servicesentinel.go:439
#, c-format
msgid "[Latency] %s %2f > %2f, Reporter: %s"
msgstr ""
#: service/singleton/servicesentinel.go:446
#, c-format
msgid "[Latency] %s %2f < %2f, Reporter: %s"
msgstr ""
#: service/singleton/servicesentinel.go:472
#, c-format
msgid "[%s] %s Reporter: %s, Error: %s"
msgstr ""
#: service/singleton/servicesentinel.go:515
#, c-format
msgid "[SSL] Fetch cert info failed, %s %s"
msgstr ""
#: service/singleton/servicesentinel.go:555
#, c-format
msgid "The SSL certificate will expire within seven days. Expiration time: %s"
msgstr ""
#: service/singleton/servicesentinel.go:568
#, c-format
msgid ""
"SSL certificate changed, old: issuer %s, expires at %s; new: issuer %s, "
"expires at %s"
msgstr ""
#: service/singleton/servicesentinel.go:604
msgid "No Data"
msgstr ""
#: service/singleton/servicesentinel.go:606
msgid "Good"
msgstr ""
#: service/singleton/servicesentinel.go:608
msgid "Low Availability"
msgstr ""
#: service/singleton/servicesentinel.go:610
msgid "Down"
msgstr ""

Binary file not shown.

View File

@@ -0,0 +1,225 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-11-01 04:58+0800\n"
"PO-Revision-Date: 2024-11-01 05:05+0800\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: en_US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 3.5\n"
#: cmd/dashboard/controller/alertrule.go:100
#, c-format
msgid "alert id %d does not exist"
msgstr "alert id %d does not exist"
#: cmd/dashboard/controller/alertrule.go:155
msgid "duration need to be at least 3"
msgstr "duration need to be at least 3"
#: cmd/dashboard/controller/alertrule.go:159
msgid "cycle_interval need to be at least 1"
msgstr "cycle_interval need to be at least 1"
#: cmd/dashboard/controller/alertrule.go:162
msgid "cycle_start is not set"
msgstr "cycle_start is not set"
#: cmd/dashboard/controller/alertrule.go:165
msgid "cycle_start is a future value"
msgstr "cycle_start is a future value"
#: cmd/dashboard/controller/alertrule.go:170
msgid "need to configure at least a single rule"
msgstr "need to configure at least a single rule"
#: cmd/dashboard/controller/controller.go:188
msgid "database error"
msgstr "database error"
#: cmd/dashboard/controller/cron.go:63 cmd/dashboard/controller/cron.go:122
msgid "scheduled tasks cannot be triggered by alarms"
msgstr "scheduled tasks cannot be triggered by alarms"
#: cmd/dashboard/controller/cron.go:161
#, c-format
msgid "task id %d does not exist"
msgstr "task id %d does not exist"
#: cmd/dashboard/controller/ddns.go:56 cmd/dashboard/controller/ddns.go:120
msgid "the retry count must be an integer between 1 and 10"
msgstr "the retry count must be an integer between 1 and 10"
#: cmd/dashboard/controller/ddns.go:79 cmd/dashboard/controller/ddns.go:148
msgid "error parsing %s: %v"
msgstr "error parsing %s: %v"
#: cmd/dashboard/controller/ddns.go:125 cmd/dashboard/controller/nat.go:95
#, c-format
msgid "profile id %d does not exist"
msgstr "profile id %d does not exist"
#: cmd/dashboard/controller/fm.go:45 cmd/dashboard/controller/terminal.go:43
msgid "server not found or not connected"
msgstr "server not found or not connected"
#: cmd/dashboard/controller/notification.go:67
#: cmd/dashboard/controller/notification.go:125
msgid "a test message"
msgstr "a test message"
#: cmd/dashboard/controller/notification.go:106
#, c-format
msgid "notification id %d does not exist"
msgstr "notification id %d does not exist"
#: cmd/dashboard/controller/notification_group.go:80
#: cmd/dashboard/controller/notification_group.go:142
msgid "have invalid notification id"
msgstr "have invalid notification id"
#: cmd/dashboard/controller/notification_group.go:131
#: cmd/dashboard/controller/server_group.go:130
#, c-format
msgid "group id %d does not exist"
msgstr "group id %d does not exist"
#: cmd/dashboard/controller/server.go:59
#, c-format
msgid "server id %d does not exist"
msgstr "server id %d does not exist"
#: cmd/dashboard/controller/server_group.go:78
#: cmd/dashboard/controller/server_group.go:139
msgid "have invalid server id"
msgstr "have invalid server id"
#: cmd/dashboard/controller/service.go:79
#: cmd/dashboard/controller/service.go:155
msgid "server not found"
msgstr "server not found"
#: cmd/dashboard/controller/service.go:86
msgid "unauthorized"
msgstr "unauthorized"
#: cmd/dashboard/controller/service.go:247
#, c-format
msgid "service id %d does not exist"
msgstr "service id %d does not exist"
#: cmd/dashboard/controller/user.go:45
msgid "password length must be greater than 6"
msgstr "password length must be greater than 6"
#: cmd/dashboard/controller/user.go:48
msgid "username can't be empty"
msgstr "username can't be empty"
#: service/rpc/io_stream.go:122
msgid "timeout: no connection established"
msgstr "timeout: no connection established"
#: service/rpc/io_stream.go:125
msgid "timeout: user connection not established"
msgstr "timeout: user connection not established"
#: service/rpc/io_stream.go:128
msgid "timeout: agent connection not established"
msgstr "timeout: agent connection not established"
#: service/rpc/nezha.go:57
msgid "Scheduled Task Executed Successfully"
msgstr "Scheduled Task Executed Successfully"
#: service/rpc/nezha.go:61
msgid "Scheduled Task Executed Failed"
msgstr "Scheduled Task Executed Failed"
#: service/rpc/nezha.go:156
msgid "IP Changed"
msgstr "IP Changed"
#: service/singleton/alertsentinel.go:159
msgid "Incident"
msgstr "Incident"
#: service/singleton/alertsentinel.go:169
msgid "Resolved"
msgstr "Resolved"
#: service/singleton/crontask.go:52
msgid "Tasks failed to register: ["
msgstr "Tasks failed to register: ["
#: service/singleton/crontask.go:59
msgid ""
"] These tasks will not execute properly. Fix them in the admin dashboard."
msgstr ""
"] These tasks will not execute properly. Fix them in the admin dashboard."
#: service/singleton/crontask.go:150 service/singleton/crontask.go:175
#, c-format
msgid "[Task failed] %s: server %s is offline and cannot execute the task"
msgstr "[Task failed] %s: server %s is offline and cannot execute the task"
#: service/singleton/servicesentinel.go:439
#, c-format
msgid "[Latency] %s %2f > %2f, Reporter: %s"
msgstr "[Latency] %s %2f > %2f, Reporter: %s"
#: service/singleton/servicesentinel.go:446
#, c-format
msgid "[Latency] %s %2f < %2f, Reporter: %s"
msgstr "[Latency] %s %2f < %2f, Reporter: %s"
#: service/singleton/servicesentinel.go:472
#, c-format
msgid "[%s] %s Reporter: %s, Error: %s"
msgstr "[%s] %s Reporter: %s, Error: %s"
#: service/singleton/servicesentinel.go:515
#, c-format
msgid "[SSL] Fetch cert info failed, %s %s"
msgstr "[SSL] Fetch cert info failed, %s %s"
#: service/singleton/servicesentinel.go:555
#, c-format
msgid "The SSL certificate will expire within seven days. Expiration time: %s"
msgstr "The SSL certificate will expire within seven days. Expiration time: %s"
#: service/singleton/servicesentinel.go:568
#, c-format
#| msgid "SSL certificate changed, old: %s, %s expired; new: %s, %s expired."
msgid ""
"SSL certificate changed, old: issuer %s, expires at %s; new: issuer %s, "
"expires at %s"
msgstr ""
"SSL certificate changed, old: issuer %s, expires at %s; new: issuer %s, "
"expires at %s"
#: service/singleton/servicesentinel.go:604
msgid "No Data"
msgstr "No Data"
#: service/singleton/servicesentinel.go:606
msgid "Good"
msgstr "Good"
#: service/singleton/servicesentinel.go:608
msgid "Low Availability"
msgstr "Low Availability"
#: service/singleton/servicesentinel.go:610
msgid "Down"
msgstr "Down"

Binary file not shown.

View File

@@ -0,0 +1,222 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-11-01 04:58+0800\n"
"PO-Revision-Date: 2024-11-01 05:05+0800\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: zh_CN\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 3.5\n"
#: cmd/dashboard/controller/alertrule.go:100
#, c-format
msgid "alert id %d does not exist"
msgstr "告警 ID %d 不存在"
#: cmd/dashboard/controller/alertrule.go:155
msgid "duration need to be at least 3"
msgstr "duration 至少为 3"
#: cmd/dashboard/controller/alertrule.go:159
msgid "cycle_interval need to be at least 1"
msgstr "cycle_interval 至少为 1"
#: cmd/dashboard/controller/alertrule.go:162
msgid "cycle_start is not set"
msgstr "cycle_start 未设置"
#: cmd/dashboard/controller/alertrule.go:165
msgid "cycle_start is a future value"
msgstr "cycle_start 是未来值"
#: cmd/dashboard/controller/alertrule.go:170
msgid "need to configure at least a single rule"
msgstr "需要至少定义一条规则"
#: cmd/dashboard/controller/controller.go:188
msgid "database error"
msgstr "数据库错误"
#: cmd/dashboard/controller/cron.go:63 cmd/dashboard/controller/cron.go:122
msgid "scheduled tasks cannot be triggered by alarms"
msgstr "计划任务不能被告警触发"
#: cmd/dashboard/controller/cron.go:161
#, c-format
msgid "task id %d does not exist"
msgstr "任务 id %d 不存在"
#: cmd/dashboard/controller/ddns.go:56 cmd/dashboard/controller/ddns.go:120
msgid "the retry count must be an integer between 1 and 10"
msgstr "重试次数必须为大于 1 且不超过 10 的整数"
#: cmd/dashboard/controller/ddns.go:79 cmd/dashboard/controller/ddns.go:148
msgid "error parsing %s: %v"
msgstr "解析 %s 时发生错误:%v"
#: cmd/dashboard/controller/ddns.go:125 cmd/dashboard/controller/nat.go:95
#, c-format
msgid "profile id %d does not exist"
msgstr "配置 id %d 不存在"
#: cmd/dashboard/controller/fm.go:45 cmd/dashboard/controller/terminal.go:43
msgid "server not found or not connected"
msgstr "服务器未找到或仍未连接"
#: cmd/dashboard/controller/notification.go:67
#: cmd/dashboard/controller/notification.go:125
msgid "a test message"
msgstr "一条测试信息"
#: cmd/dashboard/controller/notification.go:106
#, c-format
msgid "notification id %d does not exist"
msgstr "通知方式 id %d 不存在"
#: cmd/dashboard/controller/notification_group.go:80
#: cmd/dashboard/controller/notification_group.go:142
msgid "have invalid notification id"
msgstr "通知方式 id 无效"
#: cmd/dashboard/controller/notification_group.go:131
#: cmd/dashboard/controller/server_group.go:130
#, c-format
msgid "group id %d does not exist"
msgstr "组 id %d 不存在"
#: cmd/dashboard/controller/server.go:59
#, c-format
msgid "server id %d does not exist"
msgstr "服务器 id %d 不存在"
#: cmd/dashboard/controller/server_group.go:78
#: cmd/dashboard/controller/server_group.go:139
msgid "have invalid server id"
msgstr "服务器 id 无效"
#: cmd/dashboard/controller/service.go:79
#: cmd/dashboard/controller/service.go:155
msgid "server not found"
msgstr "未找到服务器"
#: cmd/dashboard/controller/service.go:86
msgid "unauthorized"
msgstr "未授权"
#: cmd/dashboard/controller/service.go:247
#, c-format
msgid "service id %d does not exist"
msgstr "服务 id %d 不存在"
#: cmd/dashboard/controller/user.go:45
msgid "password length must be greater than 6"
msgstr "密码长度必须大于6"
#: cmd/dashboard/controller/user.go:48
msgid "username can't be empty"
msgstr "用户名不能为空"
#: service/rpc/io_stream.go:122
msgid "timeout: no connection established"
msgstr "超时:无连接建立"
#: service/rpc/io_stream.go:125
msgid "timeout: user connection not established"
msgstr "超时:用户连接未建立"
#: service/rpc/io_stream.go:128
msgid "timeout: agent connection not established"
msgstr "超时agent 连接未建立"
#: service/rpc/nezha.go:57
msgid "Scheduled Task Executed Successfully"
msgstr "计划任务执行成功"
#: service/rpc/nezha.go:61
msgid "Scheduled Task Executed Failed"
msgstr "计划任务执行失败"
#: service/rpc/nezha.go:156
msgid "IP Changed"
msgstr "IP变更"
#: service/singleton/alertsentinel.go:159
msgid "Incident"
msgstr "事件"
#: service/singleton/alertsentinel.go:169
msgid "Resolved"
msgstr "恢复"
#: service/singleton/crontask.go:52
msgid "Tasks failed to register: ["
msgstr "注册失败的任务:["
#: service/singleton/crontask.go:59
msgid ""
"] These tasks will not execute properly. Fix them in the admin dashboard."
msgstr "这些任务将无法正常执行,请进入后点重新修改保存。"
#: service/singleton/crontask.go:150 service/singleton/crontask.go:175
#, c-format
msgid "[Task failed] %s: server %s is offline and cannot execute the task"
msgstr "[任务失败] %s服务器 %s 离线,无法执行"
#: service/singleton/servicesentinel.go:439
#, c-format
msgid "[Latency] %s %2f > %2f, Reporter: %s"
msgstr "[延迟告警] %s %2f > %2f, 报告服务: %s"
#: service/singleton/servicesentinel.go:446
#, c-format
msgid "[Latency] %s %2f < %2f, Reporter: %s"
msgstr "[延迟告警] %s %2f < %2f, 报告服务: %s"
#: service/singleton/servicesentinel.go:472
#, c-format
msgid "[%s] %s Reporter: %s, Error: %s"
msgstr "[%s] %s 报告服务: %s, 错误信息: %s"
#: service/singleton/servicesentinel.go:515
#, c-format
msgid "[SSL] Fetch cert info failed, %s %s"
msgstr "[SSL] 获取证书信息失败, %s %s"
#: service/singleton/servicesentinel.go:555
#, c-format
msgid "The SSL certificate will expire within seven days. Expiration time: %s"
msgstr "SSL 证书将在 7 天内过期。过期时间为:%s"
#: service/singleton/servicesentinel.go:568
#, c-format
msgid ""
"SSL certificate changed, old: issuer %s, expires at %s; new: issuer %s, "
"expires at %s"
msgstr ""
"SSL 证书发生更改,旧值:颁发者 %s过期日 %s新值颁发者 %s过期日 %s"
#: service/singleton/servicesentinel.go:604
msgid "No Data"
msgstr "无数据"
#: service/singleton/servicesentinel.go:606
msgid "Good"
msgstr "正常"
#: service/singleton/servicesentinel.go:608
msgid "Low Availability"
msgstr "低可用"
#: service/singleton/servicesentinel.go:610
msgid "Down"
msgstr "故障"