lokalise_rails 1.4.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +53 -0
  3. data/Gemfile +11 -0
  4. data/README.md +74 -54
  5. data/lib/generators/templates/lokalise_rails_config.rb +8 -4
  6. data/lib/lokalise_rails/global_config.rb +11 -0
  7. data/lib/lokalise_rails/utils.rb +20 -0
  8. data/lib/lokalise_rails/version.rb +3 -1
  9. data/lib/lokalise_rails.rb +4 -76
  10. data/lib/tasks/lokalise_rails_tasks.rake +6 -3
  11. data/lokalise_rails.gemspec +4 -16
  12. data/spec/dummy/config/lokalise_rails.rb +3 -3
  13. data/spec/lib/{lokalise_rails_spec.rb → lokalise_rails/global_config_spec.rb} +27 -5
  14. data/spec/lib/lokalise_rails/version_spec.rb +7 -0
  15. data/spec/lib/tasks/export_task_spec.rb +22 -15
  16. data/spec/lib/tasks/import_task_spec.rb +32 -97
  17. data/spec/lib/utils_spec.rb +16 -0
  18. data/spec/spec_helper.rb +0 -1
  19. data/spec/support/file_manager.rb +23 -20
  20. data/spec/support/spec_addons.rb +8 -3
  21. metadata +16 -185
  22. data/lib/lokalise_rails/error.rb +0 -10
  23. data/lib/lokalise_rails/task_definition/base.rb +0 -64
  24. data/lib/lokalise_rails/task_definition/exporter.rb +0 -73
  25. data/lib/lokalise_rails/task_definition/importer.rb +0 -108
  26. data/spec/dummy/config/initializers/application_controller_renderer.rb +0 -9
  27. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -8
  28. data/spec/dummy/config/initializers/content_security_policy.rb +0 -29
  29. data/spec/dummy/config/initializers/inflections.rb +0 -17
  30. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  31. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -16
  32. data/spec/lib/lokalise_rails/task_definition/base_spec.rb +0 -81
  33. data/spec/lib/lokalise_rails/task_definition/exporter_spec.rb +0 -160
  34. data/spec/lib/lokalise_rails/task_definition/importer_spec.rb +0 -54
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6448a3718185d09a6712428f671e02121e59c967199ae6e24f27b8623f12b280
4
- data.tar.gz: c0f2501d9c937b26088ee23a6603a842121cb6a88516ae19d09d40abe1968161
3
+ metadata.gz: eac9b215d2e4002517b8dc09f29841e78cf349178639069e963bd4b0555cce93
4
+ data.tar.gz: 00aacebe6b1f54d80b4d3832c16a38e6863c469ba09cee44fffae9e8373772a6
5
5
  SHA512:
