lokalise_rails 1.1.0 → 1.2.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: 951a2d142a56078d2d9509fa36c13f87f7069b8f2defa9bd158a90a67aba2b9a
4
- data.tar.gz: ac2360ad608a4251c9061454c0fea986f868facb90567a5bcd94ae01d33bd8e3
3
+ metadata.gz: 02b78ecae0b5dbf76599249606858c5b24e31fc9826f8795feb34abac9d5ffb4
4
+ data.tar.gz: b7bc51d45b689181ae0d3d5366f507ae0e43c878c4ab55979ba54d7089eb49f7
5
5
  SHA512:
6
- metadata.gz: 4f41072fa0c0e138fc3f6a838b672c50e0f3af799e151b0cbc2b66aa1792446c6cdde4a1426ed1a5243871c48cc55bc9e12439ecc7eb00be090629ad0a77bd4c
7
- data.tar.gz: ae1acd8d95ce227c8c16a362d2fb0d0bc371a986e5108043a7d9f3037bf292e1da2a48efa4c84720e7a4f1f1e66c13e757ba59ababe1ab081d77960516340803
6
+ metadata.gz: 838ddae7af99842dbc4a7ce900c8b02a07571ff9c561e642e3bac6f92dbaabd9132f775b0ff1877ffd18fc20c1bf84ec3f7792139bad0c9fc97dec1d03389e64
7
+ data.tar.gz: 79c0314752c8e48b552eb78c564d24da0cd14fa41b1195909d8d06ca81913322fefa56a696f3c20d1c53c51aa16feb731c85f6fdd860242c98ad8b018b9c68ae
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.0 (11-Nov-20)
4
+
5
+ * New option `translations_loader`
6
+ * New option `translations_converter`
7
+ * New option `lang_iso_inferer`
8
+
3
9
  ## 1.1.0 (23-Oct-20)
4
10
 
5
11
  * New option `branch`
data/README.md CHANGED
@@ -92,7 +92,6 @@ Options are specified in the `config/lokalise_rails.rb` file.
92
92
  * `api_token` (`string`, required) - Lokalise API token with read/write permissions.
93
93
  * `project_id` (`string`, required) - Lokalise project ID. You must have import/export permissions in the specified project.
94
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
95
  * `branch` (`string`) - Lokalise project branch to use. Defaults to `"master"`.
97
96
  * `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
97
 
@@ -134,6 +133,15 @@ en_US:
134
133
  c.skip_file_export = ->(file) { f.split[1].to_s.include?('fr') }
135
134
  ```
136
135
 
