支持embed的文件
This commit is contained in:
@@ -1,12 +1,32 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/yuninks/langx"
|
"github.com/yuninks/langx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed lang
|
||||||
|
var assetsFs embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
demoEmbed()
|
||||||
|
}
|
||||||
|
|
||||||
|
func demoEmbed() {
|
||||||
|
err := langx.RegisterEmbed(assetsFs)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
code, msg := langx.GetTransFormat("zh", "success", map[string]string{})
|
||||||
|
fmt.Println(code, msg)
|
||||||
|
code, msg = langx.GetTransFormat("en", "error", map[string]string{
|
||||||
|
"msg": "这是失败的原因",
|
||||||
|
})
|
||||||
|
fmt.Println(code, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func demo1() {
|
||||||
langx.RegisterDir("./lang")
|
langx.RegisterDir("./lang")
|
||||||
|
|
||||||
code, msg := langx.GetTransFormat("zh", "success", map[string]string{})
|
code, msg := langx.GetTransFormat("zh", "success", map[string]string{})
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package langx
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
@@ -83,14 +84,90 @@ func RegisterDir(dir string) error {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// code文件为状态码,其他为对应的语言文件,文件名为语言名
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterEmbed(asset embed.FS) error {
|
||||||
|
paths, err := readEmbedAllPath(asset, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ff := range paths {
|
||||||
|
// 获取文件的后缀
|
||||||
|
|
||||||
|
fileName := strings.Replace(ff.path, ".json", "", 1)
|
||||||
|
if fileName == "code" {
|
||||||
|
data := map[string]int{}
|
||||||
|
err = json.Unmarshal(ff.datas, &data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
RegisterCode(data)
|
||||||
|
} else {
|
||||||
|
data := map[string]string{}
|
||||||
|
err = json.Unmarshal(ff.datas, &data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
RegisterTrans(fileName, data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// code文件为状态码,其他为对应的语言文件,文件名为语言名
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type fileData struct {
|
||||||
|
path string
|
||||||
|
datas []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func readEmbedAllPath(asset embed.FS, path string) ([]fileData, error) {
|
||||||
|
newRoot := ""
|
||||||
|
if path == "" {
|
||||||
|
newRoot = "."
|
||||||
|
} else {
|
||||||
|
newRoot = path
|
||||||
|
}
|
||||||
|
|
||||||
|
fs, err := asset.ReadDir(newRoot)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var files []fileData
|
||||||
|
for _, f := range fs {
|
||||||
|
if f.IsDir() {
|
||||||
|
chaild := ""
|
||||||
|
if newRoot == "." {
|
||||||
|
chaild = f.Name()
|
||||||
|
} else {
|
||||||
|
chaild = newRoot + "/" + f.Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
df, err := readEmbedAllPath(asset, chaild)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
files = append(files, df...)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
by, err := asset.ReadFile(newRoot + "/" + f.Name())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ff := fileData{
|
||||||
|
path: f.Name(),
|
||||||
|
datas: by,
|
||||||
|
}
|
||||||
|
|
||||||
|
files = append(files, ff)
|
||||||
|
}
|
||||||
|
return files, nil
|
||||||
|
}
|
||||||
|
|
||||||
// 获取翻译
|
// 获取翻译
|
||||||
// 包含Code和Message
|
// 包含Code和Message
|
||||||
func GetTransFormat(lang string, key string, format map[string]string) (code int, str string) {
|
func GetTransFormat(lang string, key string, format map[string]string) (code int, str string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user