lokalise_rails 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ac898ae13038db9d3426f367e3cba3b54c41e35797dfb56dd5da5595959e728
4
- data.tar.gz: 2bf9f24929b226b8994b3f4648f67b5da59aa0b68687cb7f597f531ac2d7bb99
3
+ metadata.gz: 3dc5e8a5807db8316b7b60606b5e32274d669504f12917022e0fbaba3f0969db
4
+ data.tar.gz: 2c54267a133e24cef62019185d2063b09b3c57bd26bca4ee28364ab2c05f6c21
5
5
  SHA512:
6
- metadata.gz: 93be5d1f8379908fbf6e3ac7a1f44534373779d8aa3e375f456ef7a83454a07babe3971aa364afe374759e54571d036c60c68e16f14c81397128770573058a9a
7
- data.tar.gz: 84c42e122da897f7103a16d11e9a5e066cee28c2b6413c44ec3e459868dfe89d8c5005f3fa644024bea7dfd7c49233de5fecc67746016485f467febe91b3fa52
6
+ metadata.gz: 21a740e73e2d2bdef702ed8ebf719b60b70474a3503428b05131ddfe0443cc21bf0750203ee9aee9c0b47b1d75ebaa200db7b47a934204d9fe800d3f2c66e1d3
7
+ data.tar.gz: 44288cec4fc4a95c95780bfe4e00f7efc23d23a07e94dd3c2bc885ef8ee4de309ade9555291aa6a8ae56a8bb99487c2a9982f026f54794e50e08acea3c93e038
data/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.0.0 (14-Oct-21)
4
+
5
+ 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).
6
+
7
+ * **Breaking change:** the global config class is renamed therefore update your `config/lokalise_rails.rb` file to look like this:
8
+
9
+ ```ruby
10
+ LokaliseRails::GlobalConfig.config do |c|
11
+ # ...your configuration options provided as before...
12
+ end
13
+ ```
14
+
15
+ * **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:
16
+
17
+ ```ruby
18
+ LokaliseRails::GlobalConfig.config do |c|
19
+ c.branch = "master"
20
+ end
21
+ ```
22
+
23
+ * **Breaking change**: to run your task prommatically, use a new approach:
24
+
25
+ ```ruby
26
+ LokaliseManager.importer(optional_config, global_config_object).import!
27
+
28
+ LokaliseManager.exporter(optional_config, global_config_object).export!
29
+ ```
30
+
31
+ * 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.
32
+ * No need to say `require 'lokalise_rails` in your `lokalise_rails.rb` config file anymore.
33
+
3
34
  ## 2.0.0 (19-Aug-21)
4
35
 
5
36
  * Add exponential backoff mechanism for file imports to comply with the upcoming API changes ("rate limiting"), see below for more details.
data/README.md CHANGED
@@ -5,7 +5,7 @@
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
 
@@ -33,9 +33,7 @@ rails g lokalise_rails:install
33
33
  The latter command will generate a new config file `config/lokalise_rails.rb` looking like this:
34
34
 
35
35
  ```ruby
36
- require 'lokalise_rails'
37
-
38
- LokaliseRails.config do |c|
36
+ LokaliseRails::GlobalConfig.config do |c|
39
37
  c.api_token = ENV['LOKALISE_API_TOKEN']
40
38
  c.project_id = ENV['LOKALISE_PROJECT_ID']
41
39
 
@@ -45,7 +43,7 @@ end
45
43
 
46
44
  You have to provide `api_token` and `project_id` to proceed. `project_id` can be found in your Lokalise project settings.
47
45
 
48
- [Other options can be customized as well (see below)](https://github.com/bodrovis/lokalise_rails#import-settings) but they have sensible defaults.
46
+ 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
47
 
50
48
  ## Importing translations from Lokalise
51
49
 
@@ -55,7 +53,7 @@ To import translations from the specified Lokalise project to your Rails app, ru
55
53
  rails lokalise_rails:import
56
54
  ```
57
55
 
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.
56
+ 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
57
 
60
58
  ## Exporting translations to Lokalise
61
59
 
@@ -67,84 +65,101 @@ rails lokalise_rails:export
67
65
 
68
66
  ## Running tasks programmatically
69
67
 
70
- You can also run the import and export tasks from the Rails app:
68
+ 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.
69
+
70
+ For example, you can use the following approach:
71
71
 
72
72
  ```ruby
73
+ # This line is actually not required. Include it if you would like to use your global settings:
73
74
  require "#{Rails.root}/config/lokalise_rails.rb"
74
75
 
75
- # Import the files:
76
- result = LokaliseRails::TaskDefinition::Importer.import!
76
+ importer = LokaliseManager.importer({api_token: '1234abc', project_id: '123.abc'}, LokaliseRails::GlobalConfig)
77
+
78
+ # OR
79
+
80
+ exporter = LokaliseManager.exporter({api_token: '1234abc', project_id: '123.abc'}, LokaliseRails::GlobalConfig)
77
81
  ```
