web_translate_it 2.5.3 → 2.6.1
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 +4 -4
- data/bin/wti +107 -107
- data/generators/webtranslateit/lib/insert_commands.rb +6 -8
- data/generators/webtranslateit/webtranslateit_generator.rb +6 -6
- data/history.md +22 -7
- data/lib/web_translate_it/auto_fetch.rb +2 -4
- data/lib/web_translate_it/command_line.rb +264 -230
- data/lib/web_translate_it/configuration.rb +32 -29
- data/lib/web_translate_it/connection.rb +22 -34
- data/lib/web_translate_it/project.rb +22 -25
- data/lib/web_translate_it/string.rb +85 -84
- data/lib/web_translate_it/term.rb +65 -65
- data/lib/web_translate_it/term_translation.rb +34 -36
- data/lib/web_translate_it/translation.rb +25 -32
- data/lib/web_translate_it/translation_file.rb +109 -106
- data/lib/web_translate_it/util/array_util.rb +8 -8
- data/lib/web_translate_it/util/hash_util.rb +5 -5
- data/lib/web_translate_it/util/string_util.rb +5 -8
- data/lib/web_translate_it/util.rb +51 -54
- data/lib/web_translate_it.rb +7 -7
- data/readme.md +13 -9
- data/spec/spec_helper.rb +2 -3
- data/spec/web_translate_it/auto_fetch_spec.rb +1 -2
- data/spec/web_translate_it/string_spec.rb +61 -62
- data/spec/web_translate_it/term_spec.rb +40 -41
- metadata +47 -12
@@ -1,6 +1,4 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module WebTranslateIt
|
3
|
-
|
4
2
|
# Handles the configuration of your project, both via the the configuration file
|
5
3
|
# and via the API.
|
6
4
|
# Implementation example, assuming you have a valid .wti file:
|
@@ -11,49 +9,47 @@ module WebTranslateIt
|
|
11
9
|
require 'yaml'
|
12
10
|
require 'fileutils'
|
13
11
|
require 'erb'
|
14
|
-
attr_accessor :path, :api_key, :source_locale, :target_locales, :files, :ignore_locales, :needed_locales
|
15
|
-
|
16
|
-
|
12
|
+
attr_accessor :path, :api_key, :source_locale, :target_locales, :files, :ignore_locales, :needed_locales, :logger, :before_pull, :after_pull, :before_push, :after_push, :project_name, :path_to_config_file, :ignore_files
|
13
|
+
|
17
14
|
# Load configuration file from the path.
|
18
|
-
def initialize(root_path = Rails.root, path_to_config_file =
|
15
|
+
def initialize(root_path = Rails.root, path_to_config_file = '.wti') # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
|
19
16
|
self.path_to_config_file = path_to_config_file
|
20
17
|
self.path = root_path
|
21
18
|
self.logger = logger
|
22
|
-
if File.
|
23
|
-
self.api_key = ENV[
|
19
|
+
if File.exist?(File.expand_path(path_to_config_file, path))
|
20
|
+
self.api_key = ENV['WTI_PROJECT_API_KEY'] || configuration['api_key']
|
24
21
|
self.before_pull = configuration['before_pull']
|
25
22
|
self.after_pull = configuration['after_pull']
|
26
23
|
self.before_push = configuration['before_push']
|
27
24
|
self.after_push = configuration['after_push']
|
25
|
+
self.ignore_files = configuration['ignore_files']
|
28
26
|
project_info = if RUBY_VERSION >= '3.1.0'
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
YAML.safe_load WebTranslateIt::Project.fetch_info(api_key), permitted_classes: [Time]
|
28
|
+
else
|
29
|
+
YAML.load WebTranslateIt::Project.fetch_info(api_key)
|
30
|
+
end
|
33
31
|
set_locales_to_ignore(configuration)
|
34
32
|
set_locales_needed(configuration)
|
35
33
|
set_files(project_info['project'])
|
36
34
|
set_locales(project_info['project'])
|
37
|
-
WebTranslateIt::Connection.turn_silent_on if configuration['silence_errors']
|
38
35
|
self.project_name = project_info['project']['name']
|
39
36
|
else
|
40
|
-
puts StringUtil.failure("\nNo configuration file found in #{File.expand_path(path_to_config_file,
|
37
|
+
puts StringUtil.failure("\nNo configuration file found in #{File.expand_path(path_to_config_file, path)}")
|
41
38
|
exit(1)
|
42
39
|
end
|
43
40
|
end
|
44
|
-
|
41
|
+
|
45
42
|
# Reload project data
|
46
43
|
#
|
47
44
|
def reload
|
48
|
-
project_info = YAML.load WebTranslateIt::Project.fetch_info(
|
45
|
+
project_info = YAML.load WebTranslateIt::Project.fetch_info(api_key)
|
49
46
|
set_locales_to_ignore(configuration)
|
50
47
|
set_locales_needed(configuration)
|
51
48
|
set_files(project_info['project'])
|
52
49
|
set_locales(project_info['project'])
|
53
|
-
WebTranslateIt::Connection.turn_silent_on if configuration['silence_errors']
|
54
50
|
self.project_name = project_info['project']['name']
|
55
51
|
end
|
56
|
-
|
52
|
+
|
57
53
|
# Set the project locales from the Project API.
|
58
54
|
# Implementation example:
|
59
55
|
#
|
@@ -61,40 +57,47 @@ module WebTranslateIt
|
|
61
57
|
# locales = configuration.locales # returns an array of locales: ['en', 'fr', 'es', ...]
|
62
58
|
def set_locales(project)
|
63
59
|
self.source_locale = project['source_locale']['code']
|
64
|
-
self.target_locales = project['target_locales'].map{|locale| locale['code']}
|
60
|
+
self.target_locales = project['target_locales'].map { |locale| locale['code'] }
|
65
61
|
end
|
66
|
-
|
62
|
+
|
67
63
|
# Set the project files from the Project API.
|
68
64
|
# Implementation example:
|
69
65
|
#
|
70
66
|
# configuration = WebTranslateIt::Configuration.new
|
71
67
|
# files = configuration.files # returns an array of TranslationFile
|
72
|
-
def set_files(project)
|
68
|
+
def set_files(project) # rubocop:todo Metrics/AbcSize
|
73
69
|
self.files = []
|
74
70
|
project['project_files'].each do |project_file|
|
75
|
-
if project_file['name'].nil?
|
71
|
+
if project_file['name'].nil? || (project_file['name'].strip == '')
|
76
72
|
puts "File #{project_file['id']} not set up"
|
73
|
+
elsif ignore_files&.any? { |glob| File.fnmatch(glob, project_file['name']) }
|
74
|
+
puts "Ignoring #{project_file['name']}"
|
77
75
|
else
|
78
|
-
|
76
|
+
files.push TranslationFile.new(project_file['id'], project_file['name'], project_file['locale_code'], api_key, project_file['updated_at'], project_file['hash_file'], project_file['master_project_file_id'], project_file['fresh'])
|
79
77
|
end
|
80
78
|
end
|
81
79
|
end
|
82
|
-
|
80
|
+
|
83
81
|
# Set locales to ignore from the configuration file, if set.
|
84
82
|
def set_locales_to_ignore(configuration)
|
85
|
-
self.ignore_locales = Array(configuration['ignore_locales']).map
|
83
|
+
self.ignore_locales = Array(configuration['ignore_locales']).map(&:to_s)
|
86
84
|
end
|
87
85
|
|
88
86
|
# Set locales to specifically pull from the configuration file, if set
|
89
87
|
def set_locales_needed(configuration)
|
90
|
-
self.needed_locales = Array(configuration['needed_locales']).map
|
88
|
+
self.needed_locales = Array(configuration['needed_locales']).map(&:to_s)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Set files to ignore from the configuration file, if set.
|
92
|
+
def set_ignore_files(configuration)
|
93
|
+
self.ignore_files = Array(configuration['ignore_files']).map(&:to_s)
|
91
94
|
end
|
92
|
-
|
95
|
+
|
93
96
|
# Convenience method which returns the endpoint for fetching a list of locales for a project.
|
94
97
|
def api_url
|
95
98
|
"/api/projects/#{api_key}.yaml"
|
96
99
|
end
|
97
|
-
|
100
|
+
|
98
101
|
# Returns a logger. If RAILS_DEFAULT_LOGGER is defined, use it, else, define a new logger.
|
99
102
|
def logger
|
100
103
|
if defined?(Rails.logger)
|
@@ -111,7 +114,7 @@ module WebTranslateIt
|
|
111
114
|
private
|
112
115
|
|
113
116
|
def parse_erb_in_configuration
|
114
|
-
ERB.new(File.read(File.expand_path(path_to_config_file,
|
117
|
+
ERB.new(File.read(File.expand_path(path_to_config_file, path))).result
|
115
118
|
end
|
116
119
|
end
|
117
120
|
end
|
@@ -1,17 +1,17 @@
|
|
1
|
-
|
1
|
+
require 'English'
|
2
2
|
module WebTranslateIt
|
3
3
|
class Connection
|
4
|
+
attr_reader :api_key, :http_connection
|
5
|
+
|
4
6
|
require 'net/http'
|
5
7
|
require 'net/https'
|
6
8
|
require 'openssl'
|
7
9
|
require 'uri'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
@@silent = false
|
14
|
-
|
10
|
+
|
11
|
+
@api_key = nil
|
12
|
+
@http_connection = nil
|
13
|
+
@debug = false
|
14
|
+
|
15
15
|
#
|
16
16
|
# Initialize and yield a HTTPS Keep-Alive connection to WebTranslateIt.com
|
17
17
|
#
|
@@ -27,45 +27,33 @@ module WebTranslateIt
|
|
27
27
|
# http_connection.request(request)
|
28
28
|
# end
|
29
29
|
#
|
30
|
-
def initialize(api_key)
|
31
|
-
|
32
|
-
proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) :
|
30
|
+
def initialize(api_key) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength
|
31
|
+
@api_key = api_key
|
32
|
+
proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : Struct.new(:host, :port, :user, :password).new
|
33
33
|
http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
|
34
34
|
http.use_ssl = true
|
35
35
|
http.open_timeout = http.read_timeout = 60
|
36
|
-
http.set_debug_output($stderr) if
|
36
|
+
http.set_debug_output($stderr) if @debug
|
37
37
|
begin
|
38
|
-
http.verify_mode
|
39
|
-
|
40
|
-
yield
|
38
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
39
|
+
@http_connection = http.start
|
40
|
+
yield @http_connection if block_given?
|
41
41
|
rescue OpenSSL::SSL::SSLError
|
42
|
-
puts
|
42
|
+
puts 'Unable to verify SSL certificate.' unless @silent
|
43
43
|
http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
|
44
|
-
http.set_debug_output($stderr) if
|
44
|
+
http.set_debug_output($stderr) if @debug
|
45
45
|
http.use_ssl = true
|
46
46
|
http.open_timeout = http.read_timeout = 60
|
47
|
-
http.verify_mode
|
48
|
-
|
49
|
-
yield
|
47
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
48
|
+
@http_connection = http.start
|
49
|
+
yield @http_connection if block_given?
|
50
50
|
rescue
|
51
|
-
puts
|
51
|
+
puts $ERROR_INFO
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
55
|
-
def self.http_connection
|
56
|
-
@@http_connection
|
57
|
-
end
|
58
54
|
|
59
55
|
def self.turn_debug_on
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.turn_silent_on
|
64
|
-
@@silent = true
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.api_key
|
68
|
-
@@api_key
|
56
|
+
@debug = true
|
69
57
|
end
|
70
58
|
end
|
71
59
|
end
|
@@ -1,8 +1,7 @@
|
|
1
|
-
|
1
|
+
require 'English'
|
2
2
|
module WebTranslateIt
|
3
3
|
class Project
|
4
|
-
|
5
|
-
def self.fetch_info(api_key)
|
4
|
+
def self.fetch_info(api_key) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
|
6
5
|
success = true
|
7
6
|
tries ||= 3
|
8
7
|
begin
|
@@ -10,29 +9,27 @@ module WebTranslateIt
|
|
10
9
|
request = Net::HTTP::Get.new("/api/projects/#{api_key}.yaml")
|
11
10
|
WebTranslateIt::Util.add_fields(request)
|
12
11
|
response = http.request(request)
|
13
|
-
if response.is_a?(Net::HTTPSuccess)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
exit 1
|
19
|
-
end
|
12
|
+
return response.body if response.is_a?(Net::HTTPSuccess)
|
13
|
+
|
14
|
+
puts 'An error occured while fetching the project information:'
|
15
|
+
puts StringUtil.failure(response.body)
|
16
|
+
exit 1
|
20
17
|
end
|
21
18
|
rescue Timeout::Error
|
22
|
-
puts
|
23
|
-
if (tries -= 1)
|
19
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
20
|
+
if (tries -= 1).positive?
|
24
21
|
sleep(5)
|
25
22
|
retry
|
26
23
|
else
|
27
24
|
success = false
|
28
25
|
end
|
29
26
|
rescue
|
30
|
-
puts
|
27
|
+
puts $ERROR_INFO.inspect
|
31
28
|
end
|
32
29
|
success
|
33
30
|
end
|
34
|
-
|
35
|
-
def self.fetch_stats(api_key)
|
31
|
+
|
32
|
+
def self.fetch_stats(api_key) # rubocop:todo Metrics/MethodLength
|
36
33
|
success = true
|
37
34
|
tries ||= 3
|
38
35
|
begin
|
@@ -42,8 +39,8 @@ module WebTranslateIt
|
|
42
39
|
return Util.handle_response(http.request(request), true)
|
43
40
|
end
|
44
41
|
rescue Timeout::Error
|
45
|
-
puts
|
46
|
-
if (tries -= 1)
|
42
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
43
|
+
if (tries -= 1).positive?
|
47
44
|
sleep(5)
|
48
45
|
retry
|
49
46
|
else
|
@@ -52,8 +49,8 @@ module WebTranslateIt
|
|
52
49
|
end
|
53
50
|
success
|
54
51
|
end
|
55
|
-
|
56
|
-
def self.create_locale(locale_code)
|
52
|
+
|
53
|
+
def self.create_locale(locale_code) # rubocop:todo Metrics/MethodLength
|
57
54
|
success = true
|
58
55
|
tries ||= 3
|
59
56
|
begin
|
@@ -62,8 +59,8 @@ module WebTranslateIt
|
|
62
59
|
request.set_form_data({ 'id' => locale_code }, ';')
|
63
60
|
Util.handle_response(Connection.http_connection.request(request), true)
|
64
61
|
rescue Timeout::Error
|
65
|
-
puts
|
66
|
-
if (tries -= 1)
|
62
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
63
|
+
if (tries -= 1).positive?
|
67
64
|
sleep(5)
|
68
65
|
retry
|
69
66
|
else
|
@@ -72,8 +69,8 @@ module WebTranslateIt
|
|
72
69
|
end
|
73
70
|
success
|
74
71
|
end
|
75
|
-
|
76
|
-
def self.delete_locale(locale_code)
|
72
|
+
|
73
|
+
def self.delete_locale(locale_code) # rubocop:todo Metrics/MethodLength
|
77
74
|
success = true
|
78
75
|
tries ||= 3
|
79
76
|
begin
|
@@ -81,8 +78,8 @@ module WebTranslateIt
|
|
81
78
|
WebTranslateIt::Util.add_fields(request)
|
82
79
|
Util.handle_response(Connection.http_connection.request(request), true)
|
83
80
|
rescue Timeout::Error
|
84
|
-
puts
|
85
|
-
if (tries -= 1)
|
81
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
82
|
+
if (tries -= 1).positive?
|
86
83
|
sleep(5)
|
87
84
|
retry
|
88
85
|
else
|
@@ -1,11 +1,10 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module WebTranslateIt
|
3
|
-
class String
|
2
|
+
class String # rubocop:todo Metrics/ClassLength
|
4
3
|
require 'multi_json'
|
5
|
-
|
4
|
+
|
6
5
|
attr_accessor :id, :key, :plural, :type, :dev_comment, :word_count, :status, :category, :labels, :file,
|
7
6
|
:created_at, :updated_at, :translations, :new_record
|
8
|
-
|
7
|
+
|
9
8
|
# Initialize a new WebTranslateIt::String
|
10
9
|
#
|
11
10
|
# Implementation Example:
|
@@ -19,25 +18,25 @@ module WebTranslateIt
|
|
19
18
|
# WebTranslateIt::String.new({ :key => "product_name_123", :translations => [translation_en, translation_fr]})
|
20
19
|
#
|
21
20
|
# to instantiate a new String with a source and target translation.
|
22
|
-
|
23
|
-
def initialize(params = {})
|
21
|
+
|
22
|
+
def initialize(params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
|
24
23
|
params.stringify_keys!
|
25
|
-
self.id = params[
|
26
|
-
self.key = params[
|
27
|
-
self.plural = params[
|
28
|
-
self.type = params[
|
29
|
-
self.dev_comment = params[
|
30
|
-
self.word_count = params[
|
31
|
-
self.status = params[
|
32
|
-
self.category = params[
|
33
|
-
self.labels = params[
|
34
|
-
self.file = params[
|
35
|
-
self.created_at = params[
|
36
|
-
self.updated_at = params[
|
37
|
-
self.translations = params[
|
24
|
+
self.id = params['id'] || nil
|
25
|
+
self.key = params['key'] || nil
|
26
|
+
self.plural = params['plural'] || nil
|
27
|
+
self.type = params['type'] || nil
|
28
|
+
self.dev_comment = params['dev_comment'] || nil
|
29
|
+
self.word_count = params['word_count'] || nil
|
30
|
+
self.status = params['status'] || nil
|
31
|
+
self.category = params['category'] || nil
|
32
|
+
self.labels = params['labels'] || nil
|
33
|
+
self.file = params['file'] || nil
|
34
|
+
self.created_at = params['created_at'] || nil
|
35
|
+
self.updated_at = params['updated_at'] || nil
|
36
|
+
self.translations = params['translations'] || []
|
38
37
|
self.new_record = true
|
39
38
|
end
|
40
|
-
|
39
|
+
|
41
40
|
# Find a String based on filters
|
42
41
|
#
|
43
42
|
# Implementation Example:
|
@@ -49,27 +48,27 @@ module WebTranslateIt
|
|
49
48
|
# puts strings.inspect #=> An array of WebTranslateIt::String objects
|
50
49
|
#
|
51
50
|
# to find and instantiate an array of String which key is like `product_name_123`.
|
52
|
-
|
53
|
-
def self.find_all(params = {})
|
51
|
+
|
52
|
+
def self.find_all(params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
|
54
53
|
success = true
|
55
54
|
tries ||= 3
|
56
55
|
params.stringify_keys!
|
57
56
|
url = "/api/projects/#{Connection.api_key}/strings.yaml"
|
58
|
-
url +=
|
57
|
+
url += "?#{HashUtil.to_params('filters' => params)}" unless params.empty?
|
59
58
|
|
60
59
|
request = Net::HTTP::Get.new(url)
|
61
60
|
WebTranslateIt::Util.add_fields(request)
|
62
61
|
begin
|
63
62
|
strings = []
|
64
|
-
while
|
63
|
+
while request
|
65
64
|
response = Connection.http_connection.request(request)
|
66
65
|
YAML.load(response.body).each do |string_response|
|
67
66
|
string = WebTranslateIt::String.new(string_response)
|
68
67
|
string.new_record = false
|
69
68
|
strings.push(string)
|
70
69
|
end
|
71
|
-
if response[
|
72
|
-
url = response[
|
70
|
+
if response['Link']&.include?('rel="next"')
|
71
|
+
url = response['Link'].match(/<(.*)>; rel="next"/)[1]
|
73
72
|
request = Net::HTTP::Get.new(url)
|
74
73
|
WebTranslateIt::Util.add_fields(request)
|
75
74
|
else
|
@@ -78,8 +77,8 @@ module WebTranslateIt
|
|
78
77
|
end
|
79
78
|
return strings
|
80
79
|
rescue Timeout::Error
|
81
|
-
puts
|
82
|
-
if (tries -= 1)
|
80
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
81
|
+
if (tries -= 1).positive?
|
83
82
|
sleep(5)
|
84
83
|
retry
|
85
84
|
else
|
@@ -88,7 +87,7 @@ module WebTranslateIt
|
|
88
87
|
end
|
89
88
|
success
|
90
89
|
end
|
91
|
-
|
90
|
+
|
92
91
|
# Find a String based on its ID
|
93
92
|
# Return a String object, or nil if not found.
|
94
93
|
#
|
@@ -102,8 +101,8 @@ module WebTranslateIt
|
|
102
101
|
#
|
103
102
|
# to find and instantiate the String which ID is `1234`.
|
104
103
|
#
|
105
|
-
|
106
|
-
def self.find(id)
|
104
|
+
|
105
|
+
def self.find(id) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize
|
107
106
|
success = true
|
108
107
|
tries ||= 3
|
109
108
|
request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{id}.yaml")
|
@@ -111,12 +110,13 @@ module WebTranslateIt
|
|
111
110
|
begin
|
112
111
|
response = Connection.http_connection.request(request)
|
113
112
|
return nil if response.code.to_i == 404
|
113
|
+
|
114
114
|
string = WebTranslateIt::String.new(YAML.load(response.body))
|
115
115
|
string.new_record = false
|
116
116
|
return string
|
117
117
|
rescue Timeout::Error
|
118
|
-
puts
|
119
|
-
if (tries -= 1)
|
118
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
119
|
+
if (tries -= 1).positive?
|
120
120
|
sleep(5)
|
121
121
|
retry
|
122
122
|
else
|
@@ -125,7 +125,7 @@ module WebTranslateIt
|
|
125
125
|
end
|
126
126
|
success
|
127
127
|
end
|
128
|
-
|
128
|
+
|
129
129
|
# Update or create a String to WebTranslateIt.com
|
130
130
|
#
|
131
131
|
# Implementation Example:
|
@@ -136,11 +136,11 @@ module WebTranslateIt
|
|
136
136
|
# string.save
|
137
137
|
# end
|
138
138
|
#
|
139
|
-
|
139
|
+
|
140
140
|
def save
|
141
|
-
|
141
|
+
new_record ? create : update
|
142
142
|
end
|
143
|
-
|
143
|
+
|
144
144
|
# Delete a String on WebTranslateIt.com
|
145
145
|
#
|
146
146
|
# Implementation Example:
|
@@ -150,17 +150,17 @@ module WebTranslateIt
|
|
150
150
|
# string.delete
|
151
151
|
# end
|
152
152
|
#
|
153
|
-
|
154
|
-
def delete
|
153
|
+
|
154
|
+
def delete # rubocop:todo Metrics/MethodLength
|
155
155
|
success = true
|
156
156
|
tries ||= 3
|
157
|
-
request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/strings/#{
|
157
|
+
request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/strings/#{id}")
|
158
158
|
WebTranslateIt::Util.add_fields(request)
|
159
159
|
begin
|
160
160
|
Util.handle_response(Connection.http_connection.request(request), true, true)
|
161
161
|
rescue Timeout::Error
|
162
|
-
puts
|
163
|
-
if (tries -= 1)
|
162
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
163
|
+
if (tries -= 1).positive?
|
164
164
|
sleep(5)
|
165
165
|
retry
|
166
166
|
else
|
@@ -169,7 +169,7 @@ module WebTranslateIt
|
|
169
169
|
end
|
170
170
|
success
|
171
171
|
end
|
172
|
-
|
172
|
+
|
173
173
|
# Gets a Translation for a String
|
174
174
|
#
|
175
175
|
# Implementation Example:
|
@@ -179,25 +179,26 @@ module WebTranslateIt
|
|
179
179
|
# puts string.translation_for("fr") #=> A Translation object
|
180
180
|
# end
|
181
181
|
#
|
182
|
-
|
183
|
-
def translation_for(locale)
|
182
|
+
|
183
|
+
def translation_for(locale) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
|
184
184
|
success = true
|
185
185
|
tries ||= 3
|
186
|
-
translation =
|
186
|
+
translation = translations.detect { |t| t.locale == locale }
|
187
187
|
return translation if translation
|
188
|
-
return nil if
|
189
|
-
|
188
|
+
return nil if new_record
|
189
|
+
|
190
|
+
request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{id}/locales/#{locale}/translations.yaml")
|
190
191
|
WebTranslateIt::Util.add_fields(request)
|
191
192
|
begin
|
192
193
|
response = Util.handle_response(Connection.http_connection.request(request), true, true)
|
193
194
|
hash = YAML.load(response)
|
194
195
|
return nil if hash.empty?
|
196
|
+
|
195
197
|
translation = WebTranslateIt::Translation.new(hash)
|
196
198
|
return translation
|
197
|
-
|
198
199
|
rescue Timeout::Error
|
199
|
-
puts
|
200
|
-
if (tries -= 1)
|
200
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
201
|
+
if (tries -= 1).positive?
|
201
202
|
sleep(5)
|
202
203
|
retry
|
203
204
|
else
|
@@ -206,29 +207,29 @@ module WebTranslateIt
|
|
206
207
|
end
|
207
208
|
success
|
208
209
|
end
|
209
|
-
|
210
|
+
|
210
211
|
protected
|
211
|
-
|
212
|
+
|
212
213
|
# Save the changes made to a String to WebTranslateIt.com
|
213
214
|
#
|
214
|
-
|
215
|
-
def update
|
215
|
+
|
216
|
+
def update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
|
216
217
|
success = true
|
217
218
|
tries ||= 3
|
218
|
-
request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/strings/#{
|
219
|
+
request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/strings/#{id}.yaml")
|
219
220
|
WebTranslateIt::Util.add_fields(request)
|
220
|
-
request.body =
|
221
|
-
|
222
|
-
|
223
|
-
translation.string_id =
|
221
|
+
request.body = to_json
|
222
|
+
|
223
|
+
translations.each do |translation|
|
224
|
+
translation.string_id = id
|
224
225
|
translation.save
|
225
226
|
end
|
226
|
-
|
227
|
+
|
227
228
|
begin
|
228
229
|
Util.handle_response(Connection.http_connection.request(request), true, true)
|
229
230
|
rescue Timeout::Error
|
230
|
-
puts
|
231
|
-
if (tries -= 1)
|
231
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
232
|
+
if (tries -= 1).positive?
|
232
233
|
sleep(5)
|
233
234
|
retry
|
234
235
|
else
|
@@ -237,24 +238,24 @@ module WebTranslateIt
|
|
237
238
|
end
|
238
239
|
success
|
239
240
|
end
|
240
|
-
|
241
|
+
|
241
242
|
# Create a new String to WebTranslateIt.com
|
242
243
|
#
|
243
|
-
|
244
|
-
def create
|
244
|
+
|
245
|
+
def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
|
245
246
|
success = true
|
246
247
|
tries ||= 3
|
247
248
|
request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/strings")
|
248
249
|
WebTranslateIt::Util.add_fields(request)
|
249
|
-
request.body =
|
250
|
+
request.body = to_json(true)
|
250
251
|
begin
|
251
252
|
response = YAML.load(Util.handle_response(Connection.http_connection.request(request), true, true))
|
252
|
-
self.id = response[
|
253
|
+
self.id = response['id']
|
253
254
|
self.new_record = false
|
254
255
|
return true
|
255
256
|
rescue Timeout::Error
|
256
|
-
puts
|
257
|
-
if (tries -= 1)
|
257
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
258
|
+
if (tries -= 1).positive?
|
258
259
|
sleep(5)
|
259
260
|
retry
|
260
261
|
else
|
@@ -263,23 +264,23 @@ module WebTranslateIt
|
|
263
264
|
end
|
264
265
|
success
|
265
266
|
end
|
266
|
-
|
267
|
-
def to_json(with_translations = false)
|
267
|
+
|
268
|
+
def to_json(with_translations = false) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
|
268
269
|
hash = {
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
270
|
+
'id' => id,
|
271
|
+
'key' => key,
|
272
|
+
'plural' => plural,
|
273
|
+
'type' => type,
|
274
|
+
'dev_comment' => dev_comment,
|
275
|
+
'status' => status,
|
276
|
+
'labels' => labels,
|
277
|
+
'category' => category,
|
278
|
+
'file' => file
|
278
279
|
}
|
279
|
-
if
|
280
|
-
hash.update({
|
280
|
+
if translations.any? && with_translations
|
281
|
+
hash.update({ 'translations' => [] })
|
281
282
|
translations.each do |translation|
|
282
|
-
hash[
|
283
|
+
hash['translations'].push(translation.to_hash)
|
283
284
|
end
|
284
285
|
end
|
285
286
|
MultiJson.dump(hash)
|