rails_i18n_manager 1.1.0 → 1.1.2

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: f728ba9cda5d596101c9da0d9e5336ca468d9db8b9c7c15d1bd621db9e065fc9
4
- data.tar.gz: 800e36c720bf9a2a2c18b9f2e286507b463cda8dbecdb3a7b67237c2faea20a7
3
+ metadata.gz: 3398a11d93b08bd8d36e596876a4022bd6317cf45f99f69884652e39a66637ae
4
+ data.tar.gz: 1c2e294199f783bdd8edffea56742e20de92368e4e12cd386c8203d3fc4d640b
5
5
  SHA512:
6
- metadata.gz: 9c5fa427b474d3e313adc680decffb40f9b403e6a1cd9fa362d6c698b17170842b57d44650d0a0c19fd6f41d179f232880a16911d01f56c0e98f61ad7543daf2
7
- data.tar.gz: 3cd51e42ac3cec4465cf10b3a7de4c52cca60268bccc70485a8b9ae7a1087f764e3dfe13abfdade0cbe758a12a5b7ea0cad6edcc90a48348e37e11cd512afda9
6
+ metadata.gz: a5e012eac4960dd31575b95670eef389abee24fce0e5e12111e6d38d401bf2dc518d46c169679609c1108015a8c2762c99f3a84f1a10b8bcdfbe65f2d41c086f
7
+ data.tar.gz: 9b8b8ec138663a5d6a536f5db8b5a743ae870a9ac07addbf5246decbbff84427af676158165a4db15599e2ce9e7ca51572a87aeb7bd59d0be97f55b83e977ab5
@@ -96,20 +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
103
+ @form = Forms::TranslationFileForm.new(params[:import_form])
104
+
104
105
  if @form.valid?
105
106
  begin
106
- TranslationsImportJob.new.perform(
107
+ TranslationsImporter.import(
107
108
  translation_app_id: @form.translation_app_id,
108
- import_file: @form.file.path,
109
+ parsed_file_contents: @form.parsed_file_contents,
109
110
  overwrite_existing: @form.overwrite_existing,
110
111
  mark_inactive_translations: @form.mark_inactive_translations,
111
112
  )
112
- rescue TranslationsImportJob::ImportAbortedError => e
113
+ rescue TranslationsImporter::ImportAbortedError => e
113
114
  flash.now.alert = e.message
114
115
  render
115
116
  return
@@ -117,7 +118,7 @@ module RailsI18nManager
117
118
 
118
119
  redirect_to translations_path, notice: "Import Successful"
119
120
  else
120
- flash.now.alert = "Import not started due to form errors."
121
+ flash.now.alert = "Please see form errors below"
121
122
  render
122
123
  end
123
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,24 +1,18 @@
1
1
  module RailsI18nManager
2
- class TranslationsImportJob < ApplicationJob
2
+ class TranslationsImporter
3
3
 
4
4
  class ImportAbortedError < StandardError; end
5
5
 
6
- def perform(translation_app_id:, import_file:, 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
- if import_file.end_with?(".json")
10
- translations_hash = JSON.parse(File.read(import_file))
11
- else
12
- translations_hash = YAML.safe_load(File.read(import_file))
13
- end
14
-
15
- new_locales = translations_hash.keys - app_record.all_locales
9
+ new_locales = parsed_file_contents.keys - app_record.all_locales
16
10
 
17
11
  if new_locales.any?
18
12
  raise ImportAbortedError.new("Import aborted. Locale not listed in translation app: #{new_locales.join(', ')}")
19
13
  end
20
14
 
21
- all_keys = RailsI18nManager.fetch_flattened_dot_notation_keys(translations_hash)
15
+ all_keys = RailsI18nManager.fetch_flattened_dot_notation_keys(parsed_file_contents)
22
16
 
23
17
  key_records_by_key = app_record.translation_keys.includes(:translation_values).index_by(&:key)
24
18
 
@@ -35,7 +29,18 @@ module RailsI18nManager
35
29
  app_record.all_locales.each do |locale|
36
30
  split_keys = [locale] + key.split(".").map{|x| x}
37
31
 
38
- val = translations_hash.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
39
44
 
40
45
  if val.present?
41
46
  val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s }
data/config/routes.rb CHANGED
@@ -14,8 +14,6 @@ RailsI18nManager::Engine.routes.draw do
14
14
 
15
15
  get "/robots", to: "application#robots", constraints: ->(req){ req.format == :text }
16
16
 
17
- match "*a", to: "application#render_404", via: :get
18
-
19
17
  get "/", to: "translations#index"
20
18
 
21
19
  root "translations#index"
@@ -1,3 +1,3 @@
1
1
  module RailsI18nManager
2
- VERSION = "1.1.0".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.0
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-01-18 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,11 +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/application_job.rb
229
- - app/jobs/rails_i18n_manager/translations_import_job.rb
230
228
  - app/lib/rails_i18n_manager/forms/base.rb
231
229
  - app/lib/rails_i18n_manager/forms/translation_file_form.rb
232
230
  - app/lib/rails_i18n_manager/google_translate.rb
231
+ - app/lib/rails_i18n_manager/translations_importer.rb
233
232
  - app/models/rails_i18n_manager/application_record.rb
234
233
  - app/models/rails_i18n_manager/translation_app.rb
235
234
  - app/models/rails_i18n_manager/translation_key.rb
@@ -1,5 +0,0 @@
1
- module RailsI18nManager
2
- class ApplicationJob < ActiveJob::Base
3
- self.queue_adapter = :async
4
- end
5
- end