import "github.com/lokalise/go-lokalise-api/v3" // with go modules enabled (GO111MODULE=on or outside GOPATH)
import "github.com/lokalise/go-lokalise-api" // with go modules disabled
token := os.Getenv("lokalise_token")
client, err := lokalise.New(token)
You can set global API parameters with the ClientOption functions during the initialization. The following functions are available:
- WithBaseURL
- WithRetryCount
- WithRetryTimeout
- WithConnectionTimeout
- WithDebug
- WithPageLimit
Usage:
Api, err := lokalise.New(
"token-string",
lokalise.WithDebug(true),
...
)
Individual objects are represented as instances of according structs. Different objects are used for creating and updating in most cases.
Here are some object types:
-
Create/Update request objects, i.e. NewKey, NewContributor etc
-
Response objects: single/multiple, i.e. KeyResponse/KeysResponse and special , i.e. DeleteKeyResponse. There is no separate ErrorResponse - errors are encapsulated into concrete method's response.
-
List options that are used for sending certain options and pagination, i.e. KeyListOptions.
Some resources, such as Projects, Keys, Files, Tasks, Screenshots, Translations have optional parameters for List method (Keys also have an option for Retrieve). These parameters should be set before calling.
All request options could be set inline and separately:
// separately:
keys := client.Keys()
keys.SetListOptions(lokalise.KeyListOptions{
IncludeTranslations: 1,
IncludeComments: 1,
})
resp, err := keys.List("{PROJECT_ID}")
// inline:
client.Keys().WithListOptions(lokalise.KeyListOptions{Limit: 3}).List("{PROJECT_ID}")
There are two parameters, used for pagination: Limit and Page.
t := Api.Teams()
t.SetPageOptions(lokalise.PageOptions{
Page: 2,
Limit: 10,
})
resp, err := t.List()
Some resource actions, such as Files.upload, are subject to intensive processing before request fulfills. These processes got optimised by becoming asynchronous. The initial request only queues the data for processing and retrieves to queued process identifier. Additional request to QueuedProcesses resource could be executed to obtain the current processing result.
Example with Files.upload:
projectId := "aaaabbbb.cccc"
uploadOpts := lokalise.FileUpload{
Filename: "test.html",
LangISO: "en"
}
f := Api.Files()
resp, err = f.Upload(projectId, uploadOpts)
The successful response will contain process ID, which can be used to obtain the final result:
projectId := "aaaabbbb.cccc"
processId := "ddddeeeeeffff"
q := Api.QueuedProcesses()
resp, err := q.Retrieve(projectId, processId)
projectId := "aaaabbbb.cccc"
c := Api.Comments()
c.SetPageOptions(lokalise.PageOptions{Page: 1, Limit: 20})
resp, err := c.ListProject(projectId)
projectId := "aaaabbbb.cccc"
keyId := 26835183
c := Api.Comments()
c.SetPageOptions(lokalise.PageOptions{Page: 1, Limit: 20})
resp, err := c.ListByKey(projectId, keyId)
c := lokalise.NewComment{Comment: "My new comment"}
resp, err := Api.Comments().Create(projectId, keyId, []lokalise.NewComment{c})
...
commentId := 26835183
resp, err := Api.Comments().Retrieve(projectId, keyId, commentId)
...
resp, err := Api.Comments().Delete(projectId, keyId, commentId)
projectId := "aaaabbbb.cccc"
pageOpts := lokalise.PageOptions{Page: 1, Limit: 20}
c := Api.Contributors()
c.SetPageOptions(pageOpts)
c.List(projectId)
contributorCreate := lokalise.NewContributor{
Email: "[email protected]",
Fullname: "New contributor",
Permission: lokalise.Permission{
IsAdmin: true,
IsReviewer: true,
Languages: []lokalise.Language{{LangISO: "en", IsWritable: true}},
AdminRights: []string{"upload", "download"},
},
}
resp, err := Api.Contributors().Create(projectId, []lokalise.NewContributor{contributorCreate})
userId := 47913
resp, err := Api.Contributors().Retrieve(projectId, userId)
...
permissionUpdate := lokalise.Permission{
IsReviewer: true,
IsAdmin: false,
AdminRights: []string{"keys", "upload", "download"},
}
resp, err := Api.Contributors().Update(projectId, userId, permissionUpdate)