Skip to content

Commit 286391a

Browse files
committed
Bunch translate prompt fixed. Exceptions handling while json parsing added
1 parent 45f4b2e commit 286391a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/fastlane/plugin/translate_gpt/helper/translate_gpt_helper.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def prepare_prompt(string)
109109
def prepare_bunch_prompt(strings)
110110
prompt = "I want you to act as a translator for a mobile application strings. " + \
111111
"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."
112+
"You need to response with a JSON only with the translation and nothing else until I say to stop it."
113113
if @params[:context] && !@params[:context].empty?
114114
prompt += "This app is #{@params[:context]}. "
115115
end
@@ -181,7 +181,13 @@ def request_bunch_translate(strings, prompt, index, number_of_bunches)
181181
else
182182
target_string = response.dig("choices", 0, "message", "content")
183183
json_string = target_string[/\[[^\[\]]*\]/m]
184-
json_hash = JSON.parse(json_string)
184+
begin
185+
json_hash = JSON.parse(json_string)
186+
rescue => error
187+
UI.error "#{index_log} Error parsing JSON: #{error}"
188+
UI.error "#{index_log} JSON: \"#{json_string}\""
189+
return
190+
end
185191
keys_to_translate = json_hash.map { |string_hash| string_hash["key"] }
186192
json_hash.each do |string_hash|
187193
key = string_hash["key"]

0 commit comments

Comments
 (0)