-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
75 lines (60 loc) · 1.88 KB
/
export.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
package main
import (
"fmt"
"os"
"github.com/simpleapps-eu/translate"
"github.com/simpleapps-eu/translate/dotstrings"
)
func export(fuzzy bool, missing bool, srcName string, tmName string, tgtName string) (err error) {
// Open source file
srcFile, err := os.Open(srcName)
if err != nil {
return
}
defer srcFile.Close()
// Load map of translations from the tm file.
translations, err := dotstrings.LoadMessagesMapFromFile(tmName)
if err != nil {
return
}
// Open target file
tgtFile, err := os.Create(tgtName)
if err != nil {
return
}
defer tgtFile.Close()
// Start loading the messages asynchronously from the srcFile
msgChan, errChan := dotstrings.LoadMessages(dotstrings.NewReaderUTF16(srcFile))
// Start translating messages asynchronously
msgChan = translate.TranslateMessages(msgChan, translations)
// Filter out any (non)fuzzy messages asynchronously
msgChan = exportMessages(fuzzy, missing, msgChan)
// Finally write the fuzzy messages to a file synchronously.
n := dotstrings.SaveMessages(msgChan, dotstrings.NewWriterUTF16(tgtFile))
// If there wasn't an error reported during processing, report the status here.
err, _ = <-errChan
if err != nil {
return
}
if fuzzy {
fmt.Printf("%d\tFuzzy strings written to %q\n", n, tgtName)
} else {
fmt.Printf("%d\tNormal strings written to %q\n", n, tgtName)
}
return
}
func exportMessages(fuzzy bool, missing bool, srcChan <-chan dotstrings.Message) <-chan dotstrings.Message {
tgtChan := make(chan dotstrings.Message, 3)
extractor := func(srcChan <-chan dotstrings.Message, tgtChan chan<- dotstrings.Message) {
defer close(tgtChan)
// Process the translated messages, extract fuzzies to file, don't mark them as fuzzy though.
for m := range srcChan {
if fuzzy == m.Fuzzy && (missing || !m.Missing) {
m.Fuzzy = false
tgtChan <- m
}
}
}
go extractor(srcChan, tgtChan)
return tgtChan
}