From 2fc49398781261db41767c825a8ce06577a9813e Mon Sep 17 00:00:00 2001 From: naiba Date: Sat, 30 Apr 2022 23:02:40 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor=20settings=20page?= =?UTF-8?q?=20[skip=20ci]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++-- cmd/dashboard/controller/member_page.go | 4 +++- model/config.go | 13 +++++++++++++ pkg/utils/utils.go | 11 ----------- resource/l10n/zh-CN.toml | 3 --- resource/template/dashboard/setting.html | 18 +++++++----------- service/singleton/l10n.go | 11 +++++------ 7 files changed, 38 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 049e6db..0cb3b83 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,17 @@ |
Default modified [Guide]
|
Neko Mdui @MikoyChinese
| | | ![默认主题魔改](https://fastly.jsdelivr.net/gh/idarku/img@main/me/1631120192341.webp) | ![Neko Mdui](resource/template/theme-mdui/screenshot.png) | | -## User Guide +## Supported Languages -[![Crowdin](https://badges.crowdin.net/nezha/localized.svg)](https://crowdin.com/project/nezha) +Help us improve translation [![Crowdin](https://badges.crowdin.net/nezha/localized.svg)](https://crowdin.com/project/nezha) + +- English +- 简体中文 +- Español _WIP_ + +You can change the system language in the settings page (`/setting`) after the admin panel is installed. + +## User Guide - [English](docs/UserGuide_en.md) - [中文文档](docs/UserGuide_zh.md) diff --git a/cmd/dashboard/controller/member_page.go b/cmd/dashboard/controller/member_page.go index e658369..df0cf7d 100644 --- a/cmd/dashboard/controller/member_page.go +++ b/cmd/dashboard/controller/member_page.go @@ -69,6 +69,8 @@ func (mp *memberPage) notification(c *gin.Context) { func (mp *memberPage) setting(c *gin.Context) { c.HTML(http.StatusOK, "dashboard/setting", mygin.CommonEnvironment(c, gin.H{ - "Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Settings"}), + "Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Settings"}), + "Languages": model.Languages, + "Themes": model.Themes, })) } diff --git a/model/config.go b/model/config.go index aa7f106..5164b98 100644 --- a/model/config.go +++ b/model/config.go @@ -10,6 +10,19 @@ import ( "gopkg.in/yaml.v2" ) +var Languages = map[string]string{ + "zh-CN": "简体中文", + "en-US": "English", + "es-ES": "Español", +} + +var Themes = map[string]string{ + "default": "Default", + "daynight": "JackieSung DayNight", + "mdui": "Neko Mdui", + "hotaru": "Hotaru", +} + const ( ConfigTypeGitHub = "github" ConfigTypeGitee = "gitee" diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 5f8cacd..5989423 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -68,14 +68,3 @@ func IPDesensitize(ipAddr string) string { ipAddr = ipv6Desensitize(ipAddr) return ipAddr } - -func PathExists(path string) (bool, error) { - _, err := os.Stat(path) - if err == nil { - return true, nil - } - if os.IsNotExist(err) { - return false, nil - } - return false, err -} diff --git a/resource/l10n/zh-CN.toml b/resource/l10n/zh-CN.toml index 7eb1faa..b3fe1fb 100644 --- a/resource/l10n/zh-CN.toml +++ b/resource/l10n/zh-CN.toml @@ -271,9 +271,6 @@ other = "管理员列表" [Theme] other = "主题" -[DefaultTheme] -other = "默认主题" - [CustomCodes] other = "自定义代码(style、script 都可以)" diff --git a/resource/template/dashboard/setting.html b/resource/template/dashboard/setting.html index f4e232c..f3f2561 100644 --- a/resource/template/dashboard/setting.html +++ b/resource/template/dashboard/setting.html @@ -15,22 +15,18 @@
diff --git a/service/singleton/l10n.go b/service/singleton/l10n.go index b2676a9..0ede9f3 100644 --- a/service/singleton/l10n.go +++ b/service/singleton/l10n.go @@ -4,9 +4,10 @@ import ( "log" "github.com/BurntSushi/toml" - "github.com/naiba/nezha/pkg/utils" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language" + + "github.com/naiba/nezha/model" ) var Localizer *i18n.Localizer @@ -15,13 +16,11 @@ func InitLocalizer() { bundle := i18n.NewBundle(language.Chinese) bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) - userCustomLanguageFile := "resource/l10n/" + Conf.Language + ".toml" - - if exists, err := utils.PathExists(userCustomLanguageFile); !exists { - log.Println("NEZHA>> language file not found:", userCustomLanguageFile, err) + if _, exists := model.Languages[Conf.Language]; !exists { + log.Println("NEZHA>> language not exists:", Conf.Language) Conf.Language = "zh-CN" } else { - _, err := bundle.LoadMessageFile(userCustomLanguageFile) + _, err := bundle.LoadMessageFile("resource/l10n/" + Conf.Language + ".toml") if err != nil { panic(err) }