Prerequisites

  • Your RoR app must be prepared for localization (see RoR documentation)
  • Project in Lokalise (you will need Project ID from project settings)
  • Read/write API token (generate one under your Personal profile)

Once your app is prepared for localization, it should contain source language (we assume it's English) locale directory. e.g. myapp/config/locales/en.yml.

Using lokalise_rails gem

Before proceeding, add lokalise_rails to your Gemfile:

gem 'lokalise_rails'

Then run:

bundle install
rails g lokalise_rails:install

Next, open config file config/lokalise_rails.rb. Provide API token and project ID inside:

require 'lokalise_rails'

LokaliseRails.config do |c|
c.api_token = ENV['LOKALISE_API_TOKEN']
c.project_id = ENV['LOKALISE_PROJECT_ID']
# ...other options
end

Import and export options have sensible defaults but you may adjust them further as needed.

Uploading to Lokalise

Run the following Rake task:

rails lokalise_rails:export

To perform export programmatically:

require "#{Rails.root}/config/lokalise_rails.rb"

processes = LokaliseRails::TaskDefinition::Exporter.export!

processes is a list of queued background processes.

Downloading from Lokalise

Run the following Rake task:

rails lokalise_rails:import

To perform import programmatically:

require "#{Rails.root}/config/lokalise_rails.rb"

result = LokaliseRails::TaskDefinition::Importer.import!

result contains a boolean value.

Using CLI tool

Install Lokalise CLI tool before proceeding.


Uploading to Lokalise

Upload this file to Lokalise using CLI tool:

$ lokalise2 \
 --token <token> \
 --project-id <project_id> \
 file upload \
 --file "myapp/config/locales/en.yml" \
 --lang-iso en


Downloading from Lokalise

As the translators are done you need to download the language files from Lokalise:

$ lokalise2 \
 --token <token> \
 --project-id <project_id> \
 file download \
 --format yaml \
 --original-filenames=false \
 --bundle-structure "%LANG_ISO%.yml" \
 --unzip-to "myapp/config/locales/"

Did this answer your question?