This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocess.go
91 lines (74 loc) · 1.43 KB
/
process.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"bufio"
"path/filepath"
"strings"
)
var supportedExts = []string{
".gd",
".tscn",
}
type msgID = string
type poFile struct {
Translations map[msgID]message
}
type messageLocation struct {
File string
Line int
}
type message struct {
Files []messageLocation
Msgid msgID
Msgstr string
}
func process(fs fileSystem, filePaths []string) (poFile, error) {
poFile := poFile{
Translations: map[msgID]message{},
}
for _, filePath := range filePaths {
if !isSupportedExt(filepath.Ext(filePath)) {
continue
}
content, err := fs.readFile(filePath)
if err != nil {
return poFile, err
}
scanner := bufio.NewScanner(strings.NewReader(string(content)))
line := 0
for scanner.Scan() {
line++
keys := extract(string(scanner.Text()))
for _, key := range keys {
messageLoc := messageLocation{
File: filepath.Base(filePath),
Line: line,
}
message := message{
Files: []messageLocation{
messageLoc,
},
Msgid: key,
Msgstr: "",
}
if key == "" {
continue
}
if val, ok := poFile.Translations[key]; ok {
val.Files = append(val.Files, messageLoc)
poFile.Translations[key] = val
} else {
poFile.Translations[key] = message
}
}
}
}
return poFile, nil
}
func isSupportedExt(ext string) bool {
for _, supportedExt := range supportedExts {
if ext == supportedExt {
return true
}
}
return false
}