6
- metadata.gz: be178f379eb4abbb4d50c4f3817422c665bed0c8a75f256c083b0229a57594956ca8c42648fe98be40ed1c4183f0e00fd51fcaf153a87ed408279c1d2ac152ec
7
- data.tar.gz: db7dd0efdb0d9a7b72116adba959a04b899e41d5ccd89181f48e2ec2e1d097ebfebb732e6592c725b831cebf89134e77e0639deb8591876267306583064efa96
6
+ metadata.gz: b8647b6b7b1b691a03146fc69934f54fe550a76c547d4add769900095fa409949e982777b210cf977ee8523087f2f2864d5c4d141953d0513c5465b5ec21254b
7
+ data.tar.gz: 5e807d85f5cdf3c693d90ae0bbff1c2e0fcedc2ac5cf76d90ae65bb9e9992583cb72c13db29f05b028f9015668b4ace1e20936e3d67b4003337931b03233d2b3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,58 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.0.0 (27-Jan-22)
4
+
5
+ * File exporting is now multi-threaded as Lokalise API started to support parallel requests since January 2022
6
+ * Test with Ruby 3.1
7
+
8
+ ## 3.0.0 (14-Oct-21)
9
+
10
+ This is a major re-write of this gem. The actual import/export functionality was extracted to a separate gem called [lokalise_manager](https://github.com/bodrovis/lokalise_manager) that you can use to run your tasks programmatically from *any* Ruby scripts (powered or not powered by Rails). LokaliseRails now has only the Rails-related logic (even though it should probably work with other frameworks as well).
11
+
12
+ * **Breaking change:** the global config class is renamed therefore update your `config/lokalise_rails.rb` file to look like this:
13
+
14
+ ```ruby
15
+ LokaliseRails::GlobalConfig.config do |c|
16
+ # ...your configuration options provided as before...
17
+ end
18
+ ```
19
+
20
+ * **Breaking change**: the `branch` config option now has `""` set as default value (it was `"master"` previously). Therefore, you might need to explicitly state which branch to use now:
21
+
22
+ ```ruby
23
+ LokaliseRails::GlobalConfig.config do |c|
24
+ c.branch = "master"
25
+ end
26
+ ```
27
+
28
+ * **Breaking change**: to run your task prommatically, use a new approach:
29
+
30
+ ```ruby
31
+ LokaliseManager.importer(optional_config, global_config_object).import!
32
+
33
+ LokaliseManager.exporter(optional_config, global_config_object).export!
34
+ ```
35
+
36
+ * Please check [this document section](https://github.com/bodrovis/lokalise_rails#running-tasks-programmatically) to learn more about running tasks programmatically. This change doesn't have any effect on you if you're using only Rake tasks to import/export files.
37
+ * No need to say `require 'lokalise_rails` in your `lokalise_rails.rb` config file anymore.
38
+
39
+ ## 2.0.0 (19-Aug-21)
40
+
41
+ * Add exponential backoff mechanism for file imports to comply with the upcoming API changes ("rate limiting"), see below for more details.
42
+ * Add `max_retries_import` option with a default value of `5`.
43
+
44
+ ## 2.0.0.rc1 (12-Aug-21)
45
+
46
+ * **Lokalise is introducing API rate limiting.** Access to all endpoints will be limited to 6 requests per second from 14 September, 2021. This limit is applied per API token and per IP address. If you exceed the limit, a 429 HTTP status code will be returned and a `Lokalise::Error::TooManyRequests` exception will be raised. Therefore, to overcome this issue LokaliseRails is introducing an exponential backoff mechanism for file exports. Why? Because if you have, say, 10 translation files, we'll have to send 10 separate API requests which will probably mean exceeding the limit (this is not the case with importing as we're always receiving a single archive). Thus, if the HTTP status code 429 was received, we'll do the following:
47
+
48
+ ```ruby
49
+ sleep 2 ** retries
50
+ retries += 1
51
+ ```
52
+
53
+ * If the maximum number of retries has been reached LokaliseRails will re-raise the `Lokalise::Error::TooManyRequests` error and give up. By default, the maximum number of retries is set to `5` but you can control it using the `max_retries_export` option.
54
+ * Enabled compression for the API client.
55
+
3
56
  ## 1.4.0 (29-Jun-21)
4
57
 
5
58
  * Re-worked exception handling. Now when something goes wrong during the import or export process, this gem will re-raise all such errors (previously it just printed out some errors to the `$stdout`). If you run a Rake task, it will exit with a status code `1` and the actual error message. If you run a task programattically, you'll get an exception.
data/Gemfile CHANGED
@@ -5,5 +5,16 @@ source 'http://rubygems.org'
5
5
  gemspec
6
6
 
7
7
  group :test do
8
+ gem 'codecov', '~> 0.2'
9
+ gem 'dotenv', '~> 2.5'
10
+ gem 'rails', '~> 7.0.0'
11
+ gem 'rake', '~> 13.0'
12
+ gem 'rspec', '~> 3.6'
13
+ gem 'rubocop', '~> 1.0'
14
+ gem 'rubocop-performance', '~> 1.5'
15
+ gem 'rubocop-rspec', '~> 2.6'
16
+ gem 'simplecov', '~> 0.16'
17
+ gem 'sprockets-rails', '~> 3'
8
18
  gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
19
+ gem 'vcr', '~> 6.0'
9
20
  end
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # LokaliseRails
2
2
 
3
3
  ![Gem](https://img.shields.io/gem/v/lokalise_rails)
4
- [![Build Status](https://travis-ci.com/bodrovis/lokalise_rails.svg?branch=master)](https://travis-ci.com/github/bodrovis/lokalise_rails)
4
+ ![CI](https://github.com/bodrovis/lokalise_rails/actions/workflows/ci.yml/badge.svg)
5
5
  [![Test Coverage](https://codecov.io/gh/bodrovis/lokalise_rails/graph/badge.svg)](https://codecov.io/gh/bodrovis/lokalise_rails)
6
6
  ![Downloads total](https://img.shields.io/gem/dt/lokalise_rails)
7
7
 
8
- 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
+ This gem provides [Lokalise](http://lokalise.com) integration for Ruby on Rails and allows to exchange translation files easily. It relies on [lokalise_manager](https://github.com/bodrovis/lokalise_manager) which perform the actual import/export and can be used to run this task in any Ruby script.
9
9
 
10
10
  *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/).*
11
11
 
@@ -15,6 +15,8 @@ This gem provides [Lokalise](http://lokalise.com) integration for Ruby on Rails
15
15
 
16
16
  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.
17
17
 
18
+ 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](https://github.com/bodrovis/lokalise_manager#common-config) to learn more).
19
+
18
20
  ### Installation
19
21
 
20
22
  Add the gem to your `Gemfile`:
@@ -33,9 +35,7 @@ rails g lokalise_rails:install
33
35
  The latter command will generate a new config file `config/lokalise_rails.rb` looking like this:
34
36
 
35
37
  ```ruby
36
- require 'lokalise_rails'
37
-
38
- LokaliseRails.config do |c|
38
+ LokaliseRails::GlobalConfig.config do |c|
39
39
  c.api_token = ENV['LOKALISE_API_TOKEN']
40
40
  c.project_id = ENV['LOKALISE_PROJECT_ID']
41
41
 
@@ -45,7 +45,7 @@ end
45
45
 
46
46
  You have to provide `api_token` and `project_id` to proceed. `project_id` can be found in your Lokalise project settings.
47
47
 
48
- [Other options can be customized as well (see below)](https://github.com/bodrovis/lokalise_rails#import-settings) but they have sensible defaults.
48
+ Other options can be customized as well but they have sensible defaults. To learn about all the available options please check [lokalise_manager docs](https://github.com/bodrovis/lokalise_manager#configuration). The generated config file also contains some examples to help you get started.
49
49
 
50
50
  ## Importing translations from Lokalise
51
51
 
@@ -55,7 +55,7 @@ To import translations from the specified Lokalise project to your Rails app, ru
55
55
  rails lokalise_rails:import
56
56
  ```
57
57
 
58
- 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.
58
+ 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](https://github.com/bodrovis/lokalise_manager#import-config) to check whether the folder is empty or not.
59
59
 
60
60
  ## Exporting translations to Lokalise
61
61
 
@@ -67,81 +67,101 @@ rails lokalise_rails:export
67
67
 
68
68
  ## Running tasks programmatically
69
69
 
70
- You can also run the import and export tasks from the Rails app:
70
+ You can also run the import and export tasks programmatically from your code. To achieve that, please check the [`lokalise_manager`](https://github.com/bodrovis/lokalise_manager) gem which `lokalise_rails` relies on.
71
+
72
+ For example, you can use the following approach:
71
73
 
72
74
  ```ruby
75
+ # This line is actually not required. Include it if you would like to use your global settings:
73
76
  require "#{Rails.root}/config/lokalise_rails.rb"
74
77
 
75
- # Import the files:
76
- result = LokaliseRails::TaskDefinition::Importer.import!
78
+ importer = LokaliseManager.importer({api_token: '1234abc', project_id: '123.abc'}, LokaliseRails::GlobalConfig)
79
+
80
+ # OR
81
+
82
+ exporter = LokaliseManager.exporter({api_token: '1234abc', project_id: '123.abc'}, LokaliseRails::GlobalConfig)
77
83
  ```
78
- `result` contains a boolean value with the result of the operation
84
+
85
+ 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.
86
+
87
+ 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](https://github.com/bodrovis/lokalise_manager#overriding-defaults)). 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`.
88
+
89
+ However, you can also provide the translation files folder on per-client basis, for instance:
79
90
 
80
91
  ```ruby
81
- # Export the files:
82
- processes = LokaliseRails::TaskDefinition::Exporter.export!
92
+ importer = LokaliseManager.importer api_token: '1234abc', project_id: '123.abc', locales_path: "#{Rails.root}/config/locales"
83
93
  ```
84
94
 
85
- `processes` contains a list of [queued background processes](https://lokalise.github.io/ruby-lokalise-api/api/queued-processes).
95
+ After the client is instantiated, you can run the corresponding task:
96
+
97
+ ```ruby
98
+ importer.import!
99
+
100
+ # OR
101
+
102
+ exporter.export!
103
+ ```
86
104
 
87
105
  ## Configuration
88
106
 
89
107
  Options are specified in the `config/lokalise_rails.rb` file.
90
108
 
91
- ### Global settings
109
+ Here's an example:
92
110
 
93
- * `api_token` (`string`, required) - Lokalise API token with read/write permissions.
94
- * `project_id` (`string`, required) - Lokalise project ID. You must have import/export permissions in the specified project.
95
- * `locales_path` (`string`) - path to the directory with your translation files. Defaults to `"#{Rails.root}/config/locales"`.
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.
111
+ ```ruby
112
+ LokaliseRails::GlobalConfig.config do |c|
113
+ # These are mandatory options that you must set before running rake tasks:
114
+ c.api_token = ENV['LOKALISE_API_TOKEN']
115
+ c.project_id = ENV['LOKALISE_PROJECT_ID']
98
116
 
99
- ### Import settings
117
+ # Provide a custom path to the directory with your translation files:
118
+ # c.locales_path = "#{Rails.root}/config/locales"
100
119
 
101
- * `import_opts` (`hash`) - options that will be passed to Lokalise API when downloading translations to your app. Here are the default options:
120
+ # Provide a Lokalise project branch to use:
121
+ c.branch = 'develop'
102
122
 
103
- ```ruby
104
- {
105
- format: 'yaml',
106
- placeholder_format: :icu,
107
- yaml_include_root: true,
108
- original_filenames: true,
109
- directory_prefix: '',
110
- indentation: '2sp'
111
- }
112
- ```
123
+ # Provide request timeouts for the Lokalise API client:
124
+ c.timeouts = {open_timeout: 5, timeout: 5}
113
125
 
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.
126
+ # Provide maximum number of retries for file exporting:
127
+ c.max_retries_export = 5
116
128
 
117
- ### Export settings
129
+ # Provide maximum number of retries for file importing:
130
+ c.max_retries_import = 5
118
131
 
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`:
132
+ # Import options have the following defaults:
133
+ # c.import_opts = {
134
+ # format: 'yaml',
135
+ # placeholder_format: :icu,
136
+ # yaml_include_root: true,
137
+ # original_filenames: true,
138
+ # directory_prefix: '',
139
+ # indentation: '2sp'
140
+ # }
123
141
 
124
- ```yaml
125
- en_US:
126
- my_key: "my value"
127
- ```
142
+ # Safe mode for imports is disabled by default:
143
+ # c.import_safe_mode = false
128
144
 
129
- **Please note** that if your Lokalise project does not have a language with the specified `lang_iso` code, the export will fail.
145
+ # Additional export options (only filename, contents, and lang_iso params are provided by default)
146
+ # c.export_opts = {}
130
147
 
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:
148
+ # Provide additional file exclusion criteria for exports (by default, any file with the proper extension will be exported)
149
+ # c.skip_file_export = ->(file) { file.split[1].to_s.include?('fr') }
132
150
 
133
- ```ruby
134
- c.skip_file_export = ->(file) { f.split[1].to_s.include?('fr') }
135
- ```
151
+ # Set the options below if you would like to work with format other than YAML
152
+ ## Regular expression to use when choosing the files to extract from the downloaded archive and upload to Lokalise
153
+ ## c.file_ext_regexp = /\.ya?ml\z/i
136
154
 
137
- ### Settings to work with formats other than YAML
155
+ ## Load translations data and make sure they are valid:
156
+ ## c.translations_loader = ->(raw_data) { YAML.safe_load raw_data }
138
157
 
139
- If your translation files are not in YAML format, you will need to adjust the following options:
158
+ ## Convert translations data to a proper format:
159
+ ## c.translations_converter = ->(raw_data) { raw_data.to_yaml }
140
160
 
141
- * `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).
142
- * `translations_loader` (`lambda` or `proc`) - loads translations data and makes sure they are valid before saving them to a translation file. Defaults to `->(raw_data) { YAML.safe_load raw_data }`. In the simplest case you may just return the data back, for example `-> (raw_data) { raw_data }`.
143
- * `translations_converter` (`lambda` or `proc`) - converts translations data to a proper format before saving them to a translation file. Defaults to `->(raw_data) { raw_data.to_yaml }`. In the simplest case you may just return the data back, for example `-> (raw_data) { raw_data }`.
144
- * `lang_iso_inferer` (`lambda` or `proc`) - infers language ISO code based on the translation file data before uploading it to Lokalise. Defaults to `->(data) { YAML.safe_load(data)&.keys&.first }`.
161
+ ## Infer language ISO code for the translation file:
162
+ ## c.lang_iso_inferer = ->(data) { YAML.safe_load(data)&.keys&.first }
163
+ end
164
+ ```
145
165
 
146
166
  ## Running tests
147
167
 
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'lokalise_rails'
4
-
5
- LokaliseRails.config do |c|
3
+ LokaliseRails::GlobalConfig.config do |c|
6
4
  # These are mandatory options that you must set before running rake tasks:
7
5
  # c.api_token = ENV['LOKALISE_API_TOKEN']
8
6
  # c.project_id = ENV['LOKALISE_PROJECT_ID']
@@ -11,11 +9,17 @@ LokaliseRails.config do |c|
11
9
  # c.locales_path = "#{Rails.root}/config/locales"
12
10
 
13
11
  # Provide a Lokalise project branch to use:
14
- # c.branch = 'master'
12
+ # c.branch = ''
15
13
 
16
14
  # Provide request timeouts for the Lokalise API client:
17
15
  # c.timeouts = {open_timeout: nil, timeout: nil}
18
16
 
17
+ # Provide maximum number of retries for file exporting:
18
+ # c.max_retries_export = 5
19
+
20
+ # Provide maximum number of retries for file importing:
21
+ # c.max_retries_import = 5
22
+
19
23
  # Import options have the following defaults:
20
24
  # c.import_opts = {
21
25
  # format: 'yaml',
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LokaliseRails
4
+ class GlobalConfig < LokaliseManager::GlobalConfig
5
+ class << self
6
+ def locales_path
7
+ @locales_path || "#{LokaliseRails::Utils.root}/config/locales"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ module LokaliseRails
6
+ module Utils
7
+ class << self
8
+ def root
9
+ Pathname.new(rails_root || Dir.getwd)
10
+ end
11
+
12
+ def rails_root
13
+ return ::Rails.root.to_s if defined?(::Rails.root) && ::Rails.root
14
+ return RAILS_ROOT.to_s if defined?(RAILS_ROOT)
15
+
16
+ nil
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # :nocov:
3
4
  module LokaliseRails
4
- VERSION = '1.4.0'
5
+ VERSION = '4.0.0'
5
6
  end
7
+ # :nocov:
@@ -1,83 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'lokalise_rails/error'
4
- require 'lokalise_rails/task_definition/base'
5
- require 'lokalise_rails/task_definition/importer'
6
- require 'lokalise_rails/task_definition/exporter'
3
+ require 'lokalise_manager'
7
4
 
8
- module LokaliseRails
9
- class << self
10
- attr_accessor :api_token, :project_id
11
- attr_writer :import_opts, :import_safe_mode, :export_opts, :locales_path,
12
- :file_ext_regexp, :skip_file_export, :branch, :timeouts,
13
- :translations_loader, :translations_converter, :lang_iso_inferer
14
-
15
- # Main interface to provide configuration options for rake tasks
16
- def config
17
- yield self
18
- end
19
-
20
- # Full path to directory with translation files
21
- def locales_path
22
- @locales_path || "#{Rails.root}/config/locales"
23
- end
24
-
25
- # Project branch to use
26
- def branch
27
- @branch || 'master'
28
- end
29
-
30
- # Set request timeouts for the Lokalise API client
31
- def timeouts
32
- @timeouts || {}
33
- end
34
-
35
- # Regular expression used to select translation files with proper extensions
36
- def file_ext_regexp
37
- @file_ext_regexp || /\.ya?ml\z/i
38
- end
39
-
40
- # Options for import rake task
41
- def import_opts
42
- @import_opts || {
43
- format: 'yaml',
44
- placeholder_format: :icu,
45
- yaml_include_root: true,
46
- original_filenames: true,
47
- directory_prefix: '',
48
- indentation: '2sp'
49
- }
50
- end
5
+ require 'lokalise_rails/utils'
6
+ require 'lokalise_rails/global_config'
51
7
 
52
- # Options for export rake task
53
- def export_opts
54
- @export_opts || {}
55
- end
56
-
57
- # Enables safe mode for import. When enabled, will check whether the target folder is empty or not
58
- def import_safe_mode
59
- @import_safe_mode.nil? ? false : @import_safe_mode
60
- end
61
-
62
- # Additional file skip criteria to apply when performing export
63
- def skip_file_export
64
- @skip_file_export || ->(_) { false }
65
- end
66
-
67
- def translations_loader
68
- @translations_loader || ->(raw_data) { YAML.safe_load raw_data }
69
- end
70
-
71
- # Converts translations data to the proper format
72
- def translations_converter
73
- @translations_converter || ->(raw_data) { raw_data.to_yaml }
74
- end
75
-
76
- # Infers lang ISO for the given translation file
77
- def lang_iso_inferer
78
- @lang_iso_inferer || ->(data) { YAML.safe_load(data)&.keys&.first }
79
- end
80
- end
8
+ module LokaliseRails
81
9
  end
82
10
 
83
11
  require 'lokalise_rails/railtie' if defined?(Rails)
@@ -1,17 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rake'
4
- require "#{Rails.root}/config/lokalise_rails"
4
+ require 'lokalise_rails'
5
+ require "#{LokaliseRails::Utils.root}/config/lokalise_rails"
5
6
 
6
7
  namespace :lokalise_rails do
7
8
  task :import do
8
- LokaliseRails::TaskDefinition::Importer.import!
9
+ importer = LokaliseManager.importer({}, LokaliseRails::GlobalConfig)
10
+ importer.import!
9
11
  rescue StandardError => e
10
12
  abort e.inspect
11
13
  end
12
14
 
13
15
  task :export do
14
- LokaliseRails::TaskDefinition::Exporter.export!
16
+ exporter = LokaliseManager.exporter({}, LokaliseRails::GlobalConfig)
17
+ exporter.export!
15
18
  rescue StandardError => e
16
19
  abort e.inspect
17
20
  end
@@ -23,21 +23,9 @@ Gem::Specification.new do |spec|
23
23
  spec.extra_rdoc_files = ['README.md']
24
24
  spec.require_paths = ['lib']
25
25
 
26
- spec.add_dependency 'ruby-lokalise-api', '~> 4.0'
27
- spec.add_dependency 'rubyzip', '~> 2.3'
26
+ spec.add_dependency 'lokalise_manager', '~> 2.0'
28
27
 
29
- spec.add_development_dependency 'codecov', '~> 0.2'
30
- spec.add_development_dependency 'dotenv', '~> 2.5'
31
- if ENV['TEST_RAILS_VERSION'].nil?
32
- spec.add_development_dependency 'rails', '~> 6.1.0'
33
- else
34
- spec.add_development_dependency 'rails', ENV['TEST_RAILS_VERSION'].to_s
35
- end
36
- spec.add_development_dependency 'rake', '~> 13.0'
37
- spec.add_development_dependency 'rspec', '~> 3.6'
38
- spec.add_development_dependency 'rubocop', '~> 1.0'
39
- spec.add_development_dependency 'rubocop-performance', '~> 1.5'
40
- spec.add_development_dependency 'rubocop-rspec', '~> 2.4.0'
41
- spec.add_development_dependency 'simplecov', '~> 0.16'
42
- spec.add_development_dependency 'vcr', '~> 6.0'
28
+ spec.metadata = {
29
+ 'rubygems_mfa_required' => 'true'
30
+ }
43
31
  end
@@ -1,5 +1,5 @@
1
- require 'lokalise_rails'
2
- LokaliseRails.config do |c|
1
+ LokaliseRails::GlobalConfig.config do |c|
3
2
  c.api_token = ENV['LOKALISE_API_TOKEN']
4
3
  c.project_id = ENV['LOKALISE_PROJECT_ID']
5
- end
4
+
5
+ end
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- describe LokaliseRails do
4
- it 'returns a proper version' do
5
- expect(LokaliseRails::VERSION).to be_a(String)
6
- end
3
+ describe LokaliseRails::GlobalConfig do
4
+ let(:child_config) { Class.new(described_class) }
7
5
 
8
6
  it 'is possible to provide config options' do
9
7
  described_class.config do |c|
@@ -11,8 +9,22 @@ describe LokaliseRails do
11
9
  end
12
10
  end
13
11
 
12
+ it 'can be redefined' do
13
+ child_config.config do |c|
14
+ c.api_token = ENV['LOKALISE_API_TOKEN']
15
+ c.project_id = ENV['LOKALISE_PROJECT_ID']
16
+
17
+ c.branch = 'develop'
18
+ end
19
+
20
+ expect(child_config.branch).to eq('develop')
21
+ importer = LokaliseManager::TaskDefinitions::Importer.new({}, child_config)
22
+ expect(importer.config.branch).to eq('develop')
23
+ expect(importer.config.api_token).to eq(ENV['LOKALISE_API_TOKEN'])
24
+ end
25
+
14
26
  describe 'parameters' do
15
- let(:fake_class) { class_double('LokaliseRails') }
27
+ let(:fake_class) { class_double(described_class) }
16
28
 
17
29
  it 'is possible to set project_id' do
18
30
  allow(fake_class).to receive(:project_id=).with('123.abc')
@@ -58,6 +70,16 @@ describe LokaliseRails do
58
70
  fake_class.import_safe_mode = true
59
71
  end
60
72
 
73
+ it 'is possible to set max_retries_export' do
74
+ allow(fake_class).to receive(:max_retries_export=).with(10)
75
+ fake_class.max_retries_export = 10
76
+ end
77
+
78
+ it 'is possible to set max_retries_import' do
79
+ allow(fake_class).to receive(:max_retries_import=).with(10)
80
+ fake_class.max_retries_import = 10
81
+ end
82
+
61
83
  it 'is possible to set api_token' do
62
84
  allow(fake_class).to receive(:api_token=).with('abc')
63
85
  fake_class.api_token = 'abc'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe LokaliseRails do
4
+ it 'returns a proper version' do
5
+ expect(LokaliseRails::VERSION).to be_a(String)
6
+ end
7
+ end
@@ -1,27 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe LokaliseRails do
3
+ RSpec.describe 'Export Rake task' do
4
+ let(:global_config) { LokaliseRails::GlobalConfig }
5
+
4
6
  it 'halts when the API key is not set' do
5
- allow(described_class).to receive(:api_token).and_return(nil)
7
+ allow(global_config).to receive(:api_token).and_return(nil)
6
8
 
7
9
  expect(export_executor).to raise_error(SystemExit, /API token is not set/i)
8
- expect(described_class).to have_received(:api_token)
10
+ expect(global_config).to have_received(:api_token)
9
11
  end
10
12
 
11
13
  it 'halts when the project ID is not set' do
12
- allow_project_id nil do
13
- expect(export_executor).to raise_error(SystemExit, /ID is not set/i)
14
- end
15
- end
14
+ allow(global_config).to receive(:project_id).and_return(nil)
15
+
16
+ expect(export_executor).to raise_error(SystemExit, /ID is not set/i)
16
17
 
17
- it 'runs export rake task properly' do
18
- expect(export_executor).to output(/complete!/).to_stdout
18
+ expect(global_config).to have_received(:project_id)
19
19
  end
20
20
 
21
21
  context 'with two translation files' do
22
22
  let(:filename_ru) { 'ru.yml' }
23
23
  let(:path_ru) { "#{Rails.root}/config/locales/#{filename_ru}" }
24
- let(:relative_name_ru) { filename_ru }
25
24
 
26
25
  before :all do
27
26
  add_translation_files! with_ru: true
@@ -31,12 +30,20 @@ RSpec.describe LokaliseRails do
31
30
  rm_translation_files
32
31
  end
33
32
 
34
- describe '.export!' do
35
- it 're-raises export errors' do
36
- allow_project_id
33
+ describe 'export' do
34
+ it 'is callable' do
35
+ allow_project_id global_config, ENV['LOKALISE_PROJECT_ID'] do
36
+ VCR.use_cassette('upload_files') do
37
+ expect(export_executor).to output(/complete!/).to_stdout
38
+ end
39
+ end
40
+ end
37
41
 
38
- VCR.use_cassette('upload_files_error') do
39
- expect(export_executor).to raise_error(SystemExit, /Unknown `lang_iso`/)
42
+ it 're-raises export errors' do
43
+ allow_project_id global_config, '542886116159f798720dc4.94769464' do
44
+ VCR.use_cassette('upload_files_error') do
45
+ expect(export_executor).to raise_error(SystemExit, /Unknown `lang_iso`/)
46
+ end
40
47
  end
41
48
  end
42
49
  end