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




Translation files

Fetch translation files

Doc

@client.files(project_id, params = {})  # Input:
                                        ## project_id (string, required)
                                        ## params (hash)
                                        ### :page and :limit
                                        # Output:
                                        ## Collection of translation files available in the given project

For example:

project_id = '123.abc'

files = @client.files project_id, limit: 3, page: 2

files[0].filename # => 'demo.json'

Alternatively:

project = @client.project project_id

files = project.files limit: 3, page: 2

Download translation files

Doc

Exports project files as a .zip bundle and makes them available for downloading (the link is valid for 12 months).

@client.download_files(project_id, params)  # Input:
                                        ## project_id (string, required)
                                        ## params (hash, required)
                                        ### :format (string, required) - one of the file formats supported by Lokalise (json, xml, po etc).
                                        ### Find the list of other supported params at https://developers.lokalise.com/reference/download-files
                                        # Output:
                                        ## Generic object with project id and a "bundle_url" link

For example:

params = {
  format: 'yaml',
  original_filenames: true,
  filter_langs: ['fr', 'en']
}

response = @client.download_files project_id, params

response.bundle_url # => 'https://...'

Alternatively:

project = @client.project project_id

response = project.download_files params

If you need a simple way to upload and download translation files in your Ruby scripts, take advantage of the lokalise_manager gem.

Upload translation file

Doc

Please note that file uploading is performed in the background.

@client.upload_file(project_id, params) # Input:
                                        ## project_id (string, required)
                                        ## params (hash, required)
                                        ### :data (string, required) - base64-encoded data (the format must be supported by Lokalise)
                                        ### :filename (string, required)
                                        ### :lang_iso (string, required)
                                        ### Find the list of other supported params at https://developers.lokalise.com/reference/upload-a-file
                                        # Output:
                                        ## QueuedProcess resource

To encode your data in Base64, use Base64.strict_encode64() method.

After the uploading process is completed, a QueuedProcess resource will be returned. This resource contains a status of the import job, process ID to manually check the status, and some other attributes:

params = {
  data: 'Base-64 encoded data... ZnI6DQogI...',
  filename: 'my_file.yml',
  lang_iso: 'en'
}

queued_process = @client.upload_file project_id, params

queued_process.status # => 'queued'
queued_process.process_id # => 'ff1876382b7ba81f2bb465da8f030196ec401fa6'

You can periodically reload data for the queued process and check the status attribute:

reloaded_process = queued_process.reload_data # loads new data from the API
reloaded_process.status # => 'finished'

Alternatively, you may use the queued_process method:

reloaded_process = @client.queued_process project_id, queued_process.process_id

It is up to you to decide how to poll API for changes (remember that larger files will take more time to be imported), but here’s a simple example:

def uploaded?(q_process)
  5.times do # try to check the status 5 times
    q_process = q_process.reload_data # load new data
    return(true) if q_process.status == 'finished' # return true is the upload has finished
    sleep 1 # wait for 1 second, adjust this number with regards to the upload size
  end

  false # if all 5 checks failed, return false (probably something is wrong)
end

params = {
  data: 'Base-64 encoded data... ZnI6DQogI...',
  filename: 'my_file.yml',
  lang_iso: 'en'
}

process = @client.upload_file project_id,

uploaded? process

Alternatively:

project = @client.project project_id

process = project.upload_file params

If you need a simple way to upload and download translation files in your Ruby scripts, take advantage of the lokalise_manager gem.

Delete a file

Doc

This endpoint does not support “software localization” projects.

@client.destroy_file(project_id, file_id) # Input:
                                          ## project_id (string, required)
                                          ## file_id (string or integer, required)
                                          # Output:
                                          ## Generic with project_id and "file_deleted" set to "true"

For example:

response = @client.destroy_file project_id, file_id

response.file_deleted # => true

Alternatively:

project = @client.project project_id

response = project.destroy_file file_id