Skip to content

Commit

Permalink
feat: auto generate settings lang
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 27, 2022
1 parent cc9ccc4 commit fbcf082
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/auto_lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
cd ..
- name: Copy lang file
run: |
cp -f ./alist/drivers.json ./alist-web/src/lang/en/
cp -f ./alist/lang/*.json ./alist-web/src/lang/en/
- name: Commit git
run: |
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public/*.html
public/assets/
public/public/
/data
log/
log/
lang/
30 changes: 29 additions & 1 deletion cmd/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package cmd

import (
"fmt"
"github.com/alist-org/alist/v3/internal/bootstrap/data"
log "github.com/sirupsen/logrus"
"os"
"strings"

_ "github.com/alist-org/alist/v3/drivers"
Expand Down Expand Up @@ -56,15 +59,40 @@ func generateDriversJson() {
}
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
var langCmd = &cobra.Command{
Use: "lang",
Short: "Generate language json file",
Run: func(cmd *cobra.Command, args []string) {
err := os.MkdirAll("lang", 0777)
if err != nil {
log.Fatal("failed create folder: %s", err.Error())
}
generateDriversJson()
generateSettingsJson()
},
}

Expand Down
12 changes: 9 additions & 3 deletions internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var initialSettingItems []model.SettingItem

func initSettings() {
initialSettings()
InitialSettings()
// check deprecated
settings, err := db.GetSettingItems()
if err != nil {
Expand Down Expand Up @@ -58,7 +58,7 @@ func isActive(key string) bool {
return false
}

func initialSettings() {
func InitialSettings() []model.SettingItem {
var token string
if flags.Dev {
token = "dev_token"
Expand Down Expand Up @@ -105,6 +105,7 @@ func initialSettings() {
{Key: conf.VideoAutoplay, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
// global settings
{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.CustomizeHead, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
{Key: conf.CustomizeBody, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
Expand All @@ -119,6 +120,11 @@ func initialSettings() {
{Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
}
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
}

0 comments on commit fbcf082

Please sign in to comment.