-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder.go
103 lines (88 loc) · 3.1 KB
/
builder.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
package i18n
type translationBuilder struct {
domain string
language string
id string // same as msgctxt in .PO files. Disambiguates same text. Usually blank.
message string
arguments []interface{}
}
// Build returns a new translation builder.
func Build() *translationBuilder {
return new(translationBuilder)
}
// Domain sets the domain of the builder. The Domain indicates what part of the application is responsible
// for translating strings, and allows libraries and the framework to provide their own translations.
func (b *translationBuilder) Domain(domain string) *translationBuilder {
b.domain = domain
return b
}
// Lang sets the canonical value of the builder
func (b *translationBuilder) Lang(lang string) *translationBuilder {
b.language = lang
return b
}
// ID adds a context to disambiguate strings with the same message id but different meanings
func (b *translationBuilder) ID(id string) *translationBuilder {
b.id = id
return b
}
// Comment will add a comment to the extracted translation file, but will otherwise not change the builder
// Use this to add comments directed to the person doing the translation.
func (b *translationBuilder) Comment(comment string) *translationBuilder {
return b
}
// T ends the builder and performs the translation
func (b *translationBuilder) T(s string) string {
return b.t(s)
}
// Sprintf ends the builder and performs the translation using the given format string.
func (b *translationBuilder) Sprintf(s string, params ...interface{}) string {
b.arguments = params
return b.t(s)
}
func (b *translationBuilder) t(s string) string {
if s == "" {
return ""
}
if b.domain == "" {
b.domain = ProjectDomain
}
if b.language == "" {
b.language = langAttributes[0]
}
b.message = s
return translators[b.domain].Translate(b)
}
// The following are modifiers to the T() function in page.ControlBase
type id struct {
id string
}
// ID is a parameter you can add to the page.control.T() function to specify a message id. Usually the message id is the
// same as the string being translated, but when multiple strings are translated that are the same but have different meaning,
// this will be required. This is used as the msgctxt value in PO files, and is combined with the message to make a composite id
// in golang translation files. Adding a comment is helpful in these situations.
func ID(i string) interface{} {
return id{i}
}
type comment struct {
comment string
}
// Comment adds a comment to the translation. It is used in extracted files, but does not impact the translator.
func Comment(c string) interface{} {
return comment{c}
}
// ExtractBuilderFromArguments will return a new builder, but also will extract any builder-specific commands from the
// argmument list, assign those to the builder, and then return what is left of the arguments after the extraction.
func ExtractBuilderFromArguments(args []interface{}) (b *translationBuilder, args2 []interface{}) {
b = Build()
for _, a := range args {
if i, ok := a.(id); ok {
b.ID(i.id)
} else if _, ok := a.(comment); ok {
// do nothing
} else {
args2 = append(args2, a)
}
}
return
}