Skip to content

Commit

Permalink
refactor(message/serialize)!: Load 系列函数不再合并相同 Language.ID 的项
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Oct 15, 2024
1 parent 978ee5a commit c32e76d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 34 deletions.
2 changes: 1 addition & 1 deletion message/extract/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

type Options struct {
// Language 提取内容的语言 ID
// Language 提取的内容以此值作为本地化的 ID 保存
Language language.Tag

// 读取的根目录
Expand Down
2 changes: 1 addition & 1 deletion message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type (
// Language 某一语言的本地化内容
Language struct {
XMLName struct{} `xml:"language" json:"-" yaml:"-"`
ID language.Tag `xml:"id,attr" json:"id" yaml:"id"`
ID language.Tag `xml:"id,attr" json:"id" yaml:"id"` // 如果用字符串,还需要处理大小写以及不同值表示同一个 language.Tag 对象的问题
Messages []Message `xml:"message" json:"messages" yaml:"messages"`
}

Expand Down
36 changes: 4 additions & 32 deletions message/serialize/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import (
"io/fs"
"os"
"path/filepath"
"slices"

"github.com/issue9/localeutil"
"github.com/issue9/sliceutil"

"github.com/issue9/localeutil/message"
)

Expand Down Expand Up @@ -48,7 +45,7 @@ func unmarshalFS(f func() ([]byte, error), u UnmarshalFunc) (*message.Language,

// LoadGlob 批量加载文件
//
// 相同语言 ID 的项会合并。
// 相同 Language.ID 的项会合并。
func LoadGlob(s Search, glob string) ([]*message.Language, error) {
matches, err := filepath.Glob(glob)
if err != nil {
Expand All @@ -68,12 +65,12 @@ func LoadGlob(s Search, glob string) ([]*message.Language, error) {
langs = append(langs, l)
}

return joinLanguages(langs), nil
return langs, nil
}

// LoadFSGlob 批量加载文件
//
// 相同语言 ID 的项会合并。
// 相同 Language.ID 的项会合并。
func LoadFSGlob(s Search, glob string, fsys ...fs.FS) ([]*message.Language, error) {
langs := make([]*message.Language, 0, 10)
for _, f := range fsys {
Expand All @@ -96,30 +93,5 @@ func LoadFSGlob(s Search, glob string, fsys ...fs.FS) ([]*message.Language, erro
}
}

return joinLanguages(langs), nil
}

func joinLanguages(langs []*message.Language) []*message.Language {
delIndexes := make([]int, 0, len(langs))
for index, lang := range langs {
// 该元素已经被标记为删除
if slices.IndexFunc(delIndexes, func(v int) bool { return index == v }) >= 0 {
continue
}

// 找与 lang.ID 相同的元素索引
indexes := sliceutil.Indexes(langs, func(l *message.Language, i int) bool {
return l.ID == lang.ID && i != index
})

for _, i := range indexes {
lang.Join(langs[i])
}

delIndexes = append(delIndexes, indexes...)
}

return sliceutil.QuickDelete(langs, func(_ *message.Language, index int) bool {
return slices.IndexFunc(delIndexes, func(i int) bool { return i == index }) >= 0
})
return langs, nil
}

0 comments on commit c32e76d

Please sign in to comment.