Skip to content

crowdin/crowdin-api-client-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crowdin Go client

The Crowdin Go client is a lightweight interface to the Crowdin API. It provides common services for making API requests.

Our API is a full-featured RESTful API that helps you to integrate localization into your development process. The endpoints that we use allow you to easily make calls to retrieve information and to execute actions needed.

Installation

go get github.com/crowdin/crowdin-api-client-go

Quick Start

Create a new Crowdin client, then use the exposed services to access different parts of the Crowdin API.
You can generate Personal Access Token in your Crowdin Account Settings.

import "github.com/crowdin/crowdin-api-client-go/crowdin"

client, err := crowdin.NewClient(
    os.Getenv("CROWDIN_ACCESS_TOKEN"),
    crowdin.WithOrganization("organization-name"), // optional for Crowdin Enterprise
)

For example, to create a new project:

package main

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

	"github.com/crowdin/crowdin-api-client-go/crowdin"
	"github.com/crowdin/crowdin-api-client-go/crowdin/model"
)

func main() {
    client, err := crowdin.NewClient(os.Getenv("CROWDIN_ACCESS_TOKEN"))
    if err != nil {
        log.Fatalf("Error creating client: %s", err)
    }

    ctx := context.Background()
    request := &model.ProjectsAddRequest{
        Name: "My Project",
		SourceLanguageID: "en",
		TargetLanguageIDs: []string{"uk", "de"},

    }
    project, _, err := client.Projects.Add(ctx, request)
    if err != nil {
        log.Fatalf("Error creating project: %s", err)
    }

    fmt.Printf("Project: %+v\n", project)
}

Some API methods have optional parameters that can be passed.
For example, to list all projects for a specific user:

package main

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

	"github.com/crowdin/crowdin-api-client-go/crowdin"
	"github.com/crowdin/crowdin-api-client-go/crowdin/model"
)

func main() {
    client, err := crowdin.NewClient(os.Getenv("CROWDIN_ACCESS_TOKEN"))
    if err != nil {
        log.Fatalf("Error creating client: %s", err)
    }

    // list all projects for a specific user
    opts := &model.ProjectsListOptions{UserID: 1}
    projects, _, err := client.Projects.List(context.Background(), opts)
    if err != nil {
        log.Fatalf("Error getting projects: %s", err)
    }

    fmt.Printf("Projects: %+v\n", projects)
}

Error Handling

In case of an error, the client returns an error object. This can either be a generic error with an error message and a code, or a validation error that additionally contains validation error codes.

To detect this condition of error, you can use a type assertion:

res, _, err := client.SourceStrings.Add(ctx, 1, nil)
if err != nil {
    if validationErr, ok := err.(*model.ValidationErrorResponse); ok {
        fmt.Printf("Validation error: %v\n", validationErr)
    } else 
        fmt.Printf("Error: %v\n", err)
    }
}

HTTP Request Timeout

To set a timeout for HTTP requests, you can pass a custom HTTP client with a timeout to the client.
You can also use the context package, you can pass cancellation signals and deadlines to various services of the client to handle a request.

client, err := crowdin.NewClient(
    os.Getenv("CROWDIN_ACCESS_TOKEN"),
    crowdin.WithHTTPClient(&http.Client{Timeout: 10 * time.Second}),
)

Seeking Assistance

If you find any problems or would like to suggest a feature, please read the How can I contribute section in our contributing guidelines.

Contributing

If you want to contribute please read the Contributing guidelines.

License

The Crowdin Go client is licensed under the MIT License.
See the LICENSE file distributed with this work for additional
information regarding copyright ownership.

Except as contained in the LICENSE file, the name(s) of the above copyright
holders shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written authorization.