lokalise_rails 2.0.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +43 -0
  3. data/Gemfile +12 -1
  4. data/README.md +74 -57
  5. data/lib/generators/lokalise_rails/install_generator.rb +2 -0
  6. data/lib/generators/templates/lokalise_rails_config.rb +2 -4
  7. data/lib/lokalise_rails/global_config.rb +12 -0
  8. data/lib/lokalise_rails/railtie.rb +1 -0
  9. data/lib/lokalise_rails/utils.rb +23 -0
  10. data/lib/lokalise_rails/version.rb +3 -1
  11. data/lib/lokalise_rails.rb +8 -88
  12. data/lib/tasks/lokalise_rails_tasks.rake +6 -3
  13. data/lokalise_rails.gemspec +7 -18
  14. data/spec/dummy/config/lokalise_rails.rb +3 -3
  15. data/spec/lib/generators/lokalise_rails/install_generator_spec.rb +2 -2
  16. data/spec/lib/{lokalise_rails_spec.rb → lokalise_rails/global_config_spec.rb} +17 -5
  17. data/spec/lib/lokalise_rails/version_spec.rb +7 -0
  18. data/spec/lib/tasks/export_task_spec.rb +25 -18
  19. data/spec/lib/tasks/import_task_spec.rb +36 -99
  20. data/spec/lib/utils_spec.rb +16 -0
  21. data/spec/spec_helper.rb +0 -2
  22. data/spec/support/file_manager.rb +8 -11
  23. data/spec/support/spec_addons.rb +8 -3
  24. metadata +21 -178
  25. data/lib/lokalise_rails/error.rb +0 -10
  26. data/lib/lokalise_rails/task_definition/base.rb +0 -80
  27. data/lib/lokalise_rails/task_definition/exporter.rb +0 -79
  28. data/lib/lokalise_rails/task_definition/importer.rb +0 -109
  29. data/spec/dummy/config/initializers/application_controller_renderer.rb +0 -9
  30. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -8
  31. data/spec/dummy/config/initializers/content_security_policy.rb +0 -29
  32. data/spec/dummy/config/initializers/inflections.rb +0 -17
  33. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  34. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -16
  35. data/spec/lib/lokalise_rails/task_definition/base_spec.rb +0 -81
  36. data/spec/lib/lokalise_rails/task_definition/exporter_spec.rb +0 -200
  37. data/spec/lib/lokalise_rails/task_definition/importer_spec.rb +0 -72
  38. data/spec/support/rake_utils.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ac898ae13038db9d3426f367e3cba3b54c41e35797dfb56dd5da5595959e728
4
- data.tar.gz: 2bf9f24929b226b8994b3f4648f67b5da59aa0b68687cb7f597f531ac2d7bb99
3
+ metadata.gz: e0766634c77c771d4a7750da1a98c0d6c3a8f7f0ecf06ae071c4c4becc34c288
4
+ data.tar.gz: cdb4a114a582b30465e82fad8eefb5d89be6bbae8943f83255a9fd3c683c6b3c
5
5
  SHA512:
6
- metadata.gz: 93be5d1f8379908fbf6e3ac7a1f44534373779d8aa3e375f456ef7a83454a07babe3971aa364afe374759e54571d036c60c68e16f14c81397128770573058a9a
7
- data.tar.gz: 84c42e122da897f7103a16d11e9a5e066cee28c2b6413c44ec3e459868dfe89d8c5005f3fa644024bea7dfd7c49233de5fecc67746016485f467febe91b3fa52
6
+ metadata.gz: 11cc6c1dabc982278a58bc89e71c883501029badf2af8a627595e46cef2c4ed541734d872e00fa0c83bcaa7fce5b9e344ab06f1564c79a4bae78a740bd307347
7
+ data.tar.gz: 88c8ef5767a31660dd317cc828b088874cd5075eeea4afa7c9f58b1eebaa1e7a8a1917392e89e3da679558faca8346061e1aaad0e981e004f5f5497a41472e8b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,48 @@
1
1
  # Changelog
2
2
 