136
+ ### Settings to work with formats other than YAML
137
+
138
+ If your translation files are not in YAML format, you will need to adjust the following options:
139
+
140
+ * `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).
141
+ * `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 }`.
142
+ * `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 }`.
143
+ * `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 }`.
144
+
137
145
  ## Running tests
138
146
 
139
147
  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.
@@ -35,6 +35,16 @@ LokaliseRails.config do |c|
35
35
  # Provide additional file exclusion criteria for exports (by default, any file with the proper extension will be exported)
36
36
  # c.skip_file_export = ->(file) { file.split[1].to_s.include?('fr') }
37
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
38
+ # Set the options below if you would like to work with format other than YAML
39
+ ## Regular expression to use when choosing the files to extract from the downloaded archive and upload to Lokalise
40
+ ## c.file_ext_regexp = /\.ya?ml\z/i
41
+
42
+ ## Load translations data and make sure they are valid:
43
+ ## c.translations_loader = ->(raw_data) { YAML.safe_load raw_data }
44
+
45
+ ## Convert translations data to a proper format:
46
+ ## c.translations_converter = ->(raw_data) { raw_data.to_yaml }
47
+
48
+ ## Infer language ISO code for the translation file:
49
+ ## c.lang_iso_inferer = ->(data) { YAML.safe_load(data)&.keys&.first }
40
50
  end
@@ -8,7 +8,8 @@ module LokaliseRails
8
8
  class << self
9
9
  attr_accessor :api_token, :project_id
10
10
  attr_writer :import_opts, :import_safe_mode, :export_opts, :locales_path,
11
- :file_ext_regexp, :skip_file_export, :branch, :timeouts
11
+ :file_ext_regexp, :skip_file_export, :branch, :timeouts,
12
+ :translations_loader, :translations_converter, :lang_iso_inferer
12
13
 
13
14
  # Main interface to provide configuration options for rake tasks
14
15
  def config
@@ -61,6 +62,20 @@ module LokaliseRails
61
62
  def skip_file_export
62
63
  @skip_file_export || ->(_) { false }
63
64
  end
65
+
66
+ def translations_loader
67
+ @translations_loader || ->(raw_data) { YAML.safe_load raw_data }
68
+ end
69
+
70
+ # Converts translations data to the proper format
71
+ def translations_converter
72
+ @translations_converter || ->(raw_data) { raw_data.to_yaml }
73
+ end
74
+
75
+ # Infers lang ISO for the given translation file
76
+ def lang_iso_inferer
77
+ @lang_iso_inferer || ->(data) { YAML.safe_load(data)&.keys&.first }
78
+ end
64
79
  end
65
80
  end
66
81
 
@@ -55,12 +55,10 @@ module LokaliseRails
55
55
  def opts(full_p, relative_p)
56
56
  content = File.read full_p
57
57
 
58
- lang_iso = YAML.safe_load(content)&.keys&.first
59
-
60
58
  initial_opts = {
61
59
  data: Base64.strict_encode64(content.strip),
62
60
  filename: relative_p,
63
- lang_iso: lang_iso
61
+ lang_iso: LokaliseRails.lang_iso_inferer.call(content)
64
62
  }
65
63
 
66
64
  initial_opts.merge LokaliseRails.export_opts
@@ -46,7 +46,7 @@ module LokaliseRails
46
46
  #
47
47
  # @param path [String]
48
48
  def open_and_process_zip(path)
49
- Zip::File.open_buffer(URI.open(path)) do |zip|
49
+ Zip::File.open_buffer(open_file_or_remote(path)) do |zip|
50
50
  fetch_zip_entries(zip) { |entry| process!(entry) }
51
51
  end
52
52
  end
@@ -64,13 +64,13 @@ module LokaliseRails
64
64
 
65
65
  # Processes ZIP entry by reading its contents and creating the corresponding translation file
66
66
  def process!(zip_entry)
67
- data = YAML.safe_load zip_entry.get_input_stream.read
67
+ data = data_from zip_entry
68
68
  subdir, filename = subdir_and_filename_for zip_entry.name
69
69
  full_path = "#{LokaliseRails.locales_path}/#{subdir}"
70
70
  FileUtils.mkdir_p full_path
71
71
 
72
72
  File.open(File.join(full_path, filename), 'w+:UTF-8') do |f|
73
- f.write data.to_yaml
73
+ f.write LokaliseRails.translations_converter.call(data)
74
74
  end
75
75
  rescue StandardError => e
76
76
  $stdout.puts "Error when trying to process #{zip_entry&.name}: #{e.inspect}"
@@ -87,6 +87,24 @@ module LokaliseRails
87
87
  answer = $stdin.gets
88
88
  answer.to_s.strip == 'Y'
89
89
  end
90
+
91
+ # Opens a local file or a remote URL using the provided patf
92
+ #
93
+ # @return [String]
94
+ def open_file_or_remote(path)
95
+ parsed_path = URI.parse(path)
96
+ if parsed_path&.scheme&.include?('http')
97
+ parsed_path.open
98
+ else
99
+ File.open path
100
+ end
101
+ end
102
+
103
+ private
104
+
105
+ def data_from(zip_entry)
106
+ LokaliseRails.translations_loader.call zip_entry.get_input_stream.read
107
+ end
90
108
  end
91
109
  end
92
110
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LokaliseRails
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -35,9 +35,9 @@ Gem::Specification.new do |spec|
35
35
  end
36
36
  spec.add_development_dependency 'rake', '~> 13.0'
37
37
  spec.add_development_dependency 'rspec', '~> 3.6'
38
- spec.add_development_dependency 'rubocop', '~> 0.60'
38
+ spec.add_development_dependency 'rubocop', '~> 1.0'
39
39
  spec.add_development_dependency 'rubocop-performance', '~> 1.5'
40
- spec.add_development_dependency 'rubocop-rspec', '~> 1.37'
40
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.0.0'
41
41
  spec.add_development_dependency 'simplecov', '~> 0.16'
42
42
  spec.add_development_dependency 'vcr', '~> 6.0'
43
43
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  require 'lokalise_rails'
4
2
  LokaliseRails.config do |c|
5
3
  c.api_token = ENV['LOKALISE_API_TOKEN']
@@ -45,12 +45,12 @@ describe LokaliseRails::TaskDefinition::Base do
45
45
  end
46
46
 
47
47
  it 'returns an error when the project_id is not set' do
48
- allow(LokaliseRails).to receive(:project_id).and_return(nil)
49
- errors = described_class.opt_errors
48
+ allow_project_id nil do
49
+ errors = described_class.opt_errors
50
50
 
51
- expect(LokaliseRails).to have_received(:project_id)
52
- expect(errors.length).to eq(1)
53
- expect(errors.first).to include('Project ID is not set')
51
+ expect(errors.length).to eq(1)
52
+ expect(errors.first).to include('Project ID is not set')
53
+ end
54
54
  end
55
55
  end
56
56
 
@@ -49,10 +49,9 @@ describe LokaliseRails::TaskDefinition::Exporter do
49
49
  end
50
50
 
51
51
  it 'halts when the project_id is not set' do
52
- allow(LokaliseRails).to receive(:project_id).and_return(nil)
53
-
54
- expect(-> { described_class.export! }).to output(/Project ID is not set/).to_stdout
55
- expect(LokaliseRails).to have_received(:project_id)
52
+ allow_project_id nil do
53
+ expect(-> { described_class.export! }).to output(/Project ID is not set/).to_stdout
54
+ end
56
55
  end
57
56
  end
58
57
 
@@ -12,14 +12,14 @@ describe LokaliseRails::TaskDefinition::Importer do
12
12
 
13
13
  describe '.download_files' do
14
14
  it 'returns a proper download URL' do
15
- allow(LokaliseRails).to receive(:project_id).and_return('189934715f57a162257d74.88352370')
16
- response = VCR.use_cassette('download_files') do
17
- described_class.download_files
18
- end
15
+ allow_project_id '189934715f57a162257d74.88352370' do
16
+ response = VCR.use_cassette('download_files') do
17
+ described_class.download_files
18
+ end
19
19
 
20
- expect(LokaliseRails).to have_received(:project_id)
21
- expect(response['project_id']).to eq('189934715f57a162257d74.88352370')
22
- expect(response['bundle_url']).to include('s3-eu-west-1.amazonaws.com')
20
+ expect(response['project_id']).to eq('189934715f57a162257d74.88352370')
21
+ expect(response['bundle_url']).to include('s3-eu-west-1.amazonaws.com')
22
+ end
23
23
  end
24
24
 
25
25
  it 'rescues from errors during file download' do
@@ -40,10 +40,10 @@ describe LokaliseRails::TaskDefinition::Importer do
40
40
  end
41
41
 
42
42
  it 'halts when the project_id is not set' do
43
- allow(LokaliseRails).to receive(:project_id).and_return(nil)
44
- expect(-> { described_class.import! }).to output(/Project ID is not set/).to_stdout
45
- expect(LokaliseRails).to have_received(:project_id)
46
- expect(count_translations).to eq(0)
43
+ allow_project_id nil do
44
+ expect(-> { described_class.import! }).to output(/Project ID is not set/).to_stdout
45
+ expect(count_translations).to eq(0)
46
+ end
47
47
  end
48
48
  end
49
49
  end
@@ -73,5 +73,23 @@ describe LokaliseRails do
73
73
  allow(fake_class).to receive(:skip_file_export=).with(cond)
74
74
  fake_class.skip_file_export = cond
75
75
  end
76
+
77
+ it 'is possible to set translations_loader' do
78
+ runner = ->(f) { f.to_json }
79
+ allow(fake_class).to receive(:translations_loader=).with(runner)
80
+ fake_class.translations_loader = runner
81
+ end
82
+
83
+ it 'is possible to set translations_converter' do
84
+ runner = ->(f) { f.to_json }
85
+ allow(fake_class).to receive(:translations_converter=).with(runner)
86
+ fake_class.translations_converter = runner
87
+ end
88
+
89
+ it 'is possible to set lang_iso_inferer' do
90
+ runner = ->(f) { f.to_json }
91
+ allow(fake_class).to receive(:lang_iso_inferer=).with(runner)
92
+ fake_class.lang_iso_inferer = runner
93
+ end
76
94
  end
77
95
  end
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SpecAddons
4
- def allow_project_id(value = '189934715f57a162257d74.88352370')
5
- allow(LokaliseRails).to receive(:project_id).
6
- and_return(value)
4
+ def allow_project_id(value = ENV['LOKALISE_PROJECT_ID'])
5
+ allow(LokaliseRails).to receive(:project_id).and_return(value)
6
+ return unless block_given?
7
+
8
+ yield
9
+ expect(LokaliseRails).to have_received(:project_id)
7
10
  end
8
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lokalise_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Bodrov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-23 00:00:00.000000000 Z
11
+ date: 2020-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-lokalise-api
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '0.60'
117
+ version: '1.0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '0.60'
124
+ version: '1.0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rubocop-performance
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: '1.37'
145
+ version: 2.0.0
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: '1.37'
152
+ version: 2.0.0
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: simplecov
155
155
  requirement: !ruby/object:Gem::Requirement
@@ -243,7 +243,7 @@ homepage: https://github.com/bodrovis/lokalise_rails
243
243
  licenses:
244
244
  - MIT
245
245
  metadata: {}
246
- post_install_message:
246
+ post_install_message:
247
247
  rdoc_options: []
248
248
  require_paths:
249
249
  - lib
@@ -259,7 +259,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
259
259
  version: '0'
260
260
  requirements: []
261
261
  rubygems_version: 3.1.4
262
- signing_key:
262
+ signing_key:
263
263
  specification_version: 4
264
264
  summary: Lokalise integration for Ruby on Rails
265
265
  test_files: