chore: refresh gramsrv public release
This commit is contained in:
parent
75cebe8dbf
commit
70b6820474
1274 changed files with 378751 additions and 59919 deletions
|
|
@ -13,7 +13,7 @@ import (
|
|||
|
||||
var tdesktopStringRE = regexp.MustCompile(`(?s)"((?:\\.|[^"\\])*)"\s*=\s*"((?:\\.|[^"\\])*)";`)
|
||||
|
||||
// ParseTDesktopFile 解析 TDesktop .strings 文件为 domain 语言包。
|
||||
// ParseTDesktopFile 解析客户端 .strings 文件为 domain 语言包。
|
||||
func ParseTDesktopFile(path string) (domain.LangPack, error) {
|
||||
pack, err := packFromFilename(path)
|
||||
if err != nil {
|
||||
|
|
@ -21,7 +21,7 @@ func ParseTDesktopFile(path string) (domain.LangPack, error) {
|
|||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return domain.LangPack{}, fmt.Errorf("read tdesktop langpack %q: %w", path, err)
|
||||
return domain.LangPack{}, fmt.Errorf("read langpack %q: %w", path, err)
|
||||
}
|
||||
|
||||
plain := make([]domain.LangPackString, 0)
|
||||
|
|
@ -54,22 +54,37 @@ func ParseTDesktopFile(path string) (domain.LangPack, error) {
|
|||
|
||||
func packFromFilename(path string) (domain.LangPack, error) {
|
||||
name := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
|
||||
const prefix = "tdesktop_"
|
||||
if !strings.HasPrefix(name, prefix) {
|
||||
return domain.LangPack{}, fmt.Errorf("invalid tdesktop langpack filename %q", filepath.Base(path))
|
||||
idx := strings.LastIndex(name, "_v")
|
||||
if idx <= 0 || idx+2 >= len(name) {
|
||||
return domain.LangPack{}, fmt.Errorf("invalid langpack filename %q", filepath.Base(path))
|
||||
}
|
||||
rest := strings.TrimPrefix(name, prefix)
|
||||
idx := strings.LastIndex(rest, "_v")
|
||||
if idx <= 0 || idx+2 >= len(rest) {
|
||||
return domain.LangPack{}, fmt.Errorf("invalid tdesktop langpack filename %q", filepath.Base(path))
|
||||
}
|
||||
version, err := strconv.Atoi(rest[idx+2:])
|
||||
version, err := strconv.Atoi(name[idx+2:])
|
||||
if err != nil {
|
||||
return domain.LangPack{}, fmt.Errorf("parse langpack version %q: %w", rest[idx+2:], err)
|
||||
return domain.LangPack{}, fmt.Errorf("parse langpack version %q: %w", name[idx+2:], err)
|
||||
}
|
||||
|
||||
head := name[:idx]
|
||||
dirPack := filepath.Base(filepath.Dir(path))
|
||||
prefix := dirPack + "_"
|
||||
langPack := ""
|
||||
langCode := ""
|
||||
if dirPack != "." && dirPack != "" && strings.HasPrefix(head, prefix) {
|
||||
langPack = dirPack
|
||||
langCode = strings.TrimPrefix(head, prefix)
|
||||
} else {
|
||||
sep := strings.Index(head, "_")
|
||||
if sep <= 0 || sep+1 >= len(head) {
|
||||
return domain.LangPack{}, fmt.Errorf("invalid langpack filename %q", filepath.Base(path))
|
||||
}
|
||||
langPack = head[:sep]
|
||||
langCode = head[sep+1:]
|
||||
}
|
||||
if langPack == "" || langCode == "" {
|
||||
return domain.LangPack{}, fmt.Errorf("invalid langpack filename %q", filepath.Base(path))
|
||||
}
|
||||
return domain.LangPack{
|
||||
LangPack: "tdesktop",
|
||||
LangCode: rest[:idx],
|
||||
LangPack: langPack,
|
||||
LangCode: langCode,
|
||||
Version: version,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package langpack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"telesrv/internal/store/memory"
|
||||
)
|
||||
|
||||
func TestParseTDesktopFile(t *testing.T) {
|
||||
|
|
@ -35,3 +38,61 @@ func TestParseTDesktopFile(t *testing.T) {
|
|||
t.Fatalf("plural string = %+v", plural)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseClientLangPackFile(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "weba_en_v12000000.strings")
|
||||
if err := os.WriteFile(path, []byte(`
|
||||
"NewMessageTitle" = "New Message";
|
||||
`), 0o600); err != nil {
|
||||
t.Fatalf("write fixture: %v", err)
|
||||
}
|
||||
|
||||
pack, err := ParseTDesktopFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if pack.LangPack != "weba" || pack.LangCode != "en" || pack.Version != 12000000 {
|
||||
t.Fatalf("pack meta = %+v", pack)
|
||||
}
|
||||
if len(pack.Strings) != 1 || pack.Strings[0].Key != "NewMessageTitle" {
|
||||
t.Fatalf("strings = %+v", pack.Strings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedDirectoryWalksClientSubdirs(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
for _, item := range []struct {
|
||||
dir string
|
||||
file string
|
||||
key string
|
||||
}{
|
||||
{dir: "tdesktop", file: "tdesktop_en_v1.strings", key: "lng_language_name"},
|
||||
{dir: "weba", file: "weba_en_v2.strings", key: "NewMessageTitle"},
|
||||
} {
|
||||
dir := filepath.Join(root, item.dir)
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
t.Fatalf("mkdir fixture: %v", err)
|
||||
}
|
||||
content := []byte(`"` + item.key + `" = "value";`)
|
||||
if err := os.WriteFile(filepath.Join(dir, item.file), content, 0o600); err != nil {
|
||||
t.Fatalf("write fixture: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
store := memory.NewLangPackStore()
|
||||
service := NewService(store)
|
||||
seeded, err := service.SeedDirectory(context.Background(), root)
|
||||
if err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
if seeded != 2 {
|
||||
t.Fatalf("seeded = %d, want 2", seeded)
|
||||
}
|
||||
pack, err := service.GetLangPack(context.Background(), "weba", "en")
|
||||
if err != nil {
|
||||
t.Fatalf("get weba pack: %v", err)
|
||||
}
|
||||
if pack.Version != 2 || len(pack.Strings) != 1 || pack.Strings[0].Key != "NewMessageTitle" {
|
||||
t.Fatalf("weba pack = %+v", pack)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,35 +22,34 @@ func (s *Service) SeedDirectory(ctx context.Context, root string) (int, error) {
|
|||
}
|
||||
return 0, fmt.Errorf("stat langpack seed dir: %w", err)
|
||||
}
|
||||
tdesktopDir := filepath.Join(dir, "tdesktop")
|
||||
if info, err := os.Stat(tdesktopDir); err == nil && info.IsDir() {
|
||||
dir = tdesktopDir
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("read langpack seed dir: %w", err)
|
||||
}
|
||||
seeded := 0
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || !strings.EqualFold(filepath.Ext(entry.Name()), ".strings") {
|
||||
continue
|
||||
}
|
||||
pack, err := ParseTDesktopFile(filepath.Join(dir, entry.Name()))
|
||||
err := filepath.WalkDir(dir, func(path string, entry os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return seeded, err
|
||||
return err
|
||||
}
|
||||
if entry.IsDir() || !strings.EqualFold(filepath.Ext(entry.Name()), ".strings") {
|
||||
return nil
|
||||
}
|
||||
pack, err := ParseTDesktopFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
existing, err := s.packs.GetPack(ctx, pack.LangPack, pack.LangCode, pack.Version)
|
||||
if err != nil {
|
||||
return seeded, err
|
||||
return err
|
||||
}
|
||||
if existing.Version >= pack.Version {
|
||||
continue
|
||||
return nil
|
||||
}
|
||||
if err := s.packs.UpsertPack(ctx, pack); err != nil {
|
||||
return seeded, err
|
||||
return err
|
||||
}
|
||||
seeded += len(pack.Strings)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return seeded, fmt.Errorf("walk langpack seed dir: %w", err)
|
||||
}
|
||||
return seeded, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue