Skip to content

UsePolyLingo/polylingo-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

polylingo (Go)

Go client for the PolyLingo translation API.

Requires Go 1.21+.

Install

go get github.com/UsePolyLingo/polylingo-go/polylingo@v0.1.0

Quick start

package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "time"

    "github.com/UsePolyLingo/polylingo-go/polylingo"
)

func main() {
    client, err := polylingo.New(polylingo.Options{
        APIKey: os.Getenv("POLYLINGO_API_KEY"),
        // BaseURL: "https://api.usepolylingo.com/v1",
        // Timeout: 120 * time.Second,
    })
    if err != nil {
        log.Fatal(err)
    }

    ctx := context.Background()
    r, err := client.Translate(ctx, polylingo.TranslateParams{
        Content: "# Hello",
        Targets: []string{"es", "fr"},
        Format:  "markdown",
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(r.Translations["es"])
}

API

All methods take context.Context as the first argument.

Method HTTP
client.Health(ctx) GET /health
client.Languages(ctx) GET /languages
client.Translate(ctx, params) POST /translate
client.Batch(ctx, params) POST /translate/batch
client.Usage(ctx) GET /usage
client.Jobs.Create(ctx, params) POST /jobs (202)
client.Jobs.Get(ctx, jobID) GET /jobs/:id
client.Jobs.Translate(ctx, params) Submit job, poll until done

Jobs.Translate options

Time values use time.Duration:

  • PollInterval — delay between polls (default: 5s)
  • Timeout — wall-clock budget for polling (default: 20m)
  • OnProgress — optional func(*int); called with queue position while pending / processing when present

Errors

Use errors.As with pointer types:

Type When
*polylingo.PolyLingoError Base (Status, Code, Message)
*polylingo.AuthError HTTP 401
*polylingo.RateLimitError HTTP 429; optional RetryAfter *int (seconds)
*polylingo.JobFailedError Failed job, invalid completed payload, or polling timeout; JobID set when known
import "errors"

_, err := client.Translate(ctx, polylingo.TranslateParams{Content: "x", Targets: []string{"es"}})
if err != nil {
    var ae *polylingo.AuthError
    if errors.As(err, &ae) { /* 401 */ }
    var re *polylingo.RateLimitError
    if errors.As(err, &re) { /* re.RetryAfter */ }
    var je *polylingo.JobFailedError
    if errors.As(err, &je) { /* je.JobID */ }
}

Documentation

Go SDK on usepolylingo.com

Repository

github.com/UsePolyLingo/polylingo-go

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages