fastlane-plugin-translate_gpt 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be8140845fda90144346f59a44239c4baa066fc8b75f1afc26655783be3e3686
|
4
|
+
data.tar.gz: 0a39de422d2b3760c4678df1053a61799cd444434399d8f3f0f22a9dfcf261b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a60eb486cf4a385386e429d85d6d53e833ff3afb80c8f60dae482a6596f55fce3218e6268669a4ad4f60e7868244ffaf62a772e2e481fd91e168e18d746f1dd2
|
7
|
+
data.tar.gz: e4a68b0fa334c61c03dc8fdb26fcab4a1310fa9ca877f57522e21d138ea754c937d9ce11263e9dde2d184a23f05d76fc91566c4994167847dd32f2c220bd69a2
|
data/README.md
CHANGED
@@ -53,6 +53,9 @@ The following options are available for `translate-gpt`:
|
|
53
53
|
| `source_file` | The path to the `Localizable.strings` or `strings.xml` file to be translated. | `GPT_SOURCE_FILE` |
|
54
54
|
| `target_file` | The path to the output file for the translated strings. | `GPT_TARGET_FILE` |
|
55
55
|
| `context` | Common context for the translation | `GPT_COMMON_CONTEXT` |
|
56
|
+
| `bunch_size` | Number of strings to translate in a single request.| `GPT_BUNCH_SIZE` |
|
57
|
+
|
58
|
+
**Note:** __I advise using `bunch_size`. It will reduce the number of API requests and translations will be more accurate.__
|
56
59
|
|
57
60
|
## Providing context
|
58
61
|
|
@@ -9,8 +9,13 @@ module Fastlane
|
|
9
9
|
def self.run(params)
|
10
10
|
helper = Helper::TranslateGptHelper.new(params)
|
11
11
|
helper.prepare_hashes()
|
12
|
-
|
13
|
-
helper.
|
12
|
+
bunch_size = params[:bunch_size]
|
13
|
+
helper.log_input(bunch_size)
|
14
|
+
if bunch_size.nil? || bunch_size < 1
|
15
|
+
helper.translate_strings()
|
16
|
+
else
|
17
|
+
helper.translate_bunch_of_strings(bunch_size)
|
18
|
+
end
|
14
19
|
helper.write_output()
|
15
20
|
end
|
16
21
|
|
@@ -97,7 +102,14 @@ module Fastlane
|
|
97
102
|
description: "Common context for the translation",
|
98
103
|
optional: true,
|
99
104
|
type: String
|
100
|
-
)
|
105
|
+
),
|
106
|
+
FastlaneCore::ConfigItem.new(
|
107
|
+
key: :bunch_size,
|
108
|
+
env_name: "GPT_BUNCH_SIZE",
|
109
|
+
description: "Number of strings to translate in a single request",
|
110
|
+
optional: true,
|
111
|
+
type: Integer
|
112
|
+
),
|
101
113
|
]
|
102
114
|
end
|
103
115
|
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'fastlane_core/ui/ui'
|
2
2
|
require 'loco_strings'
|
3
|
+
require 'json'
|
3
4
|
|
4
5
|
module Fastlane
|
5
6
|
UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
|
@@ -23,13 +24,17 @@ module Fastlane
|
|
23
24
|
end
|
24
25
|
|
25
26
|
# Log information about the input strings
|
26
|
-
def log_input()
|
27
|
+
def log_input(bunch_size)
|
27
28
|
@translation_count = @to_translate.size
|
28
29
|
number_of_strings = Colorizer::colorize("#{@translation_count}", :blue)
|
29
30
|
UI.message "Translating #{number_of_strings} strings..."
|
30
|
-
if
|
31
|
+
if bunch_size.nil? || bunch_size < 1
|
31
32
|
estimated_string = Colorizer::colorize("#{@translation_count * @params[:request_timeout]}", :white)
|
32
33
|
UI.message "Estimated time: #{estimated_string} seconds"
|
34
|
+
else
|
35
|
+
number_of_bunches = (@translation_count / bunch_size.to_f).ceil
|
36
|
+
estimated_string = Colorizer::colorize("#{number_of_bunches * @params[:request_timeout]}", :white)
|
37
|
+
UI.message "Estimated time: #{estimated_string} seconds"
|
33
38
|
end
|
34
39
|
end
|
35
40
|
|
@@ -58,6 +63,32 @@ module Fastlane
|
|
58
63
|
end
|
59
64
|
end
|
60
65
|
|
66
|
+
def translate_bunch_of_strings(bunch_size)
|
67
|
+
bunch_index = 0
|
68
|
+
number_of_bunches = (@translation_count / bunch_size.to_f).ceil
|
69
|
+
@to_translate.each_slice(bunch_size) do |bunch|
|
70
|
+
prompt = prepare_bunch_prompt bunch
|
71
|
+
max_retries = 10
|
72
|
+
times_retried = 0
|
73
|
+
|
74
|
+
# translate the source string to the target language
|
75
|
+
begin
|
76
|
+
request_bunch_translate(bunch, prompt, bunch_index, number_of_bunches)
|
77
|
+
bunch_index += 1
|
78
|
+
rescue Net::ReadTimeout => error
|
79
|
+
if times_retried < max_retries
|
80
|
+
times_retried += 1
|
81
|
+
UI.important "Failed to request translation, retry #{times_retried}/#{max_retries}"
|
82
|
+
wait 1
|
83
|
+
retry
|
84
|
+
else
|
85
|
+
UI.error "Can't translate #{key}: #{error}"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
if bunch_index < number_of_bunches - 1 then wait end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
61
92
|
# Prepare the prompt for the GPT API
|
62
93
|
def prepare_prompt(string)
|
63
94
|
prompt = "I want you to act as a translator for a mobile application strings. " + \
|
@@ -75,6 +106,32 @@ module Fastlane
|
|
75
106
|
return prompt
|
76
107
|
end
|
77
108
|
|
109
|
+
def prepare_bunch_prompt(strings)
|
110
|
+
prompt = "I want you to act as a translator for a mobile application strings. " + \
|
111
|
+
"Try to keep length of the translated text. " + \
|
112
|
+
"You need to response with a JSON only with the translation and nothing else until I say to stop it."
|
113
|
+
if @params[:context] && !@params[:context].empty?
|
114
|
+
prompt += "This app is #{@params[:context]}. "
|
115
|
+
end
|
116
|
+
prompt += "Translate next text from #{@params[:source_language]} to #{@params[:target_language]}:\n"
|
117
|
+
|
118
|
+
json_hash = []
|
119
|
+
strings.each do |key, string|
|
120
|
+
string_hash = {}
|
121
|
+
context = string.comment
|
122
|
+
if context && !context.empty?
|
123
|
+
string_hash["context"] = context
|
124
|
+
end
|
125
|
+
string_hash["key"] = string.key
|
126
|
+
string_hash["string_to_translate"] = string.value
|
127
|
+
json_hash << string_hash
|
128
|
+
end
|
129
|
+
prompt += "'''\n"
|
130
|
+
prompt += json_hash.to_json
|
131
|
+
prompt += "\n'''"
|
132
|
+
return prompt
|
133
|
+
end
|
134
|
+
|
78
135
|
# Request a translation from the GPT API
|
79
136
|
def request_translate(key, string, prompt, index)
|
80
137
|
response = @client.chat(
|
@@ -104,6 +161,54 @@ module Fastlane
|
|
104
161
|
end
|
105
162
|
end
|
106
163
|
|
164
|
+
def request_bunch_translate(strings, prompt, index, number_of_bunches)
|
165
|
+
response = @client.chat(
|
166
|
+
parameters: {
|
167
|
+
model: @params[:model_name],
|
168
|
+
messages: [
|
169
|
+
{ role: "user", content: prompt }
|
170
|
+
],
|
171
|
+
temperature: @params[:temperature],
|
172
|
+
}
|
173
|
+
)
|
174
|
+
# extract the translated string from the response
|
175
|
+
error = response.dig("error", "message")
|
176
|
+
|
177
|
+
#key_log = Colorizer::colorize(key, :blue)
|
178
|
+
index_log = Colorizer::colorize("[#{index + 1}/#{number_of_bunches}]", :white)
|
179
|
+
if error
|
180
|
+
UI.error "#{index_log} Error translating: #{error}"
|
181
|
+
else
|
182
|
+
target_string = response.dig("choices", 0, "message", "content")
|
183
|
+
json_string = target_string[/\[[^\[\]]*\]/m]
|
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
|
191
|
+
keys_to_translate = json_hash.map { |string_hash| string_hash["key"] }
|
192
|
+
json_hash.each do |string_hash|
|
193
|
+
key = string_hash["key"]
|
194
|
+
context = string_hash["context"]
|
195
|
+
string_hash.delete("key")
|
196
|
+
string_hash.delete("context")
|
197
|
+
translated_string = string_hash.values.first
|
198
|
+
if key && !key.empty? && translated_string && !translated_string.empty?
|
199
|
+
UI.message "#{index_log} Translating #{key} - #{translated_string}"
|
200
|
+
string = LocoStrings::LocoString.new(key, translated_string, context)
|
201
|
+
@output_hash[key] = string
|
202
|
+
keys_to_translate.delete(key)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
if keys_to_translate.length > 0
|
207
|
+
UI.important "#{index_log} Unable to translate #{keys_to_translate.join(", ")}"
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
107
212
|
# Write the translated strings to the target file
|
108
213
|
def write_output()
|
109
214
|
number_of_strings = Colorizer::colorize("#{@output_hash.size}", :blue)
|
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.
|
4
|
+
version: 0.1.6
|
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-
|
11
|
+
date: 2023-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-openai
|
@@ -193,7 +193,10 @@ files:
|
|
193
193
|
homepage: https://github.com/ftp27/fastlane-plugin-translate_gpt
|
194
194
|
licenses:
|
195
195
|
- MIT
|
196
|
-
metadata:
|
196
|
+
metadata:
|
197
|
+
homepage_uri: https://github.com/ftp27/fastlane-plugin-translate_gpt
|
198
|
+
source_code_uri: https://github.com/ftp27/fastlane-plugin-translate_gpt
|
199
|
+
github_repo: https://github.com/ftp27/fastlane-plugin-translate_gpt
|
197
200
|
post_install_message:
|
198
201
|
rdoc_options: []
|
199
202
|
require_paths:
|