-
Notifications
You must be signed in to change notification settings - Fork 19
/
svc_translation.go
264 lines (218 loc) · 9.2 KB
/
svc_translation.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package lokalise
import (
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/google/go-querystring/query"
)
const (
pathTranslations = "translations"
)
// TranslationService supports List, Retrieve and Update commands
type TranslationService struct {
BaseService
opts TranslationListOptions
retrieveOpts TranslationRetrieveOptions
}
// ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Service entity objects
// _____________________________________________________________________________________________________________________
type Translation struct {
TranslationID int64 `json:"translation_id"`
Translation string `json:"translation"` // could be string or json in case it includes plural forms and is_plural is true.
KeyID int64 `json:"key_id"`
LanguageISO string `json:"language_iso"`
ModifiedAt string `json:"modified_at"`
ModifiedAtTs int64 `json:"modified_at_timestamp"`
ModifiedBy int64 `json:"modified_by"`
ModifiedByEmail string `json:"modified_by_email"`
IsUnverified bool `json:"is_unverified"`
IsReviewed bool `json:"is_reviewed"`
ReviewedBy int64 `json:"reviewed_by"`
Words int64 `json:"words"`
TaskID int64 `json:"task_id"`
SegmentNumber int64 `json:"segment_number"`
CustomTranslationStatuses []TranslationStatus `json:"custom_translation_statuses"`
}
// ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Service request/response objects
// _____________________________________________________________________________________________________________________
// NewTranslation Used for NewKey
type NewTranslation struct {
LanguageISO string `json:"language_iso"`
Translation string `json:"translation"`
IsFuzzy *bool `json:"is_fuzzy,omitempty"`
IsReviewed bool `json:"is_reviewed,omitempty"`
CustomTranslationStatusIds []int64 `json:"custom_translation_status_ids,omitempty"`
MergeCustomTranslationStatuses bool `json:"merge_custom_translation_statuses,omitempty"`
}
func (t NewTranslation) MarshalJSON() ([]byte, error) {
type Alias NewTranslation
var translation interface{} = t.Translation
if json.Valid([]byte(t.Translation)) {
_ = json.Unmarshal([]byte(t.Translation), &translation)
}
return json.Marshal(&struct {
LanguageISO string `json:"language_iso"`
Translation interface{} `json:"translation"`
Alias
}{
LanguageISO: t.LanguageISO,
Translation: translation,
Alias: (Alias)(t),
})
}
func (t *NewTranslation) UnmarshalJSON(data []byte) error {
type Alias NewTranslation
aux := &struct {
LanguageISO string `json:"language_iso"`
Translation interface{} `json:"translation"`
*Alias
}{
Alias: (*Alias)(t),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
switch aux.Translation.(type) {
case map[string]interface{}:
marshal, err := json.Marshal(aux.Translation)
if err != nil {
return err
}
t.Translation = string(marshal)
case string:
t.Translation = fmt.Sprintf("%+v", aux.Translation)
default:
return fmt.Errorf("NewTranslation tranlation type is of unknown type")
}
t.LanguageISO = aux.LanguageISO
t.IsFuzzy = aux.IsFuzzy
t.IsReviewed = aux.IsReviewed
t.CustomTranslationStatusIds = aux.CustomTranslationStatusIds
t.MergeCustomTranslationStatuses = aux.MergeCustomTranslationStatuses
return nil
}
type UpdateTranslation struct {
Translation string `json:"translation"`
IsUnverified *bool `json:"is_unverified,omitempty"`
IsReviewed bool `json:"is_reviewed,omitempty"`
CustomTranslationStatusIDs []string `json:"custom_translation_status_ids,omitempty"`
}
func (t UpdateTranslation) MarshalJSON() ([]byte, error) {
type Alias UpdateTranslation
var translation interface{} = t.Translation
if json.Valid([]byte(t.Translation)) {
_ = json.Unmarshal([]byte(t.Translation), &translation)
}
return json.Marshal(&struct {
Translation interface{} `json:"translation"`
Alias
}{
Translation: translation,
Alias: (Alias)(t),
})
}
func (t *UpdateTranslation) UnmarshalJSON(data []byte) error {
type Alias UpdateTranslation
aux := &struct {
LanguageISO string `json:"language_iso"`
Translation interface{} `json:"translation"`
*Alias
}{
Alias: (*Alias)(t),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
switch aux.Translation.(type) {
case map[string]interface{}:
marshal, err := json.Marshal(aux.Translation)
if err != nil {
return err
}
t.Translation = string(marshal)
case string:
t.Translation = fmt.Sprintf("%+v", aux.Translation)
default:
return fmt.Errorf("NewTranslation tranlation type is of unknown type")
}
t.IsUnverified = aux.IsUnverified
t.IsReviewed = aux.IsReviewed
t.CustomTranslationStatusIDs = aux.CustomTranslationStatusIDs
return nil
}
type TranslationsResponse struct {
Paged
Translations []Translation `json:"translations"`
}
type TranslationResponse struct {
WithProjectID
Translation Translation `json:"translation"`
}
// ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Service methods
// _____________________________________________________________________________________________________________________
func (c *TranslationService) List(projectID string) (r TranslationsResponse, err error) {
resp, err := c.getWithOptions(c.Ctx(), fmt.Sprintf("%s/%s/%s", pathProjects, projectID, pathTranslations), &r, c.ListOpts())
if err != nil {
return
}
applyPaged(resp, &r.Paged)
return r, apiError(resp)
}
func (c *TranslationService) Retrieve(projectID string, translationID int64) (r TranslationResponse, err error) {
resp, err := c.getWithOptions(c.Ctx(), fmt.Sprintf("%s/%s/%s/%d", pathProjects, projectID, pathTranslations, translationID), &r, c.RetrieveOpts())
if err != nil {
return
}
return r, apiError(resp)
}
func (c *TranslationService) Update(projectID string, translationID int64, opts UpdateTranslation) (r TranslationResponse, err error) {
resp, err := c.put(c.Ctx(), fmt.Sprintf("%s/%s/%s/%d", pathProjects, projectID, pathTranslations, translationID), &r, opts)
if err != nil {
return
}
return r, apiError(resp)
}
// ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Additional methods
// _____________________________________________________________________________________________________________________
type TranslationListOptions struct {
// page options
Pagination string `url:"pagination,omitempty"`
Page uint `url:"page,omitempty"`
Limit uint `url:"limit,omitempty"`
Cursor string `url:"cursor,omitempty"`
// Possible values are 1 and 0.
DisableReferences uint8 `url:"disable_references,omitempty"`
FilterLangID int64 `url:"filter_lang_id,omitempty"`
FilterIsReviewed uint8 `url:"filter_is_reviewed,omitempty"`
FilterUnverified uint8 `url:"filter_unverified,omitempty"`
FilterUntranslated uint8 `url:"filter_untranslated,omitempty"`
FilterQAIssues string `url:"filter_qa_issues,omitempty"`
FilterActiveTaskID int64 `url:"filter_active_task_id,omitempty"`
}
func (options TranslationListOptions) Apply(req *resty.Request) {
v, _ := query.Values(options)
req.SetQueryString(v.Encode())
}
func (c *TranslationService) ListOpts() TranslationListOptions { return c.opts }
func (c *TranslationService) SetListOptions(o TranslationListOptions) { c.opts = o }
func (c *TranslationService) WithListOptions(o TranslationListOptions) *TranslationService {
c.opts = o
return c
}
type TranslationRetrieveOptions struct {
DisableReferences uint8 `url:"disable_references,omitempty"`
}
func (options TranslationRetrieveOptions) Apply(req *resty.Request) {
v, _ := query.Values(options)
req.SetQueryString(v.Encode())
}
func (c *TranslationService) RetrieveOpts() TranslationRetrieveOptions { return c.retrieveOpts }
func (c *TranslationService) SetRetrieveOptions(o TranslationRetrieveOptions) { c.retrieveOpts = o }
func (c *TranslationService) WithRetrieveOptions(o TranslationRetrieveOptions) *TranslationService {
c.retrieveOpts = o
return c
}