Class: Fastlane::Actions::TranslateGptAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/translate_gpt/actions/translate_gpt_action.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



133
134
135
# File 'lib/fastlane/plugin/translate_gpt/actions/translate_gpt_action.rb', line 133

def self.authors
  ["ftp27"]
end

.available_optionsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fastlane/plugin/translate_gpt/actions/translate_gpt_action.rb', line 30

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :api_token,
      env_name: "GPT_API_KEY",
      description: "API token for ChatGPT",
      sensitive: true,
      code_gen_sensitive: true,
      default_value: ""
    ),
    FastlaneCore::ConfigItem.new(
      key: :model_name,
      env_name: "GPT_MODEL_NAME",
      description: "Name of the ChatGPT model to use",
      default_value: "gpt-3.5-turbo"
    ),
    FastlaneCore::ConfigItem.new(
      key: :request_timeout,
      env_name: "GPT_REQUEST_TIMEOUT",
      description: "Timeout for the request in seconds",
      type: Integer,
      default_value: 30
    ),
    FastlaneCore::ConfigItem.new(
      key: :temperature,
      env_name: "GPT_TEMPERATURE",
      description: "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic",
      type: Float,
      optional: true,
      default_value: 0.5
    ),
    FastlaneCore::ConfigItem.new(
      key: :skip_translated,
      env_name: "GPT_SKIP_TRANSLATED",
      description: "Whether to skip strings that have already been translated",
      type: Boolean,
      optional: true,
      default_value: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :source_language,
      env_name: "GPT_SOURCE_LANGUAGE",
      description: "Source language to translate from",
      default_value: "auto"
    ),
    FastlaneCore::ConfigItem.new(
      key: :target_language,
      env_name: "GPT_TARGET_LANGUAGE",
      description: "Target language to translate to",
      default_value: "en"
    ),
    FastlaneCore::ConfigItem.new(
      key: :source_file,
      env_name: "GPT_SOURCE_FILE",
      description: "The path to the Localizable.strings file to be translated",
      verify_block: proc do |value|
        UI.user_error!("Invalid file path: #{value}") unless File.exist?(value)
        extension = File.extname(value)
        available_extensions = [".strings", ".xcstrings"]
        UI.user_error!("Translation file must have any of these extensions: #{available_extensions}") unless available_extensions.include? extension
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :target_file,
      env_name: "GPT_TARGET_FILE",
      description: "Path to the translation file to update",
      verify_block: proc do |value|
        UI.user_error!("Invalid file path: #{value}") unless File.exist?(value)
        extension = File.extname(value)
        available_extensions = [".strings", ".xcstrings"]
        UI.user_error!("Translation file must have any of these extensions: #{available_extensions}") unless available_extensions.include? extension
      end
    ),    
    FastlaneCore::ConfigItem.new(
      key: :context,
      env_name: "GPT_COMMON_CONTEXT",
      description: "Common context for the translation",
      optional: true,
      type: String
    ), 
    FastlaneCore::ConfigItem.new(
      key: :bunch_size,
      env_name: "GPT_BUNCH_SIZE",
      description: "Number of strings to translate in a single request",
      optional: true,
      type: Integer
    ),                    
  ]
end

.descriptionObject



26
27
28
# File 'lib/fastlane/plugin/translate_gpt/actions/translate_gpt_action.rb', line 26

def self.description
  "Translate a strings file using OpenAI's GPT API"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/fastlane/plugin/translate_gpt/actions/translate_gpt_action.rb', line 137

def self.is_supported?(platform)
  [:ios, :mac].include?(platform)
end

.outputObject



120
121
122
123
124
125
126
# File 'lib/fastlane/plugin/translate_gpt/actions/translate_gpt_action.rb', line 120

def self.output
  [
    ['TRANSLATED_STRING', 'The translated string'],
    ['SOURCE_LANGUAGE', 'The source language of the string'],
    ['TARGET_LANGUAGE', 'The target language of the translation']
  ]
end

.return_valueObject



128
129
130
131
# File 'lib/fastlane/plugin/translate_gpt/actions/translate_gpt_action.rb', line 128

def self.return_value
  # This action doesn't return any specific value, so we return nil
  nil
end

.run(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fastlane/plugin/translate_gpt/actions/translate_gpt_action.rb', line 9

def self.run(params)
  helper = Helper::TranslateGptHelper.new(params)
  helper.prepare_hashes()
  bunch_size = params[:bunch_size] 
  helper.log_input(bunch_size)
  if bunch_size.nil? || bunch_size < 1
    helper.translate_strings()
  else 
    helper.translate_bunch_of_strings(bunch_size)
  end
  helper.write_output()
end