translation 1.20 → 1.21

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: 39f833e4f98c4424feca4172a7f70c3b9639f24df3aaaa8e873632a54a5bb290
4
- data.tar.gz: c76f080ced71805a079f47f6475412f1a1f4b24574e6470e77aa0d924ffd4211
3
+ metadata.gz: 8ce80b91f3c556815df4339c5fc0b98f5749dd2babb378826053fe99751e5aec
4
+ data.tar.gz: 1d4126693818e8220f41fa74f56e4d4eec61df651ac52f6429930fda9294a5b6
5
5
  SHA512:
6
- metadata.gz: 40fb09a74db12309e1dc787cc9ced44874cbc96250d6eb62cafe6b8f63962557756560f70c64f33e0e520a12a87bff14809b04b8a86c593fe4c1f48363977272
7
- data.tar.gz: 834e92a56f1d11c178905db2326e346f9108f3237bf83645a59605680344878f15a9c79d5d22295fa05804fc3374942b7999852848affb82dfa55c1f5057b5dc
6
+ metadata.gz: b17f56630fba322a948c31b84fb7ed006ef647f4cba40fe2f7ef78fd63e470f5dbbfd01f1ceb31627caabda3d27c0ea6e6c441f377709cc4291dbda07d5f4dc4
7
+ data.tar.gz: cf424585ff16540e92679195faaa0dff8db5b30faa1b9355ed203fa0e07f1d5a114c643a071f22c4afb7545d2e82d553cd7012a88200319679528a870603bbc6
@@ -12,72 +12,113 @@ module TranslationIO
12
12
 
13
13
  params.merge!({ :timestamp => metadata_timestamp })
14
14
  parsed_response = perform_source_edits_request(params)
15
+ source_edits = parsed_response['source_edits'].to_a
15
16
 
16
- unless parsed_response.nil?
17
- TranslationIO.info "Applying YAML source editions."
17
+ TranslationIO.info "Applying YAML source editions."
18
18
 
19
- parsed_response['source_edits'].each do |source_edit|
20
- inserted = false
19
+ source_edits.each do |source_edit|
20
+ applied = false
21
21
 
22
- sort_by_project_locales_first(@yaml_file_paths).each do |file_path|
23
- yaml_hash = YAML::load(File.read(file_path))
24
- flat_yaml_hash = FlatHash.to_flat_hash(yaml_hash)
22
+ reload_or_reuse_yaml_sources
25
23
 
26
- flat_yaml_hash.each do |key, value|
27
- if key == "#{@source_locale}.#{source_edit['key']}"
28
- if value == source_edit['old_text']
29
- TranslationIO.info "#{source_edit['key']} | #{source_edit['old_text']} -> #{source_edit['new_text']}", 2, 2
24
+ @yaml_sources.each do |yaml_source|
25
+ yaml_file_path = yaml_source[:yaml_file_path]
26
+ yaml_flat_hash = yaml_source[:yaml_flat_hash]
30
27
 
31
- if locale_file_path_in_project?(file_path)
32
- flat_yaml_hash[key] = source_edit['new_text']
28
+ yaml_flat_hash.each do |full_key, value|
29
+ if full_key == "#{@source_locale}.#{source_edit['key']}"
30
+ apply_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
31
+ applied = true
32
+ break
33
+ end
34
+ end
33
35
 
34
- file_content = to_hash_to_yaml(flat_yaml_hash)
36
+ break if applied
37
+ end
38
+ end
35
39
 
36
- File.open(file_path, 'w') do |f|
37
- f.write(file_content)
38
- end
39
- else # override source text of gem
40
- yaml_path = File.join(TranslationIO.config.yaml_locales_path, "#{@source_locale}.yml")
40
+ update_metadata_timestamp
41
+ end
41
42
 
42
- if File.exists?(yaml_path) # source yaml file
43
- yaml_hash = YAML::load(File.read(yaml_path))
44
- flat_yaml_hash = FlatHash.to_flat_hash(yaml_hash)
45
- else
46
- FileUtils::mkdir_p File.dirname(yaml_path)
47
- flat_yaml_hash = {}
48
- end
43
+ private
49
44
 
50
- flat_yaml_hash["#{@source_locale}.#{source_edit['key']}"] = source_edit['new_text']
45
+ def reload_or_reuse_yaml_sources
46
+ if yaml_sources_reload_needed?
47
+ @yaml_sources = sort_by_project_locales_first(@yaml_file_paths).collect do |yaml_file_path|
48
+ yaml_content = File.read(yaml_file_path)
49
+ yaml_hash = YAML::load(yaml_content)
50
+ yaml_flat_hash = FlatHash.to_flat_hash(yaml_hash)
51
+
52
+ {
53
+ :yaml_file_path => yaml_file_path,
54
+ :yaml_flat_hash => yaml_flat_hash
55
+ }
56
+ end
57
+ else
58
+ @yaml_sources
59
+ end
60
+ end
51
61
 
52
- file_content = to_hash_to_yaml(flat_yaml_hash)
62
+ def yaml_sources_reload_needed?
63
+ @yaml_file_paths.sort != @yaml_sources.to_a.collect { |y_s| y_s[:yaml_file_path] }.sort
64
+ end
65
+
66
+ # Sort YAML file paths by project locales first, gem locales after
67
+ # (to replace "overridden" source first)
68
+ def sort_by_project_locales_first(yaml_file_paths)
69
+ yaml_file_paths.sort do |x, y|
70
+ a = locale_file_path_in_project?(x)
71
+ b = locale_file_path_in_project?(y)
72
+ (!a && b) ? 1 : ((a && !b) ? -1 : 0)
73
+ end
74
+ end
53
75
 
