0.01
The project is in a healthy, maintained state
This gem allows to exchange translation files between your Rails app and Lokalise TMS.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
 Dependencies

Development

~> 0.2
~> 2.5
~> 6.1.4
~> 13.0
~> 3.6
~> 1.0
~> 0.16
~> 6.0

Runtime

 Project Readme

LokaliseRails

Gem CI Test Coverage Downloads total

This gem provides Lokalise integration for Ruby on Rails and allows to exchange translation files easily. It relies on lokalise_manager which perform the actual import/export and can be used to run this task in any Ruby script.

If you would like to know how this gem was built, check out the "How to create a Ruby gem" series at Lokalise blog.

Getting started

Requirements

This gem requires Ruby 2.5+ and Rails 5.1+. It might work with older versions of Rails though. You will also need to setup a Lokalise account and create a translation project. Finally, you will need to generate a read/write API token at your Lokalise profile.

Alternatively, you can utilize a token obtained via OAuth 2 flow. When using such a token, you'll have to set :use_oauth2_token option to true (please check options docs to learn more).

Installation

Add the gem to your Gemfile:

gem 'lokalise_rails'

and run:

bundle install
rails g lokalise_rails:install

The latter command will generate a new config file config/lokalise_rails.rb looking like this:

LokaliseRails::GlobalConfig.config do |c|
  c.api_token = ENV['LOKALISE_API_TOKEN']
  c.project_id = ENV['LOKALISE_PROJECT_ID']

  # ...other options
end

You have to provide api_token and project_id to proceed. project_id can be found in your Lokalise project settings.

Other options can be customized as well but they have sensible defaults. To learn about all the available options please check lokalise_manager docs. The generated config file also contains some examples to help you get started.

Importing translations from Lokalise

To import translations from the specified Lokalise project to your Rails app, run the following command:

rails lokalise_rails:import

Please note that any duplicating files inside the locales directory (or any other directory that you've specified in the options) will be overwritten! You can enable safe mode to check whether the folder is empty or not.

Exporting translations to Lokalise

To export translations from your Rails app to the specified Lokalise project, run the following command:

rails lokalise_rails:export

Running tasks programmatically

You can also run the import and export tasks programmatically from your code. To achieve that, please check the lokalise_manager gem which lokalise_rails relies on.

For example, you can use the following approach:

# This line is actually not required. Include it if you would like to use your global settings:
require "#{Rails.root}/config/lokalise_rails.rb"

importer = LokaliseManager.importer({api_token: '1234abc', project_id: '123.abc'}, LokaliseRails::GlobalConfig)

# OR

exporter = LokaliseManager.exporter({api_token: '1234abc', project_id: '123.abc'}, LokaliseRails::GlobalConfig)

The first argument passed to importer or exporter is the hash containing per-client options. This is an optional argument and you can simply pass an empty hash if you don't want to override any global settings.

The second argument is the name of your global config which is usually stored inside the lokalise_rails.rb file (however, you can subclass this global config and even adjust the defaults as explained here). If you would like to use LokaliseRails global defaults, then you must pass this class to the clients. If you don't do this, then LokaliseManager defaults will be used instead. The only difference is the location where translation files are stored. For LokaliseManager we use ./locales directory, whereas for LokaliseRails the directory is ./config/locales.

However, you can also provide the translation files folder on per-client basis, for instance:

importer = LokaliseManager.importer api_token: '1234abc', project_id: '123.abc', locales_path: "#{Rails.root}/config/locales"

After the client is instantiated, you can run the corresponding task:

importer.import!

# OR 

exporter.export!

Configuration

Options are specified in the config/lokalise_rails.rb file.

Here's an example:

LokaliseRails::GlobalConfig.config do |c|
  # These are mandatory options that you must set before running rake tasks:
  c.api_token = ENV['LOKALISE_API_TOKEN']
  c.project_id = ENV['LOKALISE_PROJECT_ID']

  # Provide a custom path to the directory with your translation files:
  # c.locales_path = "#{Rails.root}/config/locales"

  # Provide a Lokalise project branch to use:
  c.branch = 'develop'

  # Provide request timeouts for the Lokalise API client:
  c.timeouts = {open_timeout: 5, timeout: 5}

  # Provide maximum number of retries for file exporting:
  c.max_retries_export = 5

  # Provide maximum number of retries for file importing:
  c.max_retries_import = 5

  # Import options have the following defaults:
  # c.import_opts = {
  #   format: 'yaml',
  #   placeholder_format: :icu,
  #   yaml_include_root: true,
  #   original_filenames: true,
  #   directory_prefix: '',
  #   indentation: '2sp'
  # }

  # Safe mode for imports is disabled by default:
  # c.import_safe_mode = false

  # Additional export options (only filename, contents, and lang_iso params are provided by default)
  # c.export_opts = {}

  # Provide additional file exclusion criteria for exports (by default, any file with the proper extension will be exported)
  # c.skip_file_export = ->(file) { file.split[1].to_s.include?('fr') }

  # Set the options below if you would like to work with format other than YAML
  ## Regular expression to use when choosing the files to extract from the downloaded archive and upload to Lokalise
  ## c.file_ext_regexp = /\.ya?ml\z/i

  ## Load translations data and make sure they are valid:
  ## c.translations_loader = ->(raw_data) { YAML.safe_load raw_data }

  ## Convert translations data to a proper format:
  ## c.translations_converter = ->(raw_data) { raw_data.to_yaml }

  ## Infer language ISO code for the translation file:
  ## c.lang_iso_inferer = ->(data) { YAML.safe_load(data)&.keys&.first }
end

Running tests

  1. Copypaste .env.example file as .env. Put your Lokalise API token and project ID inside. The .env file is excluded from version control so your data is safe. All in all, we use pre-recorded VCR cassettes, so the actual API requests won’t be sent. However, providing at least some values is required.
  2. Run rspec .. Observe test results and code coverage.

License

Copyright (c) Lokalise team, Ilya Bodrov. License type is MIT.