feat: auto generate settings lang

This commit is contained in:
Noah Hsu 2022-08-27 18:35:05 +08:00
parent cc9ccc4e9b
commit fbcf082ca7
4 changed files with 41 additions and 6 deletions

View File

@ -39,7 +39,7 @@ jobs:
cd .. cd ..
- name: Copy lang file - name: Copy lang file
run: | run: |
cp -f ./alist/drivers.json ./alist-web/src/lang/en/ cp -f ./alist/lang/*.json ./alist-web/src/lang/en/
- name: Commit git - name: Commit git
run: | run: |

1
.gitignore vendored
View File

@ -29,3 +29,4 @@ public/assets/
public/public/ public/public/
/data /data
log/ log/
lang/

View File

@ -6,6 +6,9 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/alist-org/alist/v3/internal/bootstrap/data"
log "github.com/sirupsen/logrus"
"os"
"strings" "strings"
_ "github.com/alist-org/alist/v3/drivers" _ "github.com/alist-org/alist/v3/drivers"
@ -56,7 +59,27 @@ func generateDriversJson() {
} }
drivers[k] = items drivers[k] = items
} }
utils.WriteJsonToFile("drivers.json", drivers) utils.WriteJsonToFile("lang/drivers.json", drivers)
}
func generateSettingsJson() {
settings := data.InitialSettings()
settingsLang := make(KV[any])
for _, setting := range settings {
settingsLang[setting.Key] = convert(setting.Key)
if setting.Help != "" {
settingsLang[fmt.Sprintf("%s-tips", setting.Key)] = setting.Help
}
if setting.Type == conf.TypeSelect && len(setting.Options) > 0 {
options := make(KV[string])
_options := strings.Split(setting.Options, ",")
for _, o := range _options {
options[o] = convert(o)
}
settingsLang[fmt.Sprintf("%ss", setting.Key)] = options
}
}
utils.WriteJsonToFile("lang/settings.json", settingsLang)
} }
// langCmd represents the lang command // langCmd represents the lang command
@ -64,7 +87,12 @@ var langCmd = &cobra.Command{
Use: "lang", Use: "lang",
Short: "Generate language json file", Short: "Generate language json file",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
err := os.MkdirAll("lang", 0777)
if err != nil {
log.Fatal("failed create folder: %s", err.Error())
}
generateDriversJson() generateDriversJson()
generateSettingsJson()
}, },
} }

View File

@ -14,7 +14,7 @@ import (
var initialSettingItems []model.SettingItem var initialSettingItems []model.SettingItem
func initSettings() { func initSettings() {
initialSettings() InitialSettings()
// check deprecated // check deprecated
settings, err := db.GetSettingItems() settings, err := db.GetSettingItems()
if err != nil { if err != nil {
@ -58,7 +58,7 @@ func isActive(key string) bool {
return false return false
} }
func initialSettings() { func InitialSettings() []model.SettingItem {
var token string var token string
if flags.Dev { if flags.Dev {
token = "dev_token" token = "dev_token"
@ -105,6 +105,7 @@ func initialSettings() {
{Key: conf.VideoAutoplay, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW}, {Key: conf.VideoAutoplay, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
// global settings // global settings
{Key: conf.HideFiles, Value: "/\\/README.md/i", Type: conf.TypeText, Group: model.GLOBAL}, {Key: conf.HideFiles, Value: "/\\/README.md/i", Type: conf.TypeText, Group: model.GLOBAL},
{Key: "package_download", Value: "true", Type: conf.TypeBool, Group: model.GLOBAL},
{Key: conf.GlobalReadme, Value: "This is global readme", Type: conf.TypeText, Group: model.GLOBAL}, {Key: conf.GlobalReadme, Value: "This is global readme", Type: conf.TypeText, Group: model.GLOBAL},
{Key: conf.CustomizeHead, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE}, {Key: conf.CustomizeHead, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
{Key: conf.CustomizeBody, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE}, {Key: conf.CustomizeBody, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
@ -119,6 +120,11 @@ func initialSettings() {
{Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE}, {Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
} }
if flags.Dev { if flags.Dev {
initialSettingItems = append(initialSettingItems, model.SettingItem{Key: "test_deprecated", Value: "test_value", Type: conf.TypeString, Flag: model.DEPRECATED}) initialSettingItems = append(initialSettingItems, []model.SettingItem{
{Key: "test_deprecated", Value: "test_value", Type: conf.TypeString, Flag: model.DEPRECATED},
{Key: "test_options", Value: "a", Type: conf.TypeSelect, Options: "a,b,c"},
{Key: "test_help", Type: conf.TypeString, Help: "this is a help message"},
}...)
} }
return initialSettingItems
} }