// Suggest returns a slice of suggestions for a request. // glosbe returns suggestions only for the source locale. func Suggest(req data.SuggestionReq) (*[]data.Suggestion, error) { resp, err := req.FromCache() if err == nil { return resp, nil } source, err := gtranslate.Detect(req) if err != nil { return nil, err } source = req.NormalizeLocale(source) target := req.TargetLocale(source) query, err := makeSuggestQuery(req.Query, source, target) if err != nil { return nil, err } rawData, err := httputils.Get(query) if err != nil { return nil, err } resp, err = parseSuggestResp(rawData, source) if err != nil { return nil, err } if cacheErr := req.SaveCache(resp); cacheErr != nil { log.Println(cacheErr) } return resp, nil }
// Translate translates a text from source locale to target locale. func Translate(req data.TranslationReq) (data.Translation, error) { apiQuery, err := makeTranslateQuery(req) if err != nil { return data.Translation{}, err } rawData, err := httputils.Get(apiQuery) if err != nil { return data.Translation{}, err } translatedText, err := parseTranslateResp(rawData) if err != nil { return data.Translation{}, err } return translation(req, translatedText), nil }
// Detect detects locale of the text. func Detect(req data.SuggestionReq) (data.Locale, error) { apiQuery, err := makeDetectQuery(req) if err != nil { return "", err } data, err := httputils.Get(apiQuery) if err != nil { return "", err } locale, err := parseDetectResp(data) if err != nil { return "", err } return normalizeLocale(locale, req), nil }