lokalise_rails 0.0.2.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -5
- data/README.md +79 -27
- data/lib/generators/lokalise_rails/install_generator.rb +2 -2
- data/lib/generators/templates/lokalise_rails_config.rb +36 -25
- data/lib/lokalise_rails.rb +54 -19
- data/lib/lokalise_rails/railtie.rb +2 -5
- data/lib/lokalise_rails/task_definition/base.rb +50 -6
- data/lib/lokalise_rails/task_definition/exporter.rb +80 -0
- data/lib/lokalise_rails/task_definition/importer.rb +57 -15
- data/lib/lokalise_rails/version.rb +2 -2
- data/lib/tasks/lokalise_rails_tasks.rake +14 -0
- data/lokalise_rails.gemspec +1 -2
- data/spec/dummy/config/application.rb +4 -2
- data/spec/dummy/config/lokalise_rails.rb +7 -0
- data/spec/lib/generators/lokalise_rails/install_generator_spec.rb +1 -1
- data/spec/lib/lokalise_rails/task_definition/base_spec.rb +85 -0
- data/spec/lib/lokalise_rails/task_definition/exporter_spec.rb +166 -0
- data/spec/lib/lokalise_rails/task_definition/importer_spec.rb +49 -0
- data/spec/lib/lokalise_rails_spec.rb +44 -7
- data/spec/lib/tasks/export_task_spec.rb +7 -0
- data/spec/lib/tasks/import_task_spec.rb +46 -20
- data/spec/spec_helper.rb +8 -4
- data/spec/support/file_manager.rb +81 -0
- data/spec/support/rake_utils.rb +4 -0
- data/spec/support/spec_addons.rb +8 -0
- metadata +18 -36
- data/spec/dummy/config/initializers/lokalise_rails.rb +0 -29
- data/spec/lib/lokalise_rails/importer_spec.rb +0 -30
- data/spec/support/file_utils.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 951a2d142a56078d2d9509fa36c13f87f7069b8f2defa9bd158a90a67aba2b9a
|
4
|
+
data.tar.gz: ac2360ad608a4251c9061454c0fea986f868facb90567a5bcd94ae01d33bd8e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f41072fa0c0e138fc3f6a838b672c50e0f3af799e151b0cbc2b66aa1792446c6cdde4a1426ed1a5243871c48cc55bc9e12439ecc7eb00be090629ad0a77bd4c
|
7
|
+
data.tar.gz: ae1acd8d95ce227c8c16a362d2fb0d0bc371a986e5108043a7d9f3037bf292e1da2a48efa4c84720e7a4f1f1e66c13e757ba59ababe1ab081d77960516340803
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,25 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
##
|
3
|
+
## 1.1.0 (23-Oct-20)
|
4
4
|
|
5
|
-
*
|
6
|
-
*
|
5
|
+
* New option `branch`
|
6
|
+
* New option `timeouts`
|
7
|
+
* New method `.reset_api_client!` for the task definitions
|
7
8
|
|
8
|
-
##
|
9
|
+
## 1.0.1 (14-Oct-20)
|
9
10
|
|
10
|
-
*
|
11
|
+
* Minor bug fixes and spec updates
|
12
|
+
|
13
|
+
## 1.0.0 (01-Oct-20)
|
14
|
+
|
15
|
+
* Added export feature
|
16
|
+
* More convenient configuration and additional options
|
17
|
+
* Other major enhancements
|
18
|
+
|
19
|
+
## 0.2.0 (26-Sep-20)
|
20
|
+
|
21
|
+
* Allow to override options properly
|
22
|
+
|
23
|
+
## 0.1.0 (26-Sep-20)
|
24
|
+
|
25
|
+
* Initial (proper) release
|
data/README.md
CHANGED
@@ -1,43 +1,50 @@
|
|
1
1
|
# LokaliseRails
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/lokalise_rails)
|
4
4
|
[](https://travis-ci.org/bodrovis/lokalise_rails)
|
5
5
|
[](https://codecov.io/gh/bodrovis/lokalise_rails)
|
6
6
|
|
7
7
|
This gem provides [Lokalise](http://lokalise.com) integration for Ruby on Rails and allows to exchange translation files easily. It relies on [ruby-lokalise-api](https://lokalise.github.io/ruby-lokalise-api) to send APIv2 requests.
|
8
8
|
|
9
|
+
*If you would like to know how this gem was built, check out the ["How to create a Ruby gem" series at Lokalise blog](https://lokalise.com/blog/create-a-ruby-gem-basics/).*
|
10
|
+
|
9
11
|
## Getting started
|
10
12
|
|
11
13
|
### Requirements
|
12
14
|
|
13
|
-
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 a translation project. Finally, you will need to generate a read/write API token at your Lokalise profile.
|
15
|
+
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](https://app.lokalise.com/signup) and create a [translation project](https://docs.lokalise.com/en/articles/1400460-projects). Finally, you will need to generate a [read/write API token](https://docs.lokalise.com/en/articles/1929556-api-tokens) at your Lokalise profile.
|
14
16
|
|
15
17
|
### Installation
|
16
18
|
|
17
|
-
Add the gem to your `Gemfile
|
19
|
+
Add the gem to your `Gemfile`:
|
18
20
|
|
19
21
|
```ruby
|
20
22
|
gem 'lokalise_rails'
|
21
23
|
```
|
22
24
|
|
23
|
-
and run
|
25
|
+
and run:
|
24
26
|
|
25
27
|
```
|
26
28
|
bundle install
|
27
29
|
rails g lokalise_rails:install
|
28
30
|
```
|
29
31
|
|
30
|
-
The latter command will generate a new
|
32
|
+
The latter command will generate a new config file `config/lokalise_rails.rb` looking like this:
|
31
33
|
|
32
34
|
```ruby
|
33
35
|
require 'lokalise_rails'
|
34
36
|
|
35
|
-
LokaliseRails.
|
36
|
-
|
37
|
-
|
37
|
+
LokaliseRails.config do |c|
|
38
|
+
c.api_token = ENV['LOKALISE_API_TOKEN']
|
39
|
+
c.project_id = ENV['LOKALISE_PROJECT_ID']
|
40
|
+
|
41
|
+
# ...other options
|
42
|
+
end
|
38
43
|
```
|
39
44
|
|
40
|
-
You have to provide `api_token` and `project_id` to proceed.
|
45
|
+
You have to provide `api_token` and `project_id` to proceed. `project_id` can be found in your Lokalise project settings.
|
46
|
+
|
47
|
+
[Other options can be customized as well (see below)](https://github.com/bodrovis/lokalise_rails#import-settings) but they have sensible defaults.
|
41
48
|
|
42
49
|
## Importing translations from Lokalise
|
43
50
|
|
@@ -47,31 +54,51 @@ To import translations from the specified Lokalise project to your Rails app, ru
|
|
47
54
|
rails lokalise_rails:import
|
48
55
|
```
|
49
56
|
|
50
|
-
Please note that any
|
57
|
+
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 may enable [safe mode](https://github.com/bodrovis/lokalise_rails#import-settings) to check whether the folder is empty or not.
|
51
58
|
|
52
|
-
##
|
59
|
+
## Exporting translations to Lokalise
|
53
60
|
|
54
|
-
|
61
|
+
To export translations from your Rails app to the specified Lokalise project, run the following command:
|
55
62
|
|
56
|
-
|
63
|
+
```
|
64
|
+
rails lokalise_rails:export
|
65
|
+
```
|
57
66
|
|
58
|
-
|
59
|
-
|
60
|
-
|
67
|
+
## Running tasks programmatically
|
68
|
+
|
69
|
+
You can also run the import and export tasks from the Rails app:
|
61
70
|
|
62
71
|
```ruby
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
72
|
+
require "#{Rails.root}/config/lokalise_rails.rb"
|
73
|
+
|
74
|
+
# Import the files:
|
75
|
+
result = LokaliseRails::TaskDefinition::Importer.import!
|
76
|
+
```
|
77
|
+
`result` contains a boolean value with the result of the operation
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
# Export the files:
|
81
|
+
processes = LokaliseRails::TaskDefinition::Exporter.export!
|
70
82
|
```
|
71
83
|
|
84
|
+
`processes` contains a list of [queued background processes](https://lokalise.github.io/ruby-lokalise-api/api/queued-processes).
|
85
|
+
|
86
|
+
## Configuration
|
87
|
+
|
88
|
+
Options are specified in the `config/lokalise_rails.rb` file.
|
89
|
+
|
90
|
+
### Global settings
|
91
|
+
|
92
|
+
* `api_token` (`string`, required) - Lokalise API token with read/write permissions.
|
93
|
+
* `project_id` (`string`, required) - Lokalise project ID. You must have import/export permissions in the specified project.
|
94
|
+
* `locales_path` (`string`) - path to the directory with your translation files. Defaults to `"#{Rails.root}/config/locales"`.
|
95
|
+
* `file_ext_regexp` (`regexp`) - regular expression applied to file extensions to determine which files should be imported and exported. Defaults to `/\.ya?ml\z/i` (YAML files).
|
96
|
+
* `branch` (`string`) - Lokalise project branch to use. Defaults to `"master"`.
|
97
|
+
* `timeouts` (`hash`) - set [request timeouts for the Lokalise API client](https://lokalise.github.io/ruby-lokalise-api/additional_info/customization#setting-timeouts). By default, requests have no timeouts: `{open_timeout: nil, timeout: nil}`. Both values are in seconds.
|
98
|
+
|
72
99
|
### Import settings
|
73
100
|
|
74
|
-
* `
|
101
|
+
* `import_opts` (`hash`) - options that will be passed to Lokalise API when downloading translations to your app. Here are the default options:
|
75
102
|
|
76
103
|
```ruby
|
77
104
|
{
|
@@ -84,9 +111,34 @@ end
|
|
84
111
|
}
|
85
112
|
```
|
86
113
|
|
87
|
-
Full list of available options [can be found
|
88
|
-
* `
|
114
|
+
Full list of available import options [can be found in the official API documentation](https://app.lokalise.com/api2docs/curl/#transition-download-files-post).
|
115
|
+
* `import_safe_mode` (`boolean`) - default to `false`. When this option is enabled, the import task will check whether the directory set with `locales_path` is empty or not. If it is not empty, you will be prompted to continue.
|
116
|
+
|
117
|
+
### Export settings
|
118
|
+
|
119
|
+
* `export_opts` (`hash`) - options that will be passed to Lokalise API when uploading translations. Full list of available export options [can be found in the official documentation](https://app.lokalise.com/api2docs/curl/#transition-download-files-post). By default, the following options are provided:
|
120
|
+
+ `data` (`string`, required) - base64-encoded contents of the translation file.
|
121
|
+
+ `filename` (`string`, required) - translation file name. If the file is stored under a subdirectory (for example, `nested/en.yml` inside the `locales/` directory), the whole path acts as a name. Later when importing files with such names, they will be placed into the proper subdirectories.
|
122
|
+
+ `lang_iso` (`string`, required) - language ISO code which is determined using the root key inside your YAML file. For example, in this case the `lang_iso` is `en_US`:
|
123
|
+
|
124
|
+
```yaml
|
125
|
+
en_US:
|
126
|
+
my_key: "my value"
|
127
|
+
```
|
128
|
+
|
129
|
+
**Please note** that if your Lokalise project does not have a language with the specified `lang_iso` code, the export will fail.
|
130
|
+
|
131
|
+
* `skip_file_export` (`lambda` or `proc`) - specify additional exclusion criteria for the exported files. By default, the rake task will ignore all non-file entries and all files with improper extensions (the latter is controlled by the `file_ext_regexp`). Lambda passed to this option should accept a single argument which is full path to the file (instance of the [`Pathname` class](https://ruby-doc.org/stdlib-2.7.1/libdoc/pathname/rdoc/Pathname.html)). For example, to exclude all files that have `fr` part in their names, add the following config:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
c.skip_file_export = ->(file) { f.split[1].to_s.include?('fr') }
|
135
|
+
```
|
136
|
+
|
137
|
+
## Running tests
|
138
|
+
|
139
|
+
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.
|
140
|
+
2. Run `rspec .`. Observe test results and code coverage.
|
89
141
|
|
90
142
|
## License
|
91
143
|
|
92
|
-
Copyright (c) [Lokalise team](http://lokalise.com), [Ilya Bodrov](http://bodrovis.tech)
|
144
|
+
Copyright (c) [Lokalise team](http://lokalise.com), [Ilya Bodrov](http://bodrovis.tech). License type is [MIT](https://github.com/bodrovis/lokalise_rails/blob/master/LICENSE).
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'rails/generators'
|
4
4
|
|
5
|
-
|
5
|
+
module LokaliseRails
|
6
6
|
module Generators
|
7
7
|
class InstallGenerator < Rails::Generators::Base
|
8
8
|
source_root File.expand_path('../templates', __dir__)
|
@@ -10,7 +10,7 @@ class LokaliseRails
|
|
10
10
|
desc 'Creates a LokaliseRails config file.'
|
11
11
|
|
12
12
|
def copy_config
|
13
|
-
template 'lokalise_rails_config.rb', "#{Rails.root}/config/
|
13
|
+
template 'lokalise_rails_config.rb', "#{Rails.root}/config/lokalise_rails.rb"
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -2,28 +2,39 @@
|
|
2
2
|
|
3
3
|
require 'lokalise_rails'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
13
|
-
#
|
14
|
-
#
|
15
|
-
|
16
|
-
#
|
17
|
-
# }
|
18
|
-
|
19
|
-
#
|
20
|
-
#
|
21
|
-
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
|
29
|
-
#
|
5
|
+
LokaliseRails.config do |c|
|
6
|
+
# These are mandatory options that you must set before running rake tasks:
|
7
|
+
# c.api_token = ENV['LOKALISE_API_TOKEN']
|
8
|
+
# c.project_id = ENV['LOKALISE_PROJECT_ID']
|
9
|
+
|
10
|
+
# Provide a custom path to the directory with your translation files:
|
11
|
+
# c.locales_path = "#{Rails.root}/config/locales"
|
12
|
+
|
13
|
+
# Provide a Lokalise project branch to use:
|
14
|
+
# c.branch = 'master'
|
15
|
+
|
16
|
+
# Provide request timeouts for the Lokalise API client:
|
17
|
+
# c.timeouts = {open_timeout: nil, timeout: nil}
|
18
|
+
|
19
|
+
# Import options have the following defaults:
|
20
|
+
# c.import_opts = {
|
21
|
+
# format: 'yaml',
|
22
|
+
# placeholder_format: :icu,
|
23
|
+
# yaml_include_root: true,
|
24
|
+
# original_filenames: true,
|
25
|
+
# directory_prefix: '',
|
26
|
+
# indentation: '2sp'
|
27
|
+
# }
|
28
|
+
|
29
|
+
# Safe mode for imports is disabled by default:
|
30
|
+
# c.import_safe_mode = false
|
31
|
+
|
32
|
+
# Additional export options (only filename, contents, and lang_iso params are provided by default)
|
33
|
+
# c.export_opts = {}
|
34
|
+
|
35
|
+
# Provide additional file exclusion criteria for exports (by default, any file with the proper extension will be exported)
|
36
|
+
# c.skip_file_export = ->(file) { file.split[1].to_s.include?('fr') }
|
37
|
+
|
38
|
+
# Regular expression to use when choosing the files to extract from the downloaded archive and upload to Lokalise
|
39
|
+
# c.file_ext_regexp = /\.ya?ml\z/i
|
40
|
+
end
|
data/lib/lokalise_rails.rb
CHANGED
@@ -2,29 +2,64 @@
|
|
2
2
|
|
3
3
|
require 'lokalise_rails/task_definition/base'
|
4
4
|
require 'lokalise_rails/task_definition/importer'
|
5
|
+
require 'lokalise_rails/task_definition/exporter'
|
5
6
|
|
6
|
-
|
7
|
-
@project_id = nil
|
8
|
-
@import_opts = {
|
9
|
-
format: 'yaml',
|
10
|
-
placeholder_format: :icu,
|
11
|
-
yaml_include_root: true,
|
12
|
-
original_filenames: true,
|
13
|
-
directory_prefix: '',
|
14
|
-
indentation: '2sp'
|
15
|
-
}
|
16
|
-
# @export_opts = {
|
17
|
-
#
|
18
|
-
# }
|
19
|
-
@import_safe_mode = false
|
20
|
-
@api_token = nil
|
21
|
-
|
7
|
+
module LokaliseRails
|
22
8
|
class << self
|
23
|
-
attr_accessor :
|
24
|
-
|
9
|
+
attr_accessor :api_token, :project_id
|
10
|
+
attr_writer :import_opts, :import_safe_mode, :export_opts, :locales_path,
|
11
|
+
:file_ext_regexp, :skip_file_export, :branch, :timeouts
|
12
|
+
|
13
|
+
# Main interface to provide configuration options for rake tasks
|
14
|
+
def config
|
15
|
+
yield self
|
16
|
+
end
|
25
17
|
|
18
|
+
# Full path to directory with translation files
|
26
19
|
def locales_path
|
27
|
-
"#{Rails.root}/config/locales"
|
20
|
+
@locales_path || "#{Rails.root}/config/locales"
|
21
|
+
end
|
22
|
+
|
23
|
+
# Project branch to use
|
24
|
+
def branch
|
25
|
+
@branch || 'master'
|
26
|
+
end
|
27
|
+
|
28
|
+
# Set request timeouts for the Lokalise API client
|
29
|
+
def timeouts
|
30
|
+
@timeouts || {}
|
31
|
+
end
|
32
|
+
|
33
|
+
# Regular expression used to select translation files with proper extensions
|
34
|
+
def file_ext_regexp
|
35
|
+
@file_ext_regexp || /\.ya?ml\z/i
|
36
|
+
end
|
37
|
+
|
38
|
+
# Options for import rake task
|
39
|
+
def import_opts
|
40
|
+
@import_opts || {
|
41
|
+
format: 'yaml',
|
42
|
+
placeholder_format: :icu,
|
43
|
+
yaml_include_root: true,
|
44
|
+
original_filenames: true,
|
45
|
+
directory_prefix: '',
|
46
|
+
indentation: '2sp'
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
# Options for export rake task
|
51
|
+
def export_opts
|
52
|
+
@export_opts || {}
|
53
|
+
end
|
54
|
+
|
55
|
+
# Enables safe mode for import. When enabled, will check whether the target folder is empty or not
|
56
|
+
def import_safe_mode
|
57
|
+
@import_safe_mode.nil? ? false : @import_safe_mode
|
58
|
+
end
|
59
|
+
|
60
|
+
# Additional file skip criteria to apply when performing export
|
61
|
+
def skip_file_export
|
62
|
+
@skip_file_export || ->(_) { false }
|
28
63
|
end
|
29
64
|
end
|
30
65
|
end
|
@@ -1,12 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require 'rake'
|
5
|
-
|
6
|
-
class LokaliseRails
|
3
|
+
module LokaliseRails
|
7
4
|
class Railtie < Rails::Railtie
|
8
5
|
rake_tasks do
|
9
|
-
load
|
6
|
+
load 'tasks/lokalise_rails_tasks.rake'
|
10
7
|
end
|
11
8
|
end
|
12
9
|
end
|
@@ -1,17 +1,61 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'ruby-lokalise-api'
|
4
|
-
require '
|
4
|
+
require 'pathname'
|
5
5
|
|
6
|
-
|
6
|
+
module LokaliseRails
|
7
7
|
module TaskDefinition
|
8
8
|
class Base
|
9
9
|
class << self
|
10
|
-
|
11
|
-
return [false, 'Project ID is not set! Aborting...'] unless LokaliseRails.project_id
|
12
|
-
return [false, 'Lokalise API token is not set! Aborting...'] unless LokaliseRails.api_token
|
10
|
+
attr_writer :api_client
|
13
11
|
|
14
|
-
|
12
|
+
# Creates a Lokalise API client
|
13
|
+
#
|
14
|
+
# @return [Lokalise::Client]
|
15
|
+
def api_client
|
16
|
+
@api_client ||= ::Lokalise.client LokaliseRails.api_token, LokaliseRails.timeouts
|
17
|
+
end
|
18
|
+
|
19
|
+
# Resets API client
|
20
|
+
def reset_api_client!
|
21
|
+
Lokalise.reset_client!
|
22
|
+
@api_client = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
# Checks task options
|
26
|
+
#
|
27
|
+
# @return Array
|
28
|
+
def opt_errors
|
29
|
+
errors = []
|
30
|
+
errors << 'Project ID is not set! Aborting...' if LokaliseRails.project_id.nil? || LokaliseRails.project_id.empty?
|
31
|
+
errors << 'Lokalise API token is not set! Aborting...' if LokaliseRails.api_token.nil? || LokaliseRails.api_token.empty?
|
32
|
+
errors
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# Checks whether the provided file has a proper extension as dictated by the `file_ext_regexp` option
|
38
|
+
#
|
39
|
+
# @return Boolean
|
40
|
+
# @param raw_path [String, Pathname]
|
41
|
+
def proper_ext?(raw_path)
|
42
|
+
path = raw_path.is_a?(Pathname) ? raw_path : Pathname.new(raw_path)
|
43
|
+
LokaliseRails.file_ext_regexp.match? path.extname
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns directory and filename for the given entry
|
47
|
+
#
|
48
|
+
# @return Array
|
49
|
+
# @param entry [String]
|
50
|
+
def subdir_and_filename_for(entry)
|
51
|
+
Pathname.new(entry).split
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns Lokalise project ID and branch, semicolumn separated
|
55
|
+
#
|
56
|
+
# @return [String]
|
57
|
+
def project_id_with_branch
|
58
|
+
"#{LokaliseRails.project_id}:#{LokaliseRails.branch}"
|
15
59
|
end
|
16
60
|
end
|
17
61
|
end
|