78
- `result` contains a boolean value with the result of the operation
82
+
83
+ 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.
84
+
85
+ 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`.
86
+
87
+ However, you can also provide the translation files folder on per-client basis, for instance:
79
88
 
80
89
  ```ruby
81
- # Export the files:
82
- processes = LokaliseRails::TaskDefinition::Exporter.export!
90
+ importer = LokaliseManager.importer api_token: '1234abc', project_id: '123.abc', locales_path: "#{Rails.root}/config/locales"
83
91
  ```
84
92
 
85
- `processes` contains a list of [queued background processes](https://lokalise.github.io/ruby-lokalise-api/api/queued-processes).
93
+ After the client is instantiated, you can run the corresponding task:
86
94
 
87
- ## Configuration
95
+ ```ruby
96
+ importer.import!
88
97
 
89
- Options are specified in the `config/lokalise_rails.rb` file.
98
+ # OR
90
99
 
91
- ### Global settings
100
+ exporter.export!
101
+ ```
92
102
 
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.
103
+ ## Configuration
98
104
 
99
- ### Import settings
105
+ Options are specified in the `config/lokalise_rails.rb` file.
100
106
 
101
- * `import_opts` (`hash`) - options that will be passed to Lokalise API when downloading translations to your app. Here are the default options:
107
+ Here's an example:
102
108
 
103
109
  ```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
- ```
110
+ LokaliseRails::GlobalConfig.config do |c|
111
+ # These are mandatory options that you must set before running rake tasks:
112
+ c.api_token = ENV['LOKALISE_API_TOKEN']
113
+ c.project_id = ENV['LOKALISE_PROJECT_ID']
113
114
 
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.
115
+ # Provide a custom path to the directory with your translation files:
116
+ # c.locales_path = "#{Rails.root}/config/locales"
117
117
 
118
- ### Export settings
118
+ # Provide a Lokalise project branch to use:
119
+ c.branch = 'develop'
119
120
 
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`:
121
+ # Provide request timeouts for the Lokalise API client:
122
+ c.timeouts = {open_timeout: 5, timeout: 5}
124
123
 
125
- ```yaml
126
- en_US:
127
- my_key: "my value"
128
- ```
124
+ # Provide maximum number of retries for file exporting:
125
+ c.max_retries_export = 5
129
126
 
130
- **Please note** that if your Lokalise project does not have a language with the specified `lang_iso` code, the export will fail.
127
+ # Provide maximum number of retries for file importing:
128
+ c.max_retries_import = 5
131
129
 
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:
130
+ # Import options have the following defaults:
131
+ # c.import_opts = {
132
+ # format: 'yaml',
133
+ # placeholder_format: :icu,
134
+ # yaml_include_root: true,
135
+ # original_filenames: true,
136
+ # directory_prefix: '',
137
+ # indentation: '2sp'
138
+ # }
133
139
 
134
- ```ruby
135
- c.skip_file_export = ->(file) { f.split[1].to_s.include?('fr') }
136
- ```
140
+ # Safe mode for imports is disabled by default:
141
+ # c.import_safe_mode = false
142
+
143
+ # Additional export options (only filename, contents, and lang_iso params are provided by default)
144
+ # c.export_opts = {}
145
+
146
+ # Provide additional file exclusion criteria for exports (by default, any file with the proper extension will be exported)
147
+ # c.skip_file_export = ->(file) { file.split[1].to_s.include?('fr') }
137
148
 
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.
149
+ # Set the options below if you would like to work with format other than YAML
150
+ ## Regular expression to use when choosing the files to extract from the downloaded archive and upload to Lokalise
151
+ ## c.file_ext_regexp = /\.ya?ml\z/i
139
152
 
140
- ### Settings to work with formats other than YAML
153
+ ## Load translations data and make sure they are valid:
154
+ ## c.translations_loader = ->(raw_data) { YAML.safe_load raw_data }
141
155
 
142
- If your translation files are not in YAML format, you will need to adjust the following options:
156
+ ## Convert translations data to a proper format:
157
+ ## c.translations_converter = ->(raw_data) { raw_data.to_yaml }
143
158
 
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 }`.
159
+ ## Infer language ISO code for the translation file:
160
+ ## c.lang_iso_inferer = ->(data) { YAML.safe_load(data)&.keys&.first }
161
+ end
162
+ ```
148
163
 
149
164
  ## Running tests
150
165
 
@@ -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,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,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LokaliseRails
4
- VERSION = '2.0.0'
4
+ VERSION = '3.0.0'
5
5
  end
@@ -1,94 +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
- :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
5
+ require 'lokalise_rails/utils'
6
+ require 'lokalise_rails/global_config'
50
7
 
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
8
+ module LokaliseRails
92
9
  end
93
10
 
94
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,13 +23,12 @@ 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', '~> 1.0'
28
27
 
29
28
  spec.add_development_dependency 'codecov', '~> 0.2'
30
29
  spec.add_development_dependency 'dotenv', '~> 2.5'
31
30
  if ENV['TEST_RAILS_VERSION'].nil?
32
- spec.add_development_dependency 'rails', '~> 6.1.0'
31
+ spec.add_development_dependency 'rails', '~> 6.1.4'
33
32
  else
34
33
  spec.add_development_dependency 'rails', ENV['TEST_RAILS_VERSION'].to_s
35
34
  end
@@ -37,7 +36,7 @@ Gem::Specification.new do |spec|
37
36
  spec.add_development_dependency 'rspec', '~> 3.6'
38
37
  spec.add_development_dependency 'rubocop', '~> 1.0'
39
38
  spec.add_development_dependency 'rubocop-performance', '~> 1.5'
40
- spec.add_development_dependency 'rubocop-rspec', '~> 2.4.0'
39
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.5.0'
41
40
  spec.add_development_dependency 'simplecov', '~> 0.16'
42
41
  spec.add_development_dependency 'vcr', '~> 6.0'
43
42
  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
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe LokaliseRails::GlobalConfig do
4
+ let(:child_config) { Class.new(described_class) }
5
+
6
+ it 'is possible to provide config options' do
7
+ described_class.config do |c|
8
+ expect(c).to eq(described_class)
9
+ end
10
+ end
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
+
26
+ describe 'parameters' do
27
+ let(:fake_class) { class_double(described_class) }
28
+
29
+ it 'is possible to set project_id' do
30
+ allow(fake_class).to receive(:project_id=).with('123.abc')
31
+ fake_class.project_id = '123.abc'
32
+ end
33
+
34
+ it 'is possible to set file_ext_regexp' do
35
+ allow(fake_class).to receive(:file_ext_regexp=).with(Regexp.new('.*'))
36
+ fake_class.file_ext_regexp = Regexp.new('.*')
37
+ end
38
+
39
+ it 'is possible to set import_opts' do
40
+ allow(fake_class).to receive(:import_opts=).with(duck_type(:each))
41
+ fake_class.import_opts = {
42
+ format: 'json',
43
+ indentation: '8sp'
44
+ }
45
+ end
46
+
47
+ it 'is possible to set export_opts' do
48
+ allow(fake_class).to receive(:export_opts=).with(duck_type(:each))
49
+ fake_class.export_opts = {
50
+ convert_placeholders: true,
51
+ detect_icu_plurals: true
52
+ }
53
+ end
54
+
55
+ it 'is possible to set branch' do
56
+ allow(fake_class).to receive(:branch=).with('custom')
57
+ fake_class.branch = 'custom'
58
+ end
59
+
60
+ it 'is possible to set timeouts' do
61
+ allow(fake_class).to receive(:timeouts=).with(duck_type(:each))
62
+ fake_class.timeouts = {
63
+ open_timeout: 100,
64
+ timeout: 500
65
+ }
66
+ end
67
+
68
+ it 'is possible to set import_safe_mode' do
69
+ allow(fake_class).to receive(:import_safe_mode=).with(true)
70
+ fake_class.import_safe_mode = true
71
+ end
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
+
83
+ it 'is possible to set api_token' do
84
+ allow(fake_class).to receive(:api_token=).with('abc')
85
+ fake_class.api_token = 'abc'
86
+ end
87
+
88
+ it 'is possible to override locales_path' do
89
+ allow(fake_class).to receive(:locales_path=).with('/demo/path')
90
+ fake_class.locales_path = '/demo/path'
91
+ end
92
+
93
+ it 'is possible to set skip_file_export' do
94
+ cond = ->(f) { f.nil? }
95
+ allow(fake_class).to receive(:skip_file_export=).with(cond)
96
+ fake_class.skip_file_export = cond
97
+ end
98
+
99
+ it 'is possible to set translations_loader' do
100
+ runner = ->(f) { f.to_json }
101
+ allow(fake_class).to receive(:translations_loader=).with(runner)
102
+ fake_class.translations_loader = runner
103
+ end
104
+
105
+ it 'is possible to set translations_converter' do
106
+ runner = ->(f) { f.to_json }
107
+ allow(fake_class).to receive(:translations_converter=).with(runner)
108
+ fake_class.translations_converter = runner
109
+ end
110
+
111
+ it 'is possible to set lang_iso_inferer' do
112
+ runner = ->(f) { f.to_json }
113
+ allow(fake_class).to receive(:lang_iso_inferer=).with(runner)
114
+ fake_class.lang_iso_inferer = runner
115
+ end
116
+ end
117
+ end