Lokalise APIv2 Ruby SDK

Ruby interface for the Lokalise APIv2 that represents returned data as Ruby objects.

View the Project on GitHub lokalise/ruby-lokalise-api




Getting Started

Installation and Requirements

This gem requires Ruby 2.4+ and RubyGems package manager.

Install it by running:

gem install ruby-lokalise-api

Initializing the Client

In order to perform API requests, you require a special token that can be obtained in your personal profile (API tokens section).

After you’ve obtained the token, initialize the client:

require 'ruby_lokalise_api'

@client = RubyLokaliseApi.client 'YOUR_TOKEN_HERE'

Now the @client can be used to perform API requests! Learn more about additional options in the Customizing request section.

Objects and models

Individual objects are represented as instances of Ruby classes which are called models. Each model responds to the methods that are named after the API object’s attributes. This file lists all objects and their methods.

Here is an example:

project = client.project '123'
project.name
project.description
project.created_by

Many resources have common methods like project_id and branch:

webhook = client.webhook project_id, '123.abc'
webhook.project_id
webhook.branch

To get access to raw data returned by the API, use #raw_data:

project.raw_data

Models support method chaining, meaning you can fetch a resource, update and delete it in one line:

@client.project('123').update(name: 'New name').destroy

Reloading data

Most of the resources can be reloaded using the #reload_data method. This method will fetch the latest data for the resource:

project = client.project '123'
# do something else...
# project might be updated via UI, so load new data:
reloaded_project = project.reload_data
# now `reloaded_project` has fresh data from the API

Collections of resources and pagination

Fetching (or creating/updating) multiple objects will return a collection of objects. To get access to the actual data, use the #collection method:

project = @client.projects.collection.first # => Get the first project
project.name

Bulk fetches support pagination. There are two common parameters available:

projects = @client.projects limit: 10, page: 3 #=> Paginate by 10 records and fetch the third page

Collections respond to the following methods:

For example:

projects.current_page #=> 3
projects.last_page? #=> true, this is the last page and there are no more projects available

On top of that, you may easily fetch the next or the previous page of the collection by using:

These methods return instances of the same collection class or nil if the next/previous page is unavailable. Methods respect the parameters you’ve initially passed:

translations = @client.translations 'project_id', limit: 4, page: 2, disable_references: 0 # => we passed three parameters here

translations.prev_page # => will load the previous page while preserving the `limit` and `disable_references` params

Branching

If you are using project branching feature, simply add branch name separated by semicolon to your project ID in any endpoint to access the branch. For example, in order to access new-feature branch for the project with an id 123abcdef.01:

@client.files '123abcdef.01:new-feature'