fastlane-plugin-translate_gpt 0.1.5 → 0.1.7

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: c370098458b2bc0d453de5101fa0a1f3273947cd41bc953bb94cf74f5ad58d6f
4
- data.tar.gz: 96cccf7814b8796119d08340cca72089dea6c48a177df111b52bb2b1196ee756
3
+ metadata.gz: 7e90607f83a3e801607d88f8dd41a7473dd5d1079c93e0e7cb02b784690e25ea
4
+ data.tar.gz: 5bfcd52847bb949ca1b6d54754b32445fed9c8b4bb7c00fc6bc1022f467b0d8d
5
5
  SHA512:
6
- metadata.gz: 5d07f6a7c6a9351b633781d53ddd40e1a432a62bd270d9df8223d40d33b8ae5034123b430db52498a97c462baf5841f7d828df582a9e3cfd4767d83e064ef7ff
7
- data.tar.gz: e5e90093831fa83665f98b302ca433f5e04e5765f74c6a0a0fd5972fcad3dbaf690657e0136acf7ab0f49f253117a5b4deb6ab23b5e209507871b1376d59de2f
6
+ metadata.gz: 2e3db9949838d419d8debc85b602888cfd23b8f5f079d0ef521f1ac2fe7c542867d707664bf99202ec32c500bed32fa49a5cfb0bc7f77edf053b591048377bd9
7
+ data.tar.gz: 5d6add66793788842c1ff153a2556adf7d3de8cf8f97cb5c066a3aeb174dda55ad405f3fd71c3c041ebacf4a7df99185c3dc3e41dcfe2c6c9fd34e3ce27d5c8f
@@ -84,7 +84,9 @@ module Fastlane
84
84
  description: "The path to the Localizable.strings file to be translated",
85
85
  verify_block: proc do |value|
86
86
  UI.user_error!("Invalid file path: #{value}") unless File.exist?(value)
87
- UI.user_error!("Translation file must have .strings extension") unless File.extname(value) == ".strings"
87
+ extension = File.extname(value)
88
+ available_extensions = [".strings", ".xcstrings"]
89
+ UI.user_error!("Translation file must have any of these extensions: #{available_extensions}") unless available_extensions.include? extension
88
90
  end
89
91
  ),
90
92
  FastlaneCore::ConfigItem.new(
@@ -93,7 +95,9 @@ module Fastlane
93
95
  description: "Path to the translation file to update",
94
96
  verify_block: proc do |value|
95
97
  UI.user_error!("Invalid file path: #{value}") unless File.exist?(value)
96
- UI.user_error!("Translation file must have .strings extension") unless File.extname(value) == ".strings"
98
+ extension = File.extname(value)
99
+ available_extensions = [".strings", ".xcstrings"]
100
+ UI.user_error!("Translation file must have any of these extensions: #{available_extensions}") unless available_extensions.include? extension
97
101
  end
98
102
  ),
