Go client for the PolyLingo translation API.
Requires Go 1.21+.
go get github.com/UsePolyLingo/polylingo-go/polylingo@v0.1.0package 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"])
}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 |
Time values use time.Duration:
PollInterval— delay between polls (default:5s)Timeout— wall-clock budget for polling (default:20m)OnProgress— optionalfunc(*int); called with queue position whilepending/processingwhen present
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 */ }
}github.com/UsePolyLingo/polylingo-go
MIT