fastlane-plugin-translate_gpt 0.1.1 → 0.1.2

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: b7c0d621832f411e542d6c8dad89a1d3014a6d339d2d384d6f1c06af11263804
4
- data.tar.gz: 5feaad22d9a7fdc77dc03420276ba2284946b381e760cdb49297634e51f4eb36
3
+ metadata.gz: d78d5a0107d725ae3b713c7e44f1b3aaca9eff005b1d5d2fb7e4ab75dbca40aa
4
+ data.tar.gz: 3b45a4747d8222ed5d0d9b7d04f178728e679609940e60dff556cbc7b65e1e99
5
5
  SHA512:
6
- metadata.gz: 82b6bdab41b284e766cb3132e05ac232bcc67699d1f6dd1dca6bdfc944a439148d21d0938802a23fa7cc381ac691c705c077d0a97197b506f78039ab07d968a2
7
- data.tar.gz: 90566f6958edcd137faa972a4aa7d3e96ea58ea9cb5fd9d17fa9f2aecf137bf1a3744ae7ee264d1380508aa182aef5be8c02b0532ab5b5825c83da3fa13cf5bf
6
+ metadata.gz: e57f34690dea08a9eb24f7d1f975646c9500fccd983d933ff0b3a8809af91ce3b875e75691dfe7845f9aad88fcaeace425e8ead8dff3e09316c61d03ce5be682
7
+ data.tar.gz: e6e76c62b2ac4fe8b3b16d756bdd2acf4adb8d5ed35a626b9c0e5be3fb4e94511868e961f5261570af63640dafe714cf0d47fc8cc69f520b8e5b552ce27f67b2
data/README.md CHANGED
@@ -15,7 +15,7 @@ fastlane add_plugin translate_gpt
15
15
 
16
16
  ## About translate-gpt
17
17
 
18
- `translate-gpt` is a fastlane plugin that allows you to easily translate your iOS app's strings using the OpenAI GPT API.
18
+ `translate-gpt` is a fastlane plugin that allows you to easily translate your iOS and Android app's strings using the OpenAI GPT API.
19
19
 
20
20
 
21
21
  ## Features
@@ -50,8 +50,8 @@ The following options are available for `translate-gpt`:
50
50
  | `skip_translated` | Whether to skip strings that have already been translated. Defaults to `true`. | `GPT_SKIP_TRANSLATED` |
51
51
  | `source_language` | The source language of the strings to be translated. Defaults to auto-detection. | `GPT_SOURCE_LANGUAGE` |
52
52
  | `target_language` | The target language of the translated strings. Required. | `GPT_TARGET_LANGUAGE` |
53
- | `source_file` | The path to the `Localizable.strings` file to be translated. Defaults to `./Resources/Localizable.strings`. | `GPT_SOURCE_FILE` |
54
- | `target_file` | The path to the output file for the translated strings. Defaults to `./Resources/Localizable.strings.<target_language>`. | `GPT_TARGET_FILE` |
53
+ | `source_file` | The path to the `Localizable.strings` or `strings.xml` file to be translated. | `GPT_SOURCE_FILE` |
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
56
 
57
57
  ## Providing context
@@ -1,6 +1,7 @@
1
1
  require 'fastlane/action'
2
2
  require 'openai'
3
3
  require_relative '../helper/translate_gpt_helper'
4
+ require 'loco_strings'
4
5
 
5
6
  module Fastlane
6
7
  module Actions
@@ -22,9 +23,9 @@ module Fastlane
22
23
 
23
24
  UI.message "Translating #{to_translate.size} strings..."
24
25
 
25
- to_translate.each_with_index do |(key, value), index|
26
- prompt = "Translate the following string from #{params[:source_language]} to #{params[:target_language]}: #{value}"
27
- context = Helper::TranslateGptHelper.get_context(params[:source_file], key)
26
+ to_translate.each_with_index do |(key, string), index|
27
+ prompt = "Translate the following string from #{params[:source_language]} to #{params[:target_language]}: #{string.value}"
28
+ context = string.comment
28
29
  if context && !context.empty?
29
30
  prompt += "\n\nAdditional context:\n#{context}"
30
31
  end
@@ -46,10 +47,11 @@ module Fastlane
46
47
  else
47
48
  target_string = response.dig("choices", 0, "message", "content")
48
49
  if target_string && !target_string.empty?
49
- UI.message "Translating #{key} - #{value} -> #{target_string}"
50
- output_hash[key] = target_string
50
+ UI.message "Translating #{key} - #{string.value} -> #{target_string}"
51
+ string.value = target_string
52
+ output_hash[key] = string
51
53
  else
52
- UI.warning "Unable to translate #{key} - #{value}"
54
+ UI.warning "Unable to translate #{key} - #{string.value}"
53
55
  end
54
56
  end
55
57
  if index < to_translate.size - 1
@@ -59,12 +61,12 @@ module Fastlane
59
61
 
60
62
  UI.message "Writing #{output_hash.size} strings to #{params[:target_file]}..."
61
63
 
62
- # write the output hash to the output file
63
- File.open(params[:target_file], "w") do |file|
64
- output_hash.each do |key, value|
65
- file.puts "\"#{key}\" = \"#{value}\";"
66
- end
64
+ file = LocoStrings.load(params[:target_file])
65
+ file.read
66
+ output_hash.each do |key, value|
67
+ file.update(key, value.value, value.comment)
67
68
  end
69
+ file.write
68
70
  end
69
71
 
70
72
  #####################################################
@@ -1,4 +1,5 @@
1
1
  require 'fastlane_core/ui/ui'
2
+ require 'loco_strings'
2
3
 
3
4
  module Fastlane
4
5
  UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
@@ -9,16 +10,8 @@ module Fastlane
9
10
  # @param localization_file [String] The path to the strings file
10
11
  # @return [Hash] The strings file as a hash
11
12
  def self.get_strings(localization_file)
12
- # Read the strings file into a hash
13
- strings_file = File.read(localization_file)
14
- strings_hash = strings_file.split(/\n/).reduce({}) do |hash, line|
15
- match = line.match(/^"(.+)" = "(.+)";$/)
16
- if match
17
- hash[match[1]] = match[2]
18
- end
19
- hash
20
- end
21
- return strings_hash
13
+ file = LocoStrings.load(localization_file)
14
+ return file.read
22
15
  end
23
16
 
24
17
  # Get the context associated with a localization key
@@ -26,20 +19,9 @@ module Fastlane
26
19
  # @param localization_key [String] The localization key
27
20
  # @return [String] The context associated with the localization key
28
21
  def self.get_context(localization_file, localization_key)
29
- # read the localization file
30
- content = File.read(localization_file)
31
-
32
- # search for the comments associated with the localization key
33
- regex = /^\/\*(.+)\*\/\n"#{localization_key}"/
34
- match = content.match(regex)
35
-
36
- # return the comments, if found
37
- if match && match.captures.size > 0
38
- comments = match.captures[0].strip
39
- return comments.gsub(/\n\s*\*\s?/, "\n").strip
40
- else
41
- return nil
42
- end
22
+ file = LocoStrings.load(localization_file)
23
+ string = file.read[localization_key]
24
+ return string.comment
43
25
  end
44
26
 
45
27
  # Sleep for a specified number of seconds, displaying a progress bar
@@ -1,5 +1,5 @@
1
1
  module Fastlane
2
2
  module TranslateGpt
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
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.1
4
+ version: 0.1.2
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-04-23 00:00:00.000000000 Z
11
+ date: 2023-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-openai
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: loco_strings
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.1
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement