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:

@client.files project_id, limit: 1, page: 1

Download translation files

Doc

Exports project files as a .zip bundle and makes them available to download (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:
                                        ## Hash with the project id and a "bundle_url" link

For example:

@client.download_files project_id,
                       format: 'yaml',
                       original_filenames: true,
                       filter_langs: ['fr', 'en']

Upload translation file

Doc

Starting from July 2020, background uploading is the only method of importing translation files.

@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:

queued_process = @client.upload_file project_id,
                                     data: 'Base-64 encoded data... ZnI6DQogI...',
                                     filename: 'my_file.yml',
                                     lang_iso: 'en'

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

Your job is to 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

process = @client.upload_file project_id,
                              data: 'Base-64 encoded data... ZnI6DQogI...',
                              filename: 'my_file.yml',
                              lang_iso: 'en'
uploaded? process

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:
                                          ## Hash with project_id and "file_deleted" set to "true"

For example:

@client.destroy_file project_id, file_id