Class: Fastlane::Helper::TranslateGptReleaseNotesHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/translate_gpt_release_notes/helper/translate_gpt_release_notes_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ TranslateGptReleaseNotesHelper

Returns a new instance of TranslateGptReleaseNotesHelper.


10
11
12
13
14
15
16
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/translate_gpt_release_notes_helper.rb', line 10

def initialize(params)
  @params = params
  @client = OpenAI::Client.new(
    access_token: params[:api_token],
    request_timeout: params[:request_timeout]
  )
end

Instance Method Details

#translate_text(text, target_locale, platform) ⇒ Object

Request a translation from the GPT API


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/translate_gpt_release_notes_helper.rb', line 19

def translate_text(text, target_locale, platform)
  source_locale = @params[:master_locale]
  prompt = "Translate this text from #{source_locale} to #{target_locale}:\n#{text}"


  # Add condition for Android platform
  if platform == 'android'
    prompt += "\n\nNote: The length of the translated text should be 500 symbols maximum. Rephrase a little if needed."
  end

  # Context handling
  if @params[:context] && !@params[:context].empty?
    prompt = "Context: #{@params[:context]}\n" + prompt
  end

  # Updated API call with max_tokens
  response = @client.chat(
    parameters: {
      model: @params[:model_name] || 'gpt-4-1106-preview',
      messages: [{ role: "user", content: prompt }],
      temperature: @params[:temperature] || 0.5
    }
  )


  error = response.dig("error", "message")
  if error
    UI.error "Error translating text: #{error}"
    return nil
  else
    translated_text = response.dig("choices", 0, "message", "content").strip
    UI.message "Translated text: #{translated_text}"
    return translated_text
  end
end

#wait(seconds = ) ⇒ Object

Sleep for a specified number of seconds, displaying a progress bar


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/translate_gpt_release_notes_helper.rb', line 56

def wait(seconds = @params[:request_timeout])
  sleep_time = 0
  while sleep_time < seconds
    percent_complete = (sleep_time.to_f / seconds.to_f) * 100.0
    progress_bar_width = 20
    completed_width = (progress_bar_width * percent_complete / 100.0).round
    remaining_width = progress_bar_width - completed_width
    print "\rTimeout ["
    print Colorizer::code(:green)
    print "=" * completed_width
    print " " * remaining_width
    print Colorizer::code(:reset)
    print "]"
    print " %.2f%%" % percent_complete
    $stdout.flush
    sleep(1)
    sleep_time += 1
  end
  print "\r"
  $stdout.flush
end