54
- File.open(yaml_path, 'w') do |f|
55
- f.write(file_content)
56
- end
57
- end
76
+ def apply_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
77
+ full_key = "#{@source_locale}.#{source_edit['key']}"
58
78
 
59
- inserted = true
60
- break
61
- else
62
- TranslationIO.info "#{source_edit['key']} | Ignored because translation was also updated in source YAML file", 2, 2
63
- end
64
- end
65
- end
79
+ if yaml_flat_hash[full_key] == source_edit['old_text']
80
+ TranslationIO.info "#{source_edit['key']} | #{source_edit['old_text']} -> #{source_edit['new_text']}", 2, 2
66
81
 
67
- break if inserted
68
- end
82
+ if locale_file_path_in_project?(yaml_file_path)
83
+ apply_application_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
84
+ else # Override source text of gem inside the app
85
+ apply_gem_source_edit(source_edit)
69
86
  end
87
+ else
88
+ TranslationIO.info "#{source_edit['key']} | #{source_edit['old_text']} -> #{source_edit['new_text']} | Ignored because translation was also updated in source YAML file", 2, 2
70
89
  end
90
+ end
71
91
 
72
- File.open(TranslationIO.config.metadata_path, 'w') do |f|
73
- f.write({ 'timestamp' => Time.now.utc.to_i }.to_yaml)
92
+ def apply_application_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
93
+ full_key = "#{@source_locale}.#{source_edit['key']}"
94
+ yaml_flat_hash[full_key] = source_edit['new_text']
95
+ file_content = to_hash_to_yaml(yaml_flat_hash)
96
+
97
+ File.open(yaml_file_path, 'w') do |f|
98
+ f.write(file_content)
74
99
  end
75
100
  end
76
101
 
77
- private
102
+ def apply_gem_source_edit(source_edit)
103
+ # Source yaml file like config/locales/en.yml
104
+ yaml_file_path = File.expand_path(File.join(TranslationIO.config.yaml_locales_path, "#{@source_locale}.yml"))
78
105
 
79
- def to_hash_to_yaml(flat_yaml_hash)
80
- yaml_hash = FlatHash.to_hash(flat_yaml_hash)
106
+ if File.exists?(yaml_file_path)
107
+ # Complete existing hash if YAML file already exists
108
+ existing_yaml_source = @yaml_sources.detect { |y_s| normalize_path(y_s[:yaml_file_path]) == normalize_path(yaml_file_path) }
109
+ yaml_flat_hash = existing_yaml_source[:yaml_flat_hash]
110
+ else
111
+ # Create new hash if YAML file doesn't exist yet
112
+ FileUtils::mkdir_p File.dirname(yaml_file_path)
113
+ yaml_flat_hash = {}
114
+ @yaml_file_paths.push(yaml_file_path) # Application YAML are at the end of the list
115
+ end
116
+
117
+ apply_application_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
118
+ end
119
+
120
+ def to_hash_to_yaml(yaml_flat_hash)
121
+ yaml_hash = FlatHash.to_hash(yaml_flat_hash)
81
122
 
82
123
  if TranslationIO.config.yaml_line_width
83
124
  content = yaml_hash.to_yaml(:line_width => TranslationIO.config.yaml_line_width)
@@ -103,26 +144,26 @@ module TranslationIO
103
144
  end
104
145
  end
105
146
 
147
+ def update_metadata_timestamp
148
+ File.open(TranslationIO.config.metadata_path, 'w') do |f|
149
+ f.write({ 'timestamp' => Time.now.utc.to_i }.to_yaml)
150
+ end
151
+ end
152
+
106
153
  def perform_source_edits_request(params)
107
154
  uri = URI("#{TranslationIO.client.endpoint}/projects/#{TranslationIO.client.api_key}/source_edits")
108
155
  parsed_response = BaseOperation.perform_request(uri, params)
109
156
  end
110
157
 
111
- # Sort YAML file paths by project locales first, gem locales after
112
- # (to replace "overridden" source first)
113
- def sort_by_project_locales_first(yaml_file_paths)
114
- yaml_file_paths.sort do |x, y|
115
- a = locale_file_path_in_project?(x)
116
- b = locale_file_path_in_project?(y)
117
- (!a && b) ? 1 : ((a && !b) ? -1 : 0)
118
- end
119
- end
120
-
121
158
  def locale_file_path_in_project?(locale_file_path)
122
- TranslationIO.normalize_path(locale_file_path).start_with?(
123
- TranslationIO.normalize_path(TranslationIO.config.yaml_locales_path)
159
+ normalize_path(locale_file_path).start_with?(
160
+ normalize_path(TranslationIO.config.yaml_locales_path)
124
161
  )
125
162
  end
163
+
164
+ def normalize_path(path)
165
+ TranslationIO.normalize_path(path)
166
+ end
126
167
  end
127
168
  end
128
169
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translation
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.20'
4
+ version: '1.21'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Hoste
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-10-10 00:00:00.000000000 Z
12
+ date: 2019-12-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gettext