1// Package translate provides access to the Translate API.
2//
3// See https://developers.google.com/translate/v2/using_rest
4//
5// Usage example:
6//
7//   import "google.golang.org/api/translate/v2"
8//   ...
9//   translateService, err := translate.New(oauthHttpClient)
10package translate // import "google.golang.org/api/translate/v2"
11
12import (
13	"bytes"
14	"encoding/json"
15	"errors"
16	"fmt"
17	context "golang.org/x/net/context"
18	ctxhttp "golang.org/x/net/context/ctxhttp"
19	gensupport "google.golang.org/api/gensupport"
20	googleapi "google.golang.org/api/googleapi"
21	"io"
22	"net/http"
23	"net/url"
24	"strconv"
25	"strings"
26)
27
28// Always reference these packages, just in case the auto-generated code
29// below doesn't.
30var _ = bytes.NewBuffer
31var _ = strconv.Itoa
32var _ = fmt.Sprintf
33var _ = json.NewDecoder
34var _ = io.Copy
35var _ = url.Parse
36var _ = gensupport.MarshalJSON
37var _ = googleapi.Version
38var _ = errors.New
39var _ = strings.Replace
40var _ = context.Canceled
41var _ = ctxhttp.Do
42
43const apiId = "translate:v2"
44const apiName = "translate"
45const apiVersion = "v2"
46const basePath = "https://www.googleapis.com/language/translate/"
47
48func New(client *http.Client) (*Service, error) {
49	if client == nil {
50		return nil, errors.New("client is nil")
51	}
52	s := &Service{client: client, BasePath: basePath}
53	s.Detections = NewDetectionsService(s)
54	s.Languages = NewLanguagesService(s)
55	s.Translations = NewTranslationsService(s)
56	return s, nil
57}
58
59type Service struct {
60	client    *http.Client
61	BasePath  string // API endpoint base URL
62	UserAgent string // optional additional User-Agent fragment
63
64	Detections *DetectionsService
65
66	Languages *LanguagesService
67
68	Translations *TranslationsService
69}
70
71func (s *Service) userAgent() string {
72	if s.UserAgent == "" {
73		return googleapi.UserAgent
74	}
75	return googleapi.UserAgent + " " + s.UserAgent
76}
77
78func NewDetectionsService(s *Service) *DetectionsService {
79	rs := &DetectionsService{s: s}
80	return rs
81}
82
83type DetectionsService struct {
84	s *Service
85}
86
87func NewLanguagesService(s *Service) *LanguagesService {
88	rs := &LanguagesService{s: s}
89	return rs
90}
91
92type LanguagesService struct {
93	s *Service
94}
95
96func NewTranslationsService(s *Service) *TranslationsService {
97	rs := &TranslationsService{s: s}
98	return rs
99}
100
101type TranslationsService struct {
102	s *Service
103}
104
105type DetectionsListResponse struct {
106	// Detections: A detections contains detection results of several text
107	Detections [][]*DetectionsResourceItem `json:"detections,omitempty"`
108
109	// ServerResponse contains the HTTP response code and headers from the
110	// server.
111	googleapi.ServerResponse `json:"-"`
112
113	// ForceSendFields is a list of field names (e.g. "Detections") to
114	// unconditionally include in API requests. By default, fields with
115	// empty values are omitted from API requests. However, any non-pointer,
116	// non-interface field appearing in ForceSendFields will be sent to the
117	// server regardless of whether the field is empty or not. This may be
118	// used to include empty fields in Patch requests.
119	ForceSendFields []string `json:"-"`
120}
121
122func (s *DetectionsListResponse) MarshalJSON() ([]byte, error) {
123	type noMethod DetectionsListResponse
124	raw := noMethod(*s)
125	return gensupport.MarshalJSON(raw, s.ForceSendFields)
126}
127
128type DetectionsResourceItem struct {
129	// Confidence: The confidence of the detection resul of this language.
130	Confidence float64 `json:"confidence,omitempty"`
131
132	// IsReliable: A boolean to indicate is the language detection result
133	// reliable.
134	IsReliable bool `json:"isReliable,omitempty"`
135
136	// Language: The language we detect
137	Language string `json:"language,omitempty"`
138
139	// ForceSendFields is a list of field names (e.g. "Confidence") to
140	// unconditionally include in API requests. By default, fields with
141	// empty values are omitted from API requests. However, any non-pointer,
142	// non-interface field appearing in ForceSendFields will be sent to the
143	// server regardless of whether the field is empty or not. This may be
144	// used to include empty fields in Patch requests.
145	ForceSendFields []string `json:"-"`
146}
147
148func (s *DetectionsResourceItem) MarshalJSON() ([]byte, error) {
149	type noMethod DetectionsResourceItem
150	raw := noMethod(*s)
151	return gensupport.MarshalJSON(raw, s.ForceSendFields)
152}
153
154type LanguagesListResponse struct {
155	// Languages: List of source/target languages supported by the
156	// translation API. If target parameter is unspecified, the list is
157	// sorted by the ASCII code point order of the language code. If target
158	// parameter is specified, the list is sorted by the collation order of
159	// the language name in the target language.
160	Languages []*LanguagesResource `json:"languages,omitempty"`
161
162	// ServerResponse contains the HTTP response code and headers from the
163	// server.
164	googleapi.ServerResponse `json:"-"`
165
166	// ForceSendFields is a list of field names (e.g. "Languages") to
167	// unconditionally include in API requests. By default, fields with
168	// empty values are omitted from API requests. However, any non-pointer,
169	// non-interface field appearing in ForceSendFields will be sent to the
170	// server regardless of whether the field is empty or not. This may be
171	// used to include empty fields in Patch requests.
172	ForceSendFields []string `json:"-"`
173}
174
175func (s *LanguagesListResponse) MarshalJSON() ([]byte, error) {
176	type noMethod LanguagesListResponse
177	raw := noMethod(*s)
178	return gensupport.MarshalJSON(raw, s.ForceSendFields)
179}
180
181type LanguagesResource struct {
182	// Language: The language code.
183	Language string `json:"language,omitempty"`
184
185	// Name: The localized name of the language if target parameter is
186	// given.
187	Name string `json:"name,omitempty"`
188
189	// ForceSendFields is a list of field names (e.g. "Language") to
190	// unconditionally include in API requests. By default, fields with
191	// empty values are omitted from API requests. However, any non-pointer,
192	// non-interface field appearing in ForceSendFields will be sent to the
193	// server regardless of whether the field is empty or not. This may be
194	// used to include empty fields in Patch requests.
195	ForceSendFields []string `json:"-"`
196}
197
198func (s *LanguagesResource) MarshalJSON() ([]byte, error) {
199	type noMethod LanguagesResource
200	raw := noMethod(*s)
201	return gensupport.MarshalJSON(raw, s.ForceSendFields)
202}
203
204type TranslationsListResponse struct {
205	// Translations: Translations contains list of translation results of
206	// given text
207	Translations []*TranslationsResource `json:"translations,omitempty"`
208
209	// ServerResponse contains the HTTP response code and headers from the
210	// server.
211	googleapi.ServerResponse `json:"-"`
212
213	// ForceSendFields is a list of field names (e.g. "Translations") to
214	// unconditionally include in API requests. By default, fields with
215	// empty values are omitted from API requests. However, any non-pointer,
216	// non-interface field appearing in ForceSendFields will be sent to the
217	// server regardless of whether the field is empty or not. This may be
218	// used to include empty fields in Patch requests.
219	ForceSendFields []string `json:"-"`
220}
221
222func (s *TranslationsListResponse) MarshalJSON() ([]byte, error) {
223	type noMethod TranslationsListResponse
224	raw := noMethod(*s)
225	return gensupport.MarshalJSON(raw, s.ForceSendFields)
226}
227
228type TranslationsResource struct {
229	// DetectedSourceLanguage: Detected source language if source parameter
230	// is unspecified.
231	DetectedSourceLanguage string `json:"detectedSourceLanguage,omitempty"`
232
233	// TranslatedText: The translation.
234	TranslatedText string `json:"translatedText,omitempty"`
235
236	// ForceSendFields is a list of field names (e.g.
237	// "DetectedSourceLanguage") to unconditionally include in API requests.
238	// By default, fields with empty values are omitted from API requests.
239	// However, any non-pointer, non-interface field appearing in
240	// ForceSendFields will be sent to the server regardless of whether the
241	// field is empty or not. This may be used to include empty fields in
242	// Patch requests.
243	ForceSendFields []string `json:"-"`
244}
245
246func (s *TranslationsResource) MarshalJSON() ([]byte, error) {
247	type noMethod TranslationsResource
248	raw := noMethod(*s)
249	return gensupport.MarshalJSON(raw, s.ForceSendFields)
250}
251
252// method id "language.detections.list":
253
254type DetectionsListCall struct {
255	s            *Service
256	urlParams_   gensupport.URLParams
257	ifNoneMatch_ string
258	ctx_         context.Context
259}
260
261// List: Detect the language of text.
262func (r *DetectionsService) List(q []string) *DetectionsListCall {
263	c := &DetectionsListCall{s: r.s, urlParams_: make(gensupport.URLParams)}
264	c.urlParams_.SetMulti("q", append([]string{}, q...))
265	return c
266}
267
268// Fields allows partial responses to be retrieved. See
269// https://developers.google.com/gdata/docs/2.0/basics#PartialResponse
270// for more information.
271func (c *DetectionsListCall) Fields(s ...googleapi.Field) *DetectionsListCall {
272	c.urlParams_.Set("fields", googleapi.CombineFields(s))
273	return c
274}
275
276// IfNoneMatch sets the optional parameter which makes the operation
277// fail if the object's ETag matches the given value. This is useful for
278// getting updates only after the object has changed since the last
279// request. Use googleapi.IsNotModified to check whether the response
280// error from Do is the result of In-None-Match.
281func (c *DetectionsListCall) IfNoneMatch(entityTag string) *DetectionsListCall {
282	c.ifNoneMatch_ = entityTag
283	return c
284}
285
286// Context sets the context to be used in this call's Do method. Any
287// pending HTTP request will be aborted if the provided context is
288// canceled.
289func (c *DetectionsListCall) Context(ctx context.Context) *DetectionsListCall {
290	c.ctx_ = ctx
291	return c
292}
293
294func (c *DetectionsListCall) doRequest(alt string) (*http.Response, error) {
295	var body io.Reader = nil
296	c.urlParams_.Set("alt", alt)
297	urls := googleapi.ResolveRelative(c.s.BasePath, "v2/detect")
298	urls += "?" + c.urlParams_.Encode()
299	req, _ := http.NewRequest("GET", urls, body)
300	googleapi.SetOpaque(req.URL)
301	req.Header.Set("User-Agent", c.s.userAgent())
302	if c.ifNoneMatch_ != "" {
303		req.Header.Set("If-None-Match", c.ifNoneMatch_)
304	}
305	if c.ctx_ != nil {
306		return ctxhttp.Do(c.ctx_, c.s.client, req)
307	}
308	return c.s.client.Do(req)
309}
310
311// Do executes the "language.detections.list" call.
312// Exactly one of *DetectionsListResponse or error will be non-nil. Any
313// non-2xx status code is an error. Response headers are in either
314// *DetectionsListResponse.ServerResponse.Header or (if a response was
315// returned at all) in error.(*googleapi.Error).Header. Use
316// googleapi.IsNotModified to check whether the returned error was
317// because http.StatusNotModified was returned.
318func (c *DetectionsListCall) Do(opts ...googleapi.CallOption) (*DetectionsListResponse, error) {
319	gensupport.SetOptions(c.urlParams_, opts...)
320	res, err := c.doRequest("json")
321	if res != nil && res.StatusCode == http.StatusNotModified {
322		if res.Body != nil {
323			res.Body.Close()
324		}
325		return nil, &googleapi.Error{
326			Code:   res.StatusCode,
327			Header: res.Header,
328		}
329	}
330	if err != nil {
331		return nil, err
332	}
333	defer googleapi.CloseBody(res)
334	if err := googleapi.CheckResponse(res); err != nil {
335		return nil, err
336	}
337	ret := &DetectionsListResponse{
338		ServerResponse: googleapi.ServerResponse{
339			Header:         res.Header,
340			HTTPStatusCode: res.StatusCode,
341		},
342	}
343	if err := json.NewDecoder(res.Body).Decode(&ret); err != nil {
344		return nil, err
345	}
346	return ret, nil
347	// {
348	//   "description": "Detect the language of text.",
349	//   "httpMethod": "GET",
350	//   "id": "language.detections.list",
351	//   "parameterOrder": [
352	//     "q"
353	//   ],
354	//   "parameters": {
355	//     "q": {
356	//       "description": "The text to detect",
357	//       "location": "query",
358	//       "repeated": true,
359	//       "required": true,
360	//       "type": "string"
361	//     }
362	//   },
363	//   "path": "v2/detect",
364	//   "response": {
365	//     "$ref": "DetectionsListResponse"
366	//   }
367	// }
368
369}
370
371// method id "language.languages.list":
372
373type LanguagesListCall struct {
374	s            *Service
375	urlParams_   gensupport.URLParams
376	ifNoneMatch_ string
377	ctx_         context.Context
378}
379
380// List: List the source/target languages supported by the API
381func (r *LanguagesService) List() *LanguagesListCall {
382	c := &LanguagesListCall{s: r.s, urlParams_: make(gensupport.URLParams)}
383	return c
384}
385
386// Target sets the optional parameter "target": the language and
387// collation in which the localized results should be returned
388func (c *LanguagesListCall) Target(target string) *LanguagesListCall {
389	c.urlParams_.Set("target", target)
390	return c
391}
392
393// Fields allows partial responses to be retrieved. See
394// https://developers.google.com/gdata/docs/2.0/basics#PartialResponse
395// for more information.
396func (c *LanguagesListCall) Fields(s ...googleapi.Field) *LanguagesListCall {
397	c.urlParams_.Set("fields", googleapi.CombineFields(s))
398	return c
399}
400
401// IfNoneMatch sets the optional parameter which makes the operation
402// fail if the object's ETag matches the given value. This is useful for
403// getting updates only after the object has changed since the last
404// request. Use googleapi.IsNotModified to check whether the response
405// error from Do is the result of In-None-Match.
406func (c *LanguagesListCall) IfNoneMatch(entityTag string) *LanguagesListCall {
407	c.ifNoneMatch_ = entityTag
408	return c
409}
410
411// Context sets the context to be used in this call's Do method. Any
412// pending HTTP request will be aborted if the provided context is
413// canceled.
414func (c *LanguagesListCall) Context(ctx context.Context) *LanguagesListCall {
415	c.ctx_ = ctx
416	return c
417}
418
419func (c *LanguagesListCall) doRequest(alt string) (*http.Response, error) {
420	var body io.Reader = nil
421	c.urlParams_.Set("alt", alt)
422	urls := googleapi.ResolveRelative(c.s.BasePath, "v2/languages")
423	urls += "?" + c.urlParams_.Encode()
424	req, _ := http.NewRequest("GET", urls, body)
425	googleapi.SetOpaque(req.URL)
426	req.Header.Set("User-Agent", c.s.userAgent())
427	if c.ifNoneMatch_ != "" {
428		req.Header.Set("If-None-Match", c.ifNoneMatch_)
429	}
430	if c.ctx_ != nil {
431		return ctxhttp.Do(c.ctx_, c.s.client, req)
432	}
433	return c.s.client.Do(req)
434}
435
436// Do executes the "language.languages.list" call.
437// Exactly one of *LanguagesListResponse or error will be non-nil. Any
438// non-2xx status code is an error. Response headers are in either
439// *LanguagesListResponse.ServerResponse.Header or (if a response was
440// returned at all) in error.(*googleapi.Error).Header. Use
441// googleapi.IsNotModified to check whether the returned error was
442// because http.StatusNotModified was returned.
443func (c *LanguagesListCall) Do(opts ...googleapi.CallOption) (*LanguagesListResponse, error) {
444	gensupport.SetOptions(c.urlParams_, opts...)
445	res, err := c.doRequest("json")
446	if res != nil && res.StatusCode == http.StatusNotModified {
447		if res.Body != nil {
448			res.Body.Close()
449		}
450		return nil, &googleapi.Error{
451			Code:   res.StatusCode,
452			Header: res.Header,
453		}
454	}
455	if err != nil {
456		return nil, err
457	}
458	defer googleapi.CloseBody(res)
459	if err := googleapi.CheckResponse(res); err != nil {
460		return nil, err
461	}
462	ret := &LanguagesListResponse{
463		ServerResponse: googleapi.ServerResponse{
464			Header:         res.Header,
465			HTTPStatusCode: res.StatusCode,
466		},
467	}
468	if err := json.NewDecoder(res.Body).Decode(&ret); err != nil {
469		return nil, err
470	}
471	return ret, nil
472	// {
473	//   "description": "List the source/target languages supported by the API",
474	//   "httpMethod": "GET",
475	//   "id": "language.languages.list",
476	//   "parameters": {
477	//     "target": {
478	//       "description": "the language and collation in which the localized results should be returned",
479	//       "location": "query",
480	//       "type": "string"
481	//     }
482	//   },
483	//   "path": "v2/languages",
484	//   "response": {
485	//     "$ref": "LanguagesListResponse"
486	//   }
487	// }
488
489}
490
491// method id "language.translations.list":
492
493type TranslationsListCall struct {
494	s            *Service
495	urlParams_   gensupport.URLParams
496	ifNoneMatch_ string
497	ctx_         context.Context
498}
499
500// List: Returns text translations from one language to another.
501func (r *TranslationsService) List(q []string, target string) *TranslationsListCall {
502	c := &TranslationsListCall{s: r.s, urlParams_: make(gensupport.URLParams)}
503	c.urlParams_.SetMulti("q", append([]string{}, q...))
504	c.urlParams_.Set("target", target)
505	return c
506}
507
508// Cid sets the optional parameter "cid": The customization id for
509// translate
510func (c *TranslationsListCall) Cid(cid ...string) *TranslationsListCall {
511	c.urlParams_.SetMulti("cid", append([]string{}, cid...))
512	return c
513}
514
515// Format sets the optional parameter "format": The format of the text
516//
517// Possible values:
518//   "html" - Specifies the input is in HTML
519//   "text" - Specifies the input is in plain textual format
520func (c *TranslationsListCall) Format(format string) *TranslationsListCall {
521	c.urlParams_.Set("format", format)
522	return c
523}
524
525// Source sets the optional parameter "source": The source language of
526// the text
527func (c *TranslationsListCall) Source(source string) *TranslationsListCall {
528	c.urlParams_.Set("source", source)
529	return c
530}
531
532// Fields allows partial responses to be retrieved. See
533// https://developers.google.com/gdata/docs/2.0/basics#PartialResponse
534// for more information.
535func (c *TranslationsListCall) Fields(s ...googleapi.Field) *TranslationsListCall {
536	c.urlParams_.Set("fields", googleapi.CombineFields(s))
537	return c
538}
539
540// IfNoneMatch sets the optional parameter which makes the operation
541// fail if the object's ETag matches the given value. This is useful for
542// getting updates only after the object has changed since the last
543// request. Use googleapi.IsNotModified to check whether the response
544// error from Do is the result of In-None-Match.
545func (c *TranslationsListCall) IfNoneMatch(entityTag string) *TranslationsListCall {
546	c.ifNoneMatch_ = entityTag
547	return c
548}
549
550// Context sets the context to be used in this call's Do method. Any
551// pending HTTP request will be aborted if the provided context is
552// canceled.
553func (c *TranslationsListCall) Context(ctx context.Context) *TranslationsListCall {
554	c.ctx_ = ctx
555	return c
556}
557
558func (c *TranslationsListCall) doRequest(alt string) (*http.Response, error) {
559	var body io.Reader = nil
560	c.urlParams_.Set("alt", alt)
561	urls := googleapi.ResolveRelative(c.s.BasePath, "v2")
562	urls += "?" + c.urlParams_.Encode()
563	req, _ := http.NewRequest("GET", urls, body)
564	googleapi.SetOpaque(req.URL)
565	req.Header.Set("User-Agent", c.s.userAgent())
566	if c.ifNoneMatch_ != "" {
567		req.Header.Set("If-None-Match", c.ifNoneMatch_)
568	}
569	if c.ctx_ != nil {
570		return ctxhttp.Do(c.ctx_, c.s.client, req)
571	}
572	return c.s.client.Do(req)
573}
574
575// Do executes the "language.translations.list" call.
576// Exactly one of *TranslationsListResponse or error will be non-nil.
577// Any non-2xx status code is an error. Response headers are in either
578// *TranslationsListResponse.ServerResponse.Header or (if a response was
579// returned at all) in error.(*googleapi.Error).Header. Use
580// googleapi.IsNotModified to check whether the returned error was
581// because http.StatusNotModified was returned.
582func (c *TranslationsListCall) Do(opts ...googleapi.CallOption) (*TranslationsListResponse, error) {
583	gensupport.SetOptions(c.urlParams_, opts...)
584	res, err := c.doRequest("json")
585	if res != nil && res.StatusCode == http.StatusNotModified {
586		if res.Body != nil {
587			res.Body.Close()
588		}
589		return nil, &googleapi.Error{
590			Code:   res.StatusCode,
591			Header: res.Header,
592		}
593	}
594	if err != nil {
595		return nil, err
596	}
597	defer googleapi.CloseBody(res)
598	if err := googleapi.CheckResponse(res); err != nil {
599		return nil, err
600	}
601	ret := &TranslationsListResponse{
602		ServerResponse: googleapi.ServerResponse{
603			Header:         res.Header,
604			HTTPStatusCode: res.StatusCode,
605		},
606	}
607	if err := json.NewDecoder(res.Body).Decode(&ret); err != nil {
608		return nil, err
609	}
610	return ret, nil
611	// {
612	//   "description": "Returns text translations from one language to another.",
613	//   "httpMethod": "GET",
614	//   "id": "language.translations.list",
615	//   "parameterOrder": [
616	//     "q",
617	//     "target"
618	//   ],
619	//   "parameters": {
620	//     "cid": {
621	//       "description": "The customization id for translate",
622	//       "location": "query",
623	//       "repeated": true,
624	//       "type": "string"
625	//     },
626	//     "format": {
627	//       "description": "The format of the text",
628	//       "enum": [
629	//         "html",
630	//         "text"
631	//       ],
632	//       "enumDescriptions": [
633	//         "Specifies the input is in HTML",
634	//         "Specifies the input is in plain textual format"
635	//       ],
636	//       "location": "query",
637	//       "type": "string"
638	//     },
639	//     "q": {
640	//       "description": "The text to translate",
641	//       "location": "query",
642	//       "repeated": true,
643	//       "required": true,
644	//       "type": "string"
645	//     },
646	//     "source": {
647	//       "description": "The source language of the text",
648	//       "location": "query",
649	//       "type": "string"
650	//     },
651	//     "target": {
652	//       "description": "The target language into which the text should be translated",
653	//       "location": "query",
654	//       "required": true,
655	//       "type": "string"
656	//     }
657	//   },
658	//   "path": "v2",
659	//   "response": {
660	//     "$ref": "TranslationsListResponse"
661	//   }
662	// }
663
664}
665