3
+ ## 5.0.0 (11-Mar-2022)
4
+
5
+ * **Breaking change**: require Ruby 2.7+
6
+ * Use Zeitwerk loader
7
+ * Use newer LokaliseManager (v3) and RubyLokaliseApi (v6)
8
+ * Prettify code
9
+
10
+ ## 4.0.0 (27-Jan-22)
11
+
12
+ * File exporting is now multi-threaded as Lokalise API started to support parallel requests since January 2022
13
+ * Test with Ruby 3.1
14
+
15
+ ## 3.0.0 (14-Oct-21)
16
+
17
+ 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).
18
+
19
+ * **Breaking change:** the global config class is renamed therefore update your `config/lokalise_rails.rb` file to look like this:
20
+
21
+ ```ruby
22
+ LokaliseRails::GlobalConfig.config do |c|
23
+ # ...your configuration options provided as before...
24
+ end
25
+ ```
26
+
27
+ * **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:
28
+
29
+ ```ruby
30
+ LokaliseRails::GlobalConfig.config do |c|
31
+ c.branch = "master"
32
+ end
33
+ ```
34
+
35
+ * **Breaking change**: to run your task prommatically, use a new approach:
36
+
37
+ ```ruby
38
+ LokaliseManager.importer(optional_config, global_config_object).import!
39
+
40
+ LokaliseManager.exporter(optional_config, global_config_object).export!
41
+ ```
42
+
43
+ * 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.
44
+ * No need to say `require 'lokalise_rails` in your `lokalise_rails.rb` config file anymore.
45
+
3
46
  ## 2.0.0 (19-Aug-21)
4
47
 
5
48
  * Add exponential backoff mechanism for file imports to comply with the upcoming API changes ("rate limiting"), see below for more details.
data/Gemfile CHANGED
@@ -5,5 +5,16 @@ source 'http://rubygems.org'
5
5
  gemspec
6
6
 
7
7
  group :test do
8
- gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
8
+ gem 'codecov', '~> 0.2'
9
+ gem 'dotenv', '~> 2.5'
10
+ gem 'rails', '~> 7.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'
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,84 +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:
86
96
 
87
- ## Configuration
97
+ ```ruby
98
+ importer.import!
88
99
 
89
- Options are specified in the `config/lokalise_rails.rb` file.
100
+ # OR
90
101
 
91
- ### Global settings
102
+ exporter.export!
103
+ ```
92
104
 
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.
105
+ ## Configuration
98
106
 
99
- ### Import settings
107
+ Options are specified in the `config/lokalise_rails.rb` file.
100
108
 
101
- * `import_opts` (`hash`) - options that will be passed to Lokalise API when downloading translations to your app. Here are the default options:
109
+ Here's an example:
102
110
 
103
111
  ```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
- ```
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']
113
116
 
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
- * `max_retries_import` (`integer`) - this option is introduced to properly handle Lokalise API rate limiting. If the HTTP status code 429 (too many requests) has been received, LokaliseRails will apply an exponential backoff mechanism with a very simple formula: `2 ** retries`. If the maximum number of retries has been reached, a `Lokalise::Error::TooManyRequests` exception will be raised and the export operation will be halted.
117
+ # Provide a custom path to the directory with your translation files:
118
+ # c.locales_path = "#{Rails.root}/config/locales"
117
119
 
118
- ### Export settings
120
+ # Provide a Lokalise project branch to use:
121
+ c.branch = 'develop'
119
122
 
120
- * `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:
121
- + `data` (`string`, required) - base64-encoded contents of the translation file.
122
- + `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.
123
- + `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
+ # Provide request timeouts for the Lokalise API client:
124
+ c.timeouts = {open_timeout: 5, timeout: 5}
124
125
 
125
- ```yaml
126
- en_US:
127
- my_key: "my value"
128
- ```
126
+ # Provide maximum number of retries for file exporting:
127
+ c.max_retries_export = 5
129
128
 
130
- **Please note** that if your Lokalise project does not have a language with the specified `lang_iso` code, the export will fail.
129
+ # Provide maximum number of retries for file importing:
130
+ c.max_retries_import = 5
131
131
 
132
- * `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
+ # 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
+ # }
133
141
 
