feat(init): add support for international language pack initialization (#7315)

pull/7317/head
ssongliu 2024-12-10 18:36:53 +08:00 committed by GitHub
parent f01d770fec
commit 17ef66aa49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 81 additions and 0 deletions

View File

@ -50,6 +50,7 @@ func snapPanel(snap snapHelper, targetDir string) {
if err := common.CopyFile("/usr/local/bin/1pctl", targetDir); err != nil {
status = err.Error()
}
_, _ = cmd.Execf("cp -r /usr/local/bin/lang %s", targetDir)
if err := common.CopyFile("/etc/systemd/system/1panel.service", targetDir); err != nil {
status = err.Error()

View File

@ -104,6 +104,7 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b
updateRecoverStatus(snap.ID, isRecover, "1PctlBinary", constant.StatusFailed, err.Error())
return
}
_, _ = cmd.Execf("cp -r %s %s", path.Join(snapFileDir, "1panel/lang"), "/usr/local/bin/")
global.LOG.Debug("recover 1pctl from snapshot file successful!")
req.IsNew = true
}

View File

@ -143,6 +143,7 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error {
u.handleRollback(originalDir, 2)
return
}
_, _ = cmd.Execf("cp -r %s /usr/local/bin", path.Join(tmpDir, "lang"))
if _, err := cmd.Execf("sed -i -e 's#BASE_DIR=.*#BASE_DIR=%s#g' /usr/local/bin/1pctl", global.CONF.System.BaseDir); err != nil {
global.LOG.Errorf("upgrade basedir in 1pctl failed, err: %v", err)
u.handleRollback(originalDir, 2)
@ -175,6 +176,7 @@ func (u *UpgradeService) handleBackup(fileOp files.FileOp, originalDir string) e
if err := fileOp.Copy("/etc/systemd/system/1panel.service", originalDir); err != nil {
return err
}
_, _ = cmd.Execf("cp -r /usr/local/bin/lang %s", originalDir)
checkPointOfWal()
if err := handleTar(path.Join(global.CONF.System.BaseDir, "1panel/db"), originalDir, "db.tar.gz", "db/1Panel.db-*", ""); err != nil {
return err
@ -205,6 +207,8 @@ func (u *UpgradeService) handleRollback(originalDir string, errStep int) {
if err := common.CopyFile(path.Join(originalDir, "1pctl"), "/usr/local/bin"); err != nil {
global.LOG.Errorf("rollback 1panel failed, err: %v", err)
}
_, _ = cmd.Execf("cp -r %s /usr/local/bin", path.Join(originalDir, "lang"))
if errStep == 2 {
return
}

View File

@ -3,8 +3,10 @@ package hook
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path"
"sort"
"strings"
"github.com/1Panel-dev/1Panel/backend/app/dto"
@ -15,6 +17,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/1Panel-dev/1Panel/backend/utils/xpack"
)
@ -85,6 +88,8 @@ func Init() {
handleSnapStatus()
loadLocalDir()
initDir()
go initLang()
}
func handleSnapStatus() {
@ -260,3 +265,72 @@ func handleCronJobAlert(cronjob *model.Cronjob) {
return
}
}
func initLang() {
fileOp := files.NewFileOp()
upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade")
if fileOp.Stat("/usr/local/bin/lang/zh.sh") {
return
}
tmpPath, err := loadRestorePath(upgradePath)
files, _ := os.ReadDir(path.Join(upgradePath, tmpPath, "downloads"))
if len(files) == 0 {
tmpPath = "no such file"
} else {
for _, item := range files {
if item.IsDir() && strings.HasPrefix(item.Name(), "1panel-") {
tmpPath = path.Join(upgradePath, tmpPath, "downloads", item.Name(), "lang")
break
}
}
}
if err != nil || tmpPath == "no such file" || !fileOp.Stat(tmpPath) {
path := fmt.Sprintf("%s/language/lang.tar.gz", global.CONF.System.RepoUrl)
if err := fileOp.DownloadFileWithProxy(path, "/usr/local/bin/lang.tar.gz"); err != nil {
global.LOG.Errorf("download lang.tar.gz failed, err: %v", err)
return
}
if !fileOp.Stat("/usr/local/bin/lang.tar.gz") {
global.LOG.Errorf("download lang.tar.gz failed, no such file, err: %v", err)
return
}
std, err := cmd.Execf("tar zxvfC %s %s", "/usr/local/bin/lang.tar.gz", "/usr/local/bin/")
if err != nil {
fmt.Printf("decompress lang.tar.gz failed, std: %s, err: %v", std, err)
return
}
_ = os.Remove("/usr/local/bin/lang.tar.gz")
global.LOG.Info("init lang for 1pctl successful")
return
}
std, err := cmd.Execf("cp -r %s %s", tmpPath, "/usr/local/bin/")
if err != nil {
fmt.Printf("load lang from package failed, std: %s, err: %v", std, err)
return
}
global.LOG.Info("init lang for 1pctl successful")
}
func loadRestorePath(upgradeDir string) (string, error) {
if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) {
return "no such file", nil
}
files, err := os.ReadDir(upgradeDir)
if err != nil {
return "", err
}
var folders []string
for _, file := range files {
if file.IsDir() {
folders = append(folders, file.Name())
}
}
if len(folders) == 0 {
return "no such file", nil
}
sort.Slice(folders, func(i, j int) bool {
return folders[i] > folders[j]
})
return folders[0], nil
}

View File

@ -53,6 +53,7 @@ var restoreCmd = &cobra.Command{
if err := common.CopyFile(path.Join(tmpPath, "1pctl"), "/usr/local/bin"); err != nil {
return err
}
_, _ = cmdUtils.Execf("cp -r %s /usr/local/bin", path.Join(tmpPath, "lang"))
fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep3"))
if err := common.CopyFile(path.Join(tmpPath, "1panel.service"), "/etc/systemd/system"); err != nil {
return err