-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage.go
188 lines (160 loc) · 4.51 KB
/
language.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
package transifex_api_client
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
)
type Language struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes struct {
Code string `json:"code"`
Name string `json:"name"`
Rtl bool `json:"rtl"`
PluralEquation string `json:"plural_equation"`
PluralRules struct {
One string `json:"one"`
Many string `json:"many"`
Few string `json:"few"`
Other string `json:"other"`
} `json:"plural_rules"`
} `json:"attributes"`
Links struct {
Self string `json:"self"`
} `json:"links"`
}
type LanguageRelationship struct {
Type string `json:"type"`
ID string `json:"id"`
}
type ListLanguagesParameters struct {
Code string
}
// Get information for all the supported languages.
// https://developers.transifex.com/reference/get_languages
func (t *TransifexApiClient) ListLanguages(params ListLanguagesParameters) ([]Language, error) {
paramStr, err := t.createListLanguagesParametersString(params)
if err != nil {
return nil, err
}
// Define the variable to decode the service response
var ls struct {
Data []Language `json:"data"`
Links struct {
Self string `json:"self"`
Next string `json:"next"`
Previous string `json:"previous"`
} `json:"links"`
}
// Create an API request
req, err := http.NewRequest(
"GET",
strings.Join([]string{
t.apiURL,
"/languages",
paramStr,
}, ""),
bytes.NewBuffer(nil))
if err != nil {
t.l.Error(err)
return nil, err
}
// Set authorization and Accept HTTP request headers
req.Header.Set("Authorization", "Bearer "+t.token)
req.Header.Add("Accept", "application/vnd.api+json")
// Perform the request
resp, err := t.client.Do(req)
if err != nil {
t.l.Error(err)
return nil, err
}
// Decode the JSON response into the corresponding variable
err = json.NewDecoder(resp.Body).Decode(&ls)
if err != nil {
t.l.Error(err)
return nil, err
}
return ls.Data, nil
}
// Get information for a specific supported language.
// https://developers.transifex.com/reference/get_languages-language-id
func (t *TransifexApiClient) GetLanguageDetails(language_id string) (Language, error) {
// Define the variable to decode the service response
var ld struct {
Data Language `json:"data"`
}
// Create an API request
req, err := http.NewRequest(
"GET",
strings.Join([]string{
t.apiURL,
"/languages/",
language_id,
}, ""),
bytes.NewBuffer(nil))
if err != nil {
t.l.Error(err)
return Language{}, err
}
// Set authorization and Accept HTTP request headers
req.Header.Set("Authorization", "Bearer "+t.token)
req.Header.Add("Accept", "application/vnd.api+json")
// Perform the request
resp, err := t.client.Do(req)
if err != nil {
t.l.Error(err)
return Language{}, err
}
// Decode the JSON response into the corresponding variable
err = json.NewDecoder(resp.Body).Decode(&ld)
if err != nil {
t.l.Error(err)
return Language{}, err
}
return ld.Data, nil
}
// The function prints the information about a language
func (t *TransifexApiClient) PrintLanguage(l Language, formatter string) {
switch formatter {
case "text":
fmt.Printf("Language information:\n")
fmt.Printf(" ID: %v\n", l.ID)
fmt.Printf(" Type: %v\n", l.Type)
fmt.Printf(" Attributes:\n")
fmt.Printf(" Code: %v\n", l.Attributes.Code)
fmt.Printf(" Name: %v\n", l.Attributes.Name)
fmt.Printf(" Rtl: %v\n", l.Attributes.Rtl)
fmt.Printf(" PluralEquation: %v\n", l.Attributes.PluralEquation)
fmt.Printf(" PluralRules:\n")
fmt.Printf(" One: %v\n", l.Attributes.PluralRules.One)
fmt.Printf(" Many: %v\n", l.Attributes.PluralRules.Many)
fmt.Printf(" Few: %v\n", l.Attributes.PluralRules.Few)
fmt.Printf(" Other: %v\n", l.Attributes.PluralRules.Other)
fmt.Printf(" Links:\n")
fmt.Printf(" Self: %v\n", l.Links.Self)
case "json":
text2print, err := json.Marshal(l)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(text2print))
default:
}
}
// The function checks the input set of parameters and converts it into a valid URL parameters string
func (t *TransifexApiClient) createListLanguagesParametersString(params ListLanguagesParameters) (string, error) {
// Initialize the parameters string
paramStr := ""
// Add optional Code value
if params.Code != "" {
paramStr += "&filter[code][any]=" + params.Code
}
// Replace the & with ? symbol if the string is not empty
if len(paramStr) > 0 {
paramStr = "?" + strings.TrimPrefix(paramStr, "&")
}
return paramStr, nil
}