Skip to content

Commit

Permalink
feat(i18n): translations for backend (#344)
Browse files Browse the repository at this point in the history
* i18n: add i18n for server pacakge

Signed-off-by: qwqcode <[email protected]>

* i18n: add i18n for cmd pacakge

Signed-off-by: qwqcode <[email protected]>

* feat: add mustache syntax support for i18n.T in backend

Signed-off-by: qwqcode <[email protected]>

* i18n: improve i18n for backend further

Signed-off-by: qwqcode <[email protected]>

* i18n: `zh-CN` translation for backend

Signed-off-by: qwqcode <[email protected]>

Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode authored Jan 19, 2023
1 parent 891246a commit 42a714b
Show file tree
Hide file tree
Showing 65 changed files with 685 additions and 483 deletions.
23 changes: 12 additions & 11 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ArtalkJS/Artalk/internal/core"
"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/ArtalkJS/Artalk/internal/i18n"
"github.com/ArtalkJS/Artalk/internal/query"
"github.com/ArtalkJS/Artalk/internal/utils"
"github.com/sirupsen/logrus"
Expand All @@ -19,14 +20,13 @@ import (

var adminCmd = &cobra.Command{
Use: "admin",
Short: "创建管理员账号",
Long: "根据提示创建管理员账号",
Short: "Create or edit an administrator account",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
core.LoadCore(cfgFile, workDir)

fmt.Println("--------------------------------")
fmt.Println(" 管理员账户创建 ")
fmt.Println(" " + i18n.T("Create admin account"))
fmt.Println("--------------------------------")

username, email, password, err := credentials()
Expand All @@ -41,15 +41,16 @@ var adminCmd = &cobra.Command{
logrus.Fatal(err)
}

logrus.Info("该账户已存在,密码修改成功")
logrus.Info(i18n.T("{{name}} already exists", map[string]interface{}{"name": i18n.T("Account")}) +
", " + i18n.T("Password updated"))
return
}

user := entity.User{
Name: username,
Email: email,
IsAdmin: true,
BadgeName: "管理员",
BadgeName: i18n.T("Admin"),
BadgeColor: "#FF6C00",
}
user.SetPasswordEncrypt(password)
Expand All @@ -72,29 +73,29 @@ func init() {
func credentials() (string, string, string, error) {
reader := bufio.NewReader(os.Stdin)

fmt.Print("Enter Username: ")
fmt.Print(i18n.T("Enter {{name}}", map[string]interface{}{"name": i18n.T("Username")}) + ": ")
username, err := reader.ReadString('\n')
if err != nil {
return "", "", "", err
}

fmt.Print("Enter Email: ")
fmt.Print(i18n.T("Enter {{name}}", map[string]interface{}{"name": i18n.T("Email")}) + ": ")
email, err := reader.ReadString('\n')
if err != nil {
return "", "", "", err
}
if !utils.ValidateEmail(strings.TrimSpace(email)) {
return "", "", "", errors.New("邮箱格式不合法")
return "", "", "", errors.New("invalid email format")
}

fmt.Print("Enter Password: ")
fmt.Print(i18n.T("Enter {{name}}", map[string]interface{}{"name": i18n.T("Password")}) + ": ")
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", "", "", err
}

fmt.Println()
fmt.Print("Retype Password: ")
fmt.Print(i18n.T("Retype {{name}}", map[string]interface{}{"name": i18n.T("Password")}) + ": " + ": ")
byteRePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", "", "", err
Expand All @@ -106,7 +107,7 @@ func credentials() (string, string, string, error) {
rePassword := strings.TrimSpace(string(byteRePassword))

if rePassword != password {
return "", "", "", errors.New("输入的密码不一致")
return "", "", "", errors.New("inconsistent password input")
}

return strings.TrimSpace(username), strings.TrimSpace(email), password, nil
Expand Down
12 changes: 5 additions & 7 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ArtalkJS/Artalk/internal/artransfer"
"github.com/ArtalkJS/Artalk/internal/core"
"github.com/ArtalkJS/Artalk/internal/i18n"
"github.com/ArtalkJS/Artalk/internal/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -17,13 +18,10 @@ import (
var exportCmd = &cobra.Command{
Use: "export",
Aliases: []string{},
Short: "数据迁移 - 迁出",
Long: "\n# 数据迁移 - 迁出\n\n 将所有数据从 Artalk 导出,用作备份,或迁移至其他地方\n 打包所有数据并导出成 “Artalk 数据行囊 (Artrans)”,为数据迁移做准备\n" + `
- 重新导入 Artalk,可执行: artalk import <数据行囊文件路径>
- 文档:https://artalk.js.org/guide/transfer.html
`,
Short: "Artransfer - Export",
Long: "\n# Artransfer - Export\n\n See the documentation to learn more: https://artalk.js.org/guide/transfer.html",
Run: func(cmd *cobra.Command, args []string) {
core.LoadCore(cfgFile, workDir) // 装载核心
core.LoadCore(cfgFile, workDir)

jsonStr, err := artransfer.ExportArtransString()
if err != nil {
Expand Down Expand Up @@ -67,7 +65,7 @@ var exportCmd = &cobra.Command{
logrus.Fatal(err2)
}

logrus.Info("已导出 Artrans 文件:" + filename)
logrus.Info(i18n.T("Export complete") + ": " + filename)
}
},
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
)

var genCmd = &cobra.Command{
Use: "gen <类型> <目标路径>",
Short: "生成一些内容",
Long: "生成一些内容\n例如:artalk gen config ./artalk.yml",
Use: "gen <TYPE> <DEST>",
Short: "A collection of several useful generators",
Long: "Generate some content\ne.g. `artalk gen config ./artalk.yml`",
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
// 工作目录
// change working directory
if workDir != "" {
if err := os.Chdir(workDir); err != nil {
logrus.Fatal("工作目录切换错误 ", err)
logrus.Fatal("change working directory error ", err)
}
}

Expand Down
20 changes: 9 additions & 11 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,32 @@ import (

"github.com/ArtalkJS/Artalk/internal/artransfer"
"github.com/ArtalkJS/Artalk/internal/core"
"github.com/ArtalkJS/Artalk/internal/i18n"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var importCmd = &cobra.Command{
Use: "import <数据行囊文件路径>",
Use: "import <FILENAME>",
Aliases: []string{},
Short: "数据迁移 - 迁入",
Long: "\n# 数据迁移 - 迁入\n\n 从其他评论系统迁移数据到 Artalk\n" + `
- 导入前需要使用转换工具 Artransfer 将其他评论数据转为 Artrans 格式
- 文档:https://artalk.js.org/guide/transfer.html
`,
Args: cobra.MinimumNArgs(1),
Short: "Artransfer - Import",
Long: "\n# Artransfer - Import\n\n See the documentation to learn more: https://artalk.js.org/guide/transfer.html",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
core.LoadCore(cfgFile, workDir) // 装载核心
core.LoadCore(cfgFile, workDir) // load core

parcelFile := args[0]
if _, err := os.Stat(parcelFile); errors.Is(err, os.ErrNotExist) {
logrus.Fatal("`数据行囊` 文件不存在,请检查路径是否正确")
logrus.Fatal(i18n.T("{{name}} not found", map[string]interface{}{"name": i18n.T("File")}))
}

payload := args[1:]
payload = append(payload, "json_file:"+parcelFile)

// 导入 Artrans
// import Artrans
artransfer.RunImportArtrans(payload)

logrus.Info("导入结束")
logrus.Info(i18n.T("Import complete"))
},
}

Expand Down
20 changes: 10 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
var Version = config.Version + `/` + config.CommitHash

var Banner = `
________ ________ _________ ________ ___ ___ __
|\ __ \|\ __ \|\___ ___\\ __ \|\ \ |\ \|\ \
\ \ \|\ \ \ \|\ \|___ \ \_\ \ \|\ \ \ \ \ \ \/ /|_
\ \ __ \ \ _ _\ \ \ \ \ \ __ \ \ \ \ \ ___ \
\ \ \ \ \ \ \\ \| \ \ \ \ \ \ \ \ \ \____\ \ \\ \ \
________ ________ _________ ________ ___ ___ __
|\ __ \|\ __ \|\___ ___\\ __ \|\ \ |\ \|\ \
\ \ \|\ \ \ \|\ \|___ \ \_\ \ \|\ \ \ \ \ \ \/ /|_
\ \ __ \ \ _ _\ \ \ \ \ \ __ \ \ \ \ \ ___ \
\ \ \ \ \ \ \\ \| \ \ \ \ \ \ \ \ \ \____\ \ \\ \ \
\ \__\ \__\ \__\\ _\ \ \__\ \ \__\ \__\ \_______\ \__\\ \__\
\|__|\|__|\|__|\|__| \|__| \|__|\|__|\|_______|\|__| \|__|
Artalk (` + Version + `)
-> A Selfhosted Comment System.
Expand Down Expand Up @@ -49,13 +49,13 @@ func Execute() {

func init() {
rootCmd.SetVersionTemplate("Artalk ({{printf \"%s\" .Version}})\n")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "配置文件路径 (defaults are './artalk.yml')")
rootCmd.PersistentFlags().StringVarP(&workDir, "workdir", "w", "", "程序工作目录 (defaults are './')")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file path (defaults are './artalk.yml')")
rootCmd.PersistentFlags().StringVarP(&workDir, "workdir", "w", "", "program working directory (defaults are './')")

// Version Command
versionCmd := &cobra.Command{
Use: "version",
Short: "输出版本信息",
Short: "Output Version Information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Artalk (" + Version + ")")
},
Expand All @@ -65,7 +65,7 @@ func init() {
// Config Command
configCmd := &cobra.Command{
Use: "config",
Short: "输出配置信息",
Short: "Output Config Information",
Run: func(cmd *cobra.Command, args []string) {
core.LoadConfOnly(cfgFile, workDir)
buf, _ := json.MarshalIndent(config.Instance, "", " ")
Expand Down
8 changes: 4 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
var serverCmd = &cobra.Command{
Use: "server",
Aliases: []string{"serve"},
Short: "启动服务器",
Short: "Start the server",
Long: Banner,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
core.LoadCore(cfgFile, workDir) // 装载核心
core.LoadCore(cfgFile, workDir)

fmt.Println(Banner)
fmt.Print("-------------------------------\n\n")
Expand Down Expand Up @@ -62,6 +62,6 @@ var serverCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(serverCmd)

flagPV(serverCmd, "host", "", "0.0.0.0", "监听 IP")
flagPV(serverCmd, "port", "", 23366, "监听端口")
flagPV(serverCmd, "host", "", "0.0.0.0", "Listening IP")
flagPV(serverCmd, "port", "", 23366, "Listening port")
}
19 changes: 10 additions & 9 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ArtalkJS/Artalk/internal/config"
"github.com/ArtalkJS/Artalk/internal/core"
"github.com/ArtalkJS/Artalk/internal/i18n"
"github.com/blang/semver"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"github.com/sirupsen/logrus"
Expand All @@ -16,14 +17,14 @@ import (
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Aliases: []string{"update"},
Short: "升级到最新版",
Long: " Artalk 升级到最新版本,\n更新源为 GitHub Releases\n更新需要重启 Artalk 才能生效。",
Short: "Upgrade to the latest version",
Long: "Upgrade Artalk to the latest version, \n update source is GitHub Releases, \n update need to restart Artalk to take effect.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// loadCore() // 装载核心
// loadCore()
core.LoadConfOnly(cfgFile, workDir)

logrus.Info("从 GitHub Release 检索更新中...")
logrus.Info(i18n.T("Checking for updates") + "...")

latest, found, err := selfupdate.DetectLatest("ArtalkJS/Artalk")
if err != nil {
Expand All @@ -34,24 +35,24 @@ var upgradeCmd = &cobra.Command{
if !ignoreVersionCheck {
v := semver.MustParse(strings.TrimPrefix(config.Version, "v"))
if !found || latest.Version.LTE(v) {
logrus.Println("当前已是最新版本 v" + v.String() + " 无需升级")
logrus.Println(i18n.T("Current version is the latest") + " (v" + v.String() + ")")
return
}
}

logrus.Info("发现新版本: v" + latest.Version.String())
logrus.Info("正在下载更新中...")
logrus.Info(i18n.T("New version available") + ": v" + latest.Version.String())
logrus.Info(i18n.T("Downloading") + "...")

exe, err := os.Executable()
if err != nil {
logrus.Fatal("Could not locate executable path ", err)
}

if err := selfupdate.UpdateTo(latest.AssetURL, exe); err != nil {
logrus.Fatal("更新失败 ", err)
logrus.Fatal(i18n.T("Update failed")+" ", err)
}

logrus.Println("更新完毕")
logrus.Println(i18n.T("Update complete"))
fmt.Println("\n-------------------------------\n v" +
latest.Version.String() +
" Release Note\n" +
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra"
)

//// 捷径函数 ////
//// Shortcut Functions ////

func flag(cmd *cobra.Command, name string, defaultVal interface{}, usage string) {
f := cmd.PersistentFlags()
Expand Down
Loading

0 comments on commit 42a714b

Please sign in to comment.