We offer a dedicated integration for Golang apps that enables you to easily exchange translation files.
Prerequisites
You'll need an existing Lokalise project. Specifically, you'll require the project ID that can be found in the project settings.
A read/write Lokalise API token is required. Learn how to work with the API tokens in the corresponding article.
Installation
Install the Lokex package:
go get github.com/bodrovis/lokex/v2
It provides a wrapper around the Lokalise API with retry/backoff, async polling, safe unzipping, and strict upload validation.
Client creation and configuration
Find more information in the package's repo.
Lokex also has a cross-platform CLI version: lokex-cli.
Create a new client in the code:
import (
"log"
"time"
"github.com/bodrovis/lokex/v2/client"
)
cli, err := client.NewClient("YOUR_API_TOKEN", "LOKALISE_PROJECT_ID", client.WithBackoff(
1*time.Second, // min backoff
5*time.Second, // max backoff
))
if err != nil {
log.Fatal(err)
}
Download translations
Download and unzip a translation bundle into ./locales:
import (
"github.com/bodrovis/lokex/v2/client/download"
)
downloader := download.NewDownloader(cli)
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Second)
defer cancel()
// call DownloadAsync() for the async download flow
url, err := downloader.Download(ctx, "./locales", download.DownloadParams{
"format": "json",
// other request params...
})
if err != nil {
log.Fatal(err)
}
fmt.Println("Bundle downloaded from:", url)
Upload translations
Upload a JSON file for the English (en) locale:
import (
"github.com/bodrovis/lokex/v2/client/upload"
)
uploader := upload.NewUploader(cli)
fp := filepath.Join(dir, "en.json")
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Second)
defer cancel()
// srcPath (3rd argument) is optional:
// - if srcPath == "" -> uploader reads from params["filename"]
// - if srcPath != "" -> uploader reads file bytes from srcPath,
// but still sends params["filename"] to Lokalise API as the remote filename.
pid, err := uploader.Upload(ctx, upload.UploadParams{
"filename": fp, // sent to Lokalise (remote filename)
"lang_iso": "en",
// other request params...
}, "", true) // srcPath="", poll=true (pass false to skip polling)
if err != nil {
log.Fatal(err)
}
fmt.Println("Upload finished with process ID:", pid)
