forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathi18n.go
139 lines (112 loc) · 3.68 KB
/
i18n.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package i18n
import (
"fmt"
"io"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
)
var PLACEHOLDER = regexp.MustCompile(`{(\d)}`)
type Logger interface {
Fprintln(w io.Writer, level string, format string, a ...interface{})
Println(level string, format string, a ...interface{})
}
type LoggerToCustomStreams struct {
Stdout io.Writer
Stderr io.Writer
mux sync.Mutex
}
func (s *LoggerToCustomStreams) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
s.mux.Lock()
defer s.mux.Unlock()
target := s.Stdout
if w == os.Stderr {
target = s.Stderr
}
fmt.Fprintln(target, Format(format, a...))
}
func (s *LoggerToCustomStreams) Println(level string, format string, a ...interface{}) {
s.Fprintln(nil, level, format, a...)
}
type NoopLogger struct{}
func (s *NoopLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {}
func (s *NoopLogger) Println(level string, format string, a ...interface{}) {}
type HumanTagsLogger struct{}
func (s *HumanTagsLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
format = "[" + level + "] " + format
fprintln(w, Format(format, a...))
}
func (s *HumanTagsLogger) Println(level string, format string, a ...interface{}) {
s.Fprintln(os.Stdout, level, format, a...)
}
type HumanLogger struct{}
func (s *HumanLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
fprintln(w, Format(format, a...))
}
func (s *HumanLogger) Println(level string, format string, a ...interface{}) {
s.Fprintln(os.Stdout, level, format, a...)
}
type MachineLogger struct{}
func (s *MachineLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
printMachineFormattedLogLine(w, level, format, a)
}
func (s *MachineLogger) Println(level string, format string, a ...interface{}) {
printMachineFormattedLogLine(os.Stdout, level, format, a)
}
func printMachineFormattedLogLine(w io.Writer, level string, format string, a []interface{}) {
a = append([]interface{}(nil), a...)
for idx, value := range a {
if str, ok := value.(string); ok {
a[idx] = url.QueryEscape(str)
} else if stringer, ok := value.(fmt.Stringer); ok {
a[idx] = url.QueryEscape(stringer.String())
}
}
fprintf(w, "===%s ||| %s ||| %s\n", level, format, a)
}
var lock sync.Mutex
func fprintln(w io.Writer, s string) {
lock.Lock()
defer lock.Unlock()
fmt.Fprintln(w, s)
}
func fprintf(w io.Writer, format string, a ...interface{}) {
lock.Lock()
defer lock.Unlock()
fmt.Fprintf(w, format, a...)
}
func FromJavaToGoSyntax(s string) string {
submatches := PLACEHOLDER.FindAllStringSubmatch(s, -1)
for _, submatch := range submatches {
idx, err := strconv.Atoi(submatch[1])
if err != nil {
panic(err)
}
idx = idx + 1
s = strings.Replace(s, submatch[0], "%["+strconv.Itoa(idx)+"]v", -1)
}
s = strings.Replace(s, "''", "'", -1)
return s
}
func Format(format string, a ...interface{}) string {
format = FromJavaToGoSyntax(format)
message := fmt.Sprintf(format, a...)
return message
}