rails_i18n_manager 1.1.0 → 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 +4 -4
- data/app/controllers/rails_i18n_manager/translations_controller.rb +7 -6
- data/app/lib/rails_i18n_manager/forms/translation_file_form.rb +24 -7
- data/app/{jobs/rails_i18n_manager/translations_import_job.rb → lib/rails_i18n_manager/translations_importer.rb} +16 -11
- data/config/routes.rb +0 -2
- data/lib/rails_i18n_manager/version.rb +1 -1
- metadata +3 -4
- data/app/jobs/rails_i18n_manager/application_job.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3398a11d93b08bd8d36e596876a4022bd6317cf45f99f69884652e39a66637ae
|
4
|
+
data.tar.gz: 1c2e294199f783bdd8edffea56742e20de92368e4e12cd386c8203d3fc4d640b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
107
|
+
TranslationsImporter.import(
|
107
108
|
translation_app_id: @form.translation_app_id,
|
108
|
-
|
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
|
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 = "
|
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
|
-
|
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 !
|
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
|
-
|
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
|
2
|
+
class TranslationsImporter
|
3
3
|
|
4
4
|
class ImportAbortedError < StandardError; end
|
5
5
|
|
6
|
-
def
|
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
|
-
|
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(
|
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 =
|
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"
|
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.
|
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-
|
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
|