rails_i18n_manager 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42c0b085e7edbb9d009e8bccba78e937350e914d44b6d571fbfa9dab4949f23f
4
- data.tar.gz: cf189df04c6de14d7796a34a9da4328d290066a81e7992048703df65d2c71c8a
3
+ metadata.gz: 3398a11d93b08bd8d36e596876a4022bd6317cf45f99f69884652e39a66637ae
4
+ data.tar.gz: 1c2e294199f783bdd8edffea56742e20de92368e4e12cd386c8203d3fc4d640b
5
5
  SHA512:
6
- metadata.gz: 7318df372d6f81bfd1a53592a5ebf5e3d390233c9834fcb6aa72529922c79052c9f64ecd623dfc2ee437a1e4ee45d20d60d43410a8594574f153848ecbbd5ea4
7
- data.tar.gz: eb177a2ceddceb4f928d33e4119490ce8a8a61f8112e1453f80263ddf8395ce925d990e56998e513da5e78b32b90e22d11e430132d9f0a4a6b2a960288da8b23
6
+ metadata.gz: a5e012eac4960dd31575b95670eef389abee24fce0e5e12111e6d38d401bf2dc518d46c169679609c1108015a8c2762c99f3a84f1a10b8bcdfbe65f2d41c086f
7
+ data.tar.gz: 9b8b8ec138663a5d6a536f5db8b5a743ae870a9ac07addbf5246decbbff84427af676158165a4db15599e2ce9e7ca51572a87aeb7bd59d0be97f55b83e977ab5
@@ -96,26 +96,21 @@ module RailsI18nManager
96
96
  end
97
97
 
98
98
  def import
99
- @form = Forms::TranslationFileForm.new(params[:import_form])
100
-
101
99
  if request.get?
100
+ @form = Forms::TranslationFileForm.new
102
101
  render
103
102
  else
104
- if @form.valid?
105
- if @form.file.path.end_with?(".json")
106
- parsed_file_contents = JSON.parse(@form.file.read)
107
- else
108
- parsed_file_contents = YAML.safe_load(@form.file.read)
109
- end
103
+ @form = Forms::TranslationFileForm.new(params[:import_form])
110
104
 
105
+ if @form.valid?
111
106
  begin
112
- TranslationsImportJob.new.perform(
107
+ TranslationsImporter.import(
113
108
  translation_app_id: @form.translation_app_id,
114
- parsed_file_contents: parsed_file_contents,
109
+ parsed_file_contents: @form.parsed_file_contents,
115
110
  overwrite_existing: @form.overwrite_existing,
116
111
  mark_inactive_translations: @form.mark_inactive_translations,
117
112
  )
118
- rescue TranslationsImportJob::ImportAbortedError => e
113
+ rescue TranslationsImporter::ImportAbortedError => e
119
114
  flash.now.alert = e.message
120
115
  render
121
116
  return
@@ -123,7 +118,7 @@ module RailsI18nManager
123
118
 
124
119
  redirect_to translations_path, notice: "Import Successful"
125
120
  else
126
- flash.now.alert = "Import not started due to form errors."
121
+ flash.now.alert = "Please see form errors below"
127
122
  render
128
123
  end
129
124
  end
@@ -17,36 +17,53 @@ module RailsI18nManager
17
17
  @mark_inactive_translations = ["1", "true", "t"].include?(val.to_s.downcase)
18
18
  end
19
19
 
20
+ def file_extname
21
+ @file_extname ||= File.extname(file)
22
+ end
23
+
24
+ def file_contents_string
25
+ @file_contents_string ||= file.read
26
+ end
27
+
28
+ def parsed_file_contents
29
+ if defined?(@parsed_file_contents)
30
+ return @parsed_file_contents
31
+ end
32
+
33
+ case file_extname
34
+ when ".yml", ".yaml"
35
+ @parsed_file_contents = YAML.safe_load(file_contents_string)
36
+ when ".json"
37
+ @parsed_file_contents = JSON.parse(file_contents_string)
38
+ end
39
+ end
40
+
20
41
  def validate_file
21
42
  if file.blank?
22
43
  errors.add(:file, "Must upload a valid translation file.")
23
44
  return
24
45
  end
25
46
 
26
- file_extname = File.extname(file)
27
-
28
47
  if [".yml", ".yaml", ".json"].exclude?(file_extname)
29
48
  errors.add(:file, "Invalid file format. Must be yaml or json file.")
30
49
  return
31
50
  end
32
51
 
33
- file_contents = File.read(file)
34
-
35
- if file_contents.blank?
52
+ if file_contents_string.blank?
36
53
  errors.add(:file, "Empty file provided.")
37
54
  return
38
55
  end
39
56
 
40
57
  case file_extname
41
58
  when ".yml", ".yaml"
42
- if !YAML.safe_load(file_contents).is_a?(Hash)
59
+ if !parsed_file_contents.is_a?(Hash)
43
60
  errors.add(:file, "Invalid #{file_extname.sub(".","")} file.")
44
61
  return
45
62
  end
46
63
 
47
64
  when ".json"
48
65
  begin
49
- JSON.parse(file_contents)
66
+ parsed_file_contents
50
67
  rescue JSON::ParserError
51
68
  errors.add(:file, "Invalid json file.")
52
69
  return
@@ -1,9 +1,9 @@
1
1
  module RailsI18nManager
2
- class TranslationsImportJob
2
+ class TranslationsImporter
3
3
 
4
4
  class ImportAbortedError < StandardError; end
5
5
 
6
- def perform(translation_app_id:, parsed_file_contents:, overwrite_existing: false, mark_inactive_translations: false)
6
+ def self.import(translation_app_id:, parsed_file_contents:, overwrite_existing: false, mark_inactive_translations: false)
7
7
  app_record = TranslationApp.find(translation_app_id)
8
8
 
9
9
  new_locales = parsed_file_contents.keys - app_record.all_locales
@@ -29,7 +29,18 @@ module RailsI18nManager
29
29
  app_record.all_locales.each do |locale|
30
30
  split_keys = [locale] + key.split(".").map{|x| x}
31
31
 
32
- val = parsed_file_contents.dig(*split_keys)
32
+ val = nil
33
+
34
+ current_hash = parsed_file_contents
35
+
36
+ split_keys.each do |k|
37
+ if current_hash[k].is_a?(Hash)
38
+ current_hash = current_hash[k]
39
+ else
40
+ val = current_hash[k]
41
+ break
42
+ end
43
+ end
33
44
 
34
45
  if val.present?
35
46
  val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s }
@@ -1,3 +1,3 @@
1
1
  module RailsI18nManager
2
- VERSION = "1.1.1".freeze
2
+ VERSION = "1.1.2".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_i18n_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Ganger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-04 00:00:00.000000000 Z
11
+ date: 2025-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -225,10 +225,10 @@ files:
225
225
  - app/controllers/rails_i18n_manager/translations_controller.rb
226
226
  - app/helpers/rails_i18n_manager/application_helper.rb
227
227
  - app/helpers/rails_i18n_manager/custom_form_builder.rb
228
- - app/jobs/rails_i18n_manager/translations_import_job.rb
229
228
  - app/lib/rails_i18n_manager/forms/base.rb
230
229
  - app/lib/rails_i18n_manager/forms/translation_file_form.rb
231
230
  - app/lib/rails_i18n_manager/google_translate.rb
231
+ - app/lib/rails_i18n_manager/translations_importer.rb
232
232
  - app/models/rails_i18n_manager/application_record.rb
233
233
  - app/models/rails_i18n_manager/translation_app.rb
234
234
  - app/models/rails_i18n_manager/translation_key.rb