134
- ```ruby
135
- c.skip_file_export = ->(file) { f.split[1].to_s.include?('fr') }
136
- ```
142
+ # Safe mode for imports is disabled by default:
143
+ # c.import_safe_mode = false
144
+
145
+ # Additional export options (only filename, contents, and lang_iso params are provided by default)
146
+ # c.export_opts = {}
137
147
 
138
- * `max_retries_export` (`integer`) - this option is introduced to properly handle Lokalise API rate limiting. If the HTTP status code 429 (too many requests) has been received, LokaliseRails will apply an exponential backoff mechanism with a very simple formula: `2 ** retries` (initially `retries` is `0`). If the maximum number of retries has been reached, a `Lokalise::Error::TooManyRequests` exception will be raised and the export operation will be halted. By default, LokaliseRails will make up to `5` retries which potentially means `1 + 2 + 4 + 8 + 16 + 32 = 63` seconds of waiting time. If the `max_retries_export` is less than `1`, LokaliseRails will not perform any retries and give up immediately after receiving error 429.
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') }
139
150
 
140
- ### Settings to work with formats other than YAML
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
141
154
 
142
- If your translation files are not in YAML format, you will need to adjust the following options:
155
+ ## Load translations data and make sure they are valid:
156
+ ## c.translations_loader = ->(raw_data) { YAML.safe_load raw_data }
143
157
 
144
- * `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).
145
- * `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 }`.
146
- * `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 }`.
147
- * `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 }`.
158
+ ## Convert translations data to a proper format:
159
+ ## c.translations_converter = ->(raw_data) { raw_data.to_yaml }
160
+
161
+ ## Infer language ISO code for the translation file:
162
+ ## c.lang_iso_inferer = ->(data) { YAML.safe_load(data)&.keys&.first }
163
+ end
164
+ ```
148
165
 
149
166
  ## Running tests
150
167
 
@@ -3,7 +3,9 @@
3
3
  require 'rails/generators'
4
4
 
5
5
  module LokaliseRails
6
+ # Generators for LokaliseRails
6
7
  module Generators
8
+ # Installs LokaliseRails config
7
9
  class InstallGenerator < Rails::Generators::Base
8
10
  source_root File.expand_path('../templates', __dir__)
9
11
 
@@ -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,7 +9,7 @@ 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}
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LokaliseRails
4
+ # Global configuration, based on LokaliseManager config
5
+ class GlobalConfig < LokaliseManager::GlobalConfig
6
+ class << self
7
+ def locales_path
8
+ @locales_path || "#{LokaliseRails::Utils.root}/config/locales"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LokaliseRails
4
+ # Load Rake tasks
4
5
  class Railtie < Rails::Railtie
5
6
  rake_tasks do
6
7
  load 'tasks/lokalise_rails_tasks.rake'
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ module LokaliseRails
6
+ # Util methods
7
+ module Utils
8
+ class << self
9
+ # Current project root
10
+ def root
11
+ Pathname.new(rails_root || Dir.getwd)
12
+ end
13
+
14
+ # Tries to get Rails root if Rails is defined
15
+ def rails_root
16
+ return ::Rails.root.to_s if defined?(::Rails.root) && ::Rails.root
17
+ return RAILS_ROOT.to_s if defined?(RAILS_ROOT)
18
+
19
+ nil
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # :nocov:
3
4
  module LokaliseRails
4
- VERSION = '2.0.0'
5
+ VERSION = '5.0.0'
5
6
  end
7
+ # :nocov:
@@ -1,94 +1,14 @@
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 'zeitwerk'
4
+ require 'lokalise_manager'
7
5
 
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
- :max_retries_export, :max_retries_import
15
-
16
- # Main interface to provide configuration options for rake tasks
17
- def config
18
- yield self
19
- end
20
-
21
- # Full path to directory with translation files
22
- def locales_path
23
- @locales_path || "#{Rails.root}/config/locales"
24
- end
25
-
26
- # Project branch to use
27
- def branch
28
- @branch || 'master'
29
- end
30
-
31
- # Set request timeouts for the Lokalise API client
32
- def timeouts
33
- @timeouts || {}
34
- end
35
-
36
- # Maximum number of retries for file exporting
37
- def max_retries_export
38
- @max_retries_export || 5
39
- end
40
-
41
- # Maximum number of retries for file importing
42
- def max_retries_import
43
- @max_retries_import || 5
44
- end
45
-
46
- # Regular expression used to select translation files with proper extensions
47
- def file_ext_regexp
48
- @file_ext_regexp || /\.ya?ml\z/i
49
- end
6
+ loader = Zeitwerk::Loader.for_gem
7
+ loader.ignore "#{__dir__}/lokalise_rails/railtie.rb"
8
+ loader.setup
50
9
 
51
- # Options for import rake task
52
- def import_opts
53
- @import_opts || {
54
- format: 'yaml',
55
- placeholder_format: :icu,
56
- yaml_include_root: true,
57
- original_filenames: true,
58
- directory_prefix: '',
59
- indentation: '2sp'
60
- }
61
- end
62
-
63
- # Options for export rake task
64
- def export_opts
65
- @export_opts || {}
66
- end
67
-
68
- # Enables safe mode for import. When enabled, will check whether the target folder is empty or not
69
- def import_safe_mode
70
- @import_safe_mode.nil? ? false : @import_safe_mode
71
- end
72
-
73
- # Additional file skip criteria to apply when performing export
74
- def skip_file_export
75
- @skip_file_export || ->(_) { false }
76
- end
77
-
78
- def translations_loader
79
- @translations_loader || ->(raw_data) { YAML.safe_load raw_data }
80
- end
81
-
82
- # Converts translations data to the proper format
83
- def translations_converter
84
- @translations_converter || ->(raw_data) { raw_data.to_yaml }
85
- end
86
-
87
- # Infers lang ISO for the given translation file
88
- def lang_iso_inferer
89
- @lang_iso_inferer || ->(data) { YAML.safe_load(data)&.keys&.first }
90
- end
91
- end
10
+ # Main LokaliseRails module
11
+ module LokaliseRails
92
12
  end
93
13
 
94
- require 'lokalise_rails/railtie' if defined?(Rails)
14
+ require_relative '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
@@ -5,14 +5,14 @@ require File.expand_path('lib/lokalise_rails/version', __dir__)
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'lokalise_rails'
7
7
  spec.version = LokaliseRails::VERSION
8
- spec.authors = ['Ilya Bodrov']
8
+ spec.authors = ['Ilya Bodrov-Krukowski']
9
9
  spec.email = ['golosizpru@gmail.com']
10
10
  spec.summary = 'Lokalise integration for Ruby on Rails'
11
11
  spec.description = 'This gem allows to exchange translation files between your Rails app and Lokalise TMS.'
12
12
  spec.homepage = 'https://github.com/bodrovis/lokalise_rails'
13
13
  spec.license = 'MIT'
14
14
  spec.platform = Gem::Platform::RUBY
15
- spec.required_ruby_version = '>= 2.5.0'
15
+ spec.required_ruby_version = '>= 2.7.0'
16
16
 
17
17
  spec.files = Dir['README.md', 'LICENSE',
18
18
  'CHANGELOG.md', 'lib/**/*.rb',
@@ -23,21 +23,10 @@ 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', '~> 3.0'
27
+ spec.add_dependency 'zeitwerk', '~> 2.4'
28
28
 
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'
29
+ spec.metadata = {
30
+ 'rubygems_mfa_required' => 'true'
31
+ }
43
32
  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
@@ -3,11 +3,11 @@
3
3
  require 'generators/lokalise_rails/install_generator'
4
4
 
5
5
  describe LokaliseRails::Generators::InstallGenerator do
6
- before :all do
6
+ before do
7
7
  remove_config
8
8
  end
9
9
 
10
- after :all do
10
+ after do
11
11
  remove_config
12
12
  add_config!
13
13
  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')
@@ -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