99
103
  FastlaneCore::ConfigItem.new(
@@ -16,13 +16,30 @@ module Fastlane
16
16
  @timeout = params[:request_timeout]
17
17
  end
18
18
 
19
- # Get the strings from a file
20
- def prepare_hashes()
19
+ def prepare_xcstrings()
20
+ @xcfile = LocoStrings.load(@params[:source_file])
21
+ @output_hash = {}
22
+ @to_translate = @xcfile.read
23
+ if @params[:skip_translated] == true
24
+ @to_translate = @to_translate.reject { |k, v| @xcfile.value(v.key, @params[:target_language]) }
25
+ end
26
+ end
27
+
28
+ def prepare_strings()
21
29
  @input_hash = get_strings(@params[:source_file])
22
30
  @output_hash = get_strings(@params[:target_file])
23
31
  @to_translate = filter_translated(@params[:skip_translated], @input_hash, @output_hash)
24
32
  end
25
33
 
34
+ # Get the strings from a file
35
+ def prepare_hashes()
36
+ if File.extname(@params[:source_file]) == ".xcstrings"
37
+ prepare_xcstrings()
38
+ else
39
+ prepare_strings()
40
+ end
41
+ end
42
+
26
43
  # Log information about the input strings
27
44
  def log_input(bunch_size)
28
45
  @translation_count = @to_translate.size
@@ -66,6 +83,7 @@ module Fastlane
66
83
  def translate_bunch_of_strings(bunch_size)
67
84
  bunch_index = 0
68
85
  number_of_bunches = (@translation_count / bunch_size.to_f).ceil
86
+ @keys_associations = {}
69
87
  @to_translate.each_slice(bunch_size) do |bunch|
70
88
  prompt = prepare_bunch_prompt bunch
71
89
  max_retries = 10
@@ -109,7 +127,7 @@ module Fastlane
109
127
  def prepare_bunch_prompt(strings)
110
128
  prompt = "I want you to act as a translator for a mobile application strings. " + \
111
129
  "Try to keep length of the translated text. " + \
112
- "You need to answer only with the translation and nothing else until I say to stop it."
130
+ "You need to response with a JSON only with the translation and nothing else until I say to stop it. "
113
131
  if @params[:context] && !@params[:context].empty?
114
132
  prompt += "This app is #{@params[:context]}. "
115
133
  end
@@ -122,7 +140,9 @@ module Fastlane
122
140
  if context && !context.empty?
123
141
  string_hash["context"] = context
124
142
  end
125
- string_hash["key"] = string.key
143
+ key = transform_string(string.key)
144
+ @keys_associations[key] = string.key
145
+ string_hash["key"] = key
126
146
  string_hash["string_to_translate"] = string.value
127
147
  json_hash << string_hash
128
148
  end
@@ -132,6 +152,12 @@ module Fastlane
132
152
  return prompt
133
153
  end
134
154
 
155
+ def transform_string(input_string)
156
+ uppercased_string = input_string.upcase
157
+ escaped_string = uppercased_string.gsub(/[^0-9a-zA-Z]+/, '_')
158
+ return escaped_string
159
+ end
160
+
135
161
  # Request a translation from the GPT API
136
162
  def request_translate(key, string, prompt, index)
137
163
  response = @client.chat(
@@ -181,7 +207,13 @@ module Fastlane
181
207
  else
182
208
  target_string = response.dig("choices", 0, "message", "content")
183
209
  json_string = target_string[/\[[^\[\]]*\]/m]
184
- json_hash = JSON.parse(json_string)
210
+ begin
211
+ json_hash = JSON.parse(json_string)
212
+ rescue => error
213
+ UI.error "#{index_log} Error parsing JSON: #{error}"
214
+ UI.error "#{index_log} JSON: \"#{json_string}\""
215
+ return
216
+ end
185
217
  keys_to_translate = json_hash.map { |string_hash| string_hash["key"] }
186
218
  json_hash.each do |string_hash|
187
219
  key = string_hash["key"]
@@ -190,9 +222,10 @@ module Fastlane
190
222
  string_hash.delete("context")
191
223
  translated_string = string_hash.values.first
192
224
  if key && !key.empty? && translated_string && !translated_string.empty?
193
- UI.message "#{index_log} Translating #{key} - #{translated_string}"
194
- string = LocoStrings::LocoString.new(key, translated_string, context)
195
- @output_hash[key] = string
225
+ real_key = @keys_associations[key]
226
+ UI.message "#{index_log} Translating #{real_key} - #{translated_string}"
227
+ string = LocoStrings::LocoString.new(real_key, translated_string, context)
228
+ @output_hash[real_key] = string
196
229
  keys_to_translate.delete(key)
197
230
  end
198
231
  end
@@ -209,12 +242,20 @@ module Fastlane
209
242
  target_string = Colorizer::colorize(@params[:target_file], :white)
210
243
  UI.message "Writing #{number_of_strings} strings to #{target_string}..."
211
244
 
212
- file = LocoStrings.load(@params[:target_file])
213
- file.read
214
- @output_hash.each do |key, value|
215
- file.update(key, value.value, value.comment)
245
+ if @xcfile.nil?
246
+ file = LocoStrings.load(@params[:target_file])
247
+ file.read
248
+ @output_hash.each do |key, value|
249
+ file.update(key, value.value, value.comment)
250
+ end
251
+ file.write
252
+ else
253
+ @xcfile.update_file_path(@params[:target_file])
254
+ @output_hash.each do |key, value|
255
+ @xcfile.update(key, value.value, value.comment, @params[:target_language])
256
+ end
257
+ @xcfile.write
216
258
  end
217
- file.write
218
259
  end
219
260
 
220
261
  # Read the strings file into a hash
@@ -1,5 +1,5 @@
1
1
  module Fastlane
2
2
  module TranslateGpt
3
- VERSION = "0.1.5"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastlane-plugin-translate_gpt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Cherepanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-02 00:00:00.000000000 Z
11
+ date: 2023-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-openai
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.1.1
33
+ version: 0.1.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.1.1
40
+ version: 0.1.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -212,7 +212,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
212
  - !ruby/object:Gem::Version
213
213
  version: '0'
214
214
  requirements: []
215
- rubygems_version: 3.4.13
215
+ rubygems_version: 3.4.19
216
216
  signing_key:
217
217
  specification_version: 4
218
218
  summary: This fastlane plugin provides an easy way to use the OpenAI GPT language