web_translate_it 2.5.4 → 2.6.0
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 +13 -7
- data/lib/web_translate_it/auto_fetch.rb +2 -4
- data/lib/web_translate_it/command_line.rb +244 -251
- data/lib/web_translate_it/configuration.rb +32 -27
- data/lib/web_translate_it/connection.rb +7 -8
- data/lib/web_translate_it/project.rb +16 -20
- data/lib/web_translate_it/string.rb +79 -78
- data/lib/web_translate_it/term.rb +59 -59
- data/lib/web_translate_it/term_translation.rb +32 -34
- data/lib/web_translate_it/translation.rb +24 -31
- data/lib/web_translate_it/translation_file.rb +105 -102
- 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 +47 -50
- data/lib/web_translate_it.rb +5 -7
- data/readme.md +6 -2
- 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 +45 -10
@@ -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,25 +9,25 @@ 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'])
|
@@ -37,15 +35,15 @@ module WebTranslateIt
|
|
37
35
|
WebTranslateIt::Connection.turn_silent_on if configuration['silence_errors']
|
38
36
|
self.project_name = project_info['project']['name']
|
39
37
|
else
|
40
|
-
puts StringUtil.failure("\nNo configuration file found in #{File.expand_path(path_to_config_file,
|
38
|
+
puts StringUtil.failure("\nNo configuration file found in #{File.expand_path(path_to_config_file, path)}")
|
41
39
|
exit(1)
|
42
40
|
end
|
43
41
|
end
|
44
|
-
|
42
|
+
|
45
43
|
# Reload project data
|
46
44
|
#
|
47
|
-
def reload
|
48
|
-
project_info = YAML.load WebTranslateIt::Project.fetch_info(
|
45
|
+
def reload # rubocop:todo Metrics/AbcSize
|
46
|
+
project_info = YAML.load WebTranslateIt::Project.fetch_info(api_key)
|
49
47
|
set_locales_to_ignore(configuration)
|
50
48
|
set_locales_needed(configuration)
|
51
49
|
set_files(project_info['project'])
|
@@ -53,7 +51,7 @@ module WebTranslateIt
|
|
53
51
|
WebTranslateIt::Connection.turn_silent_on if configuration['silence_errors']
|
54
52
|
self.project_name = project_info['project']['name']
|
55
53
|
end
|
56
|
-
|
54
|
+
|
57
55
|
# Set the project locales from the Project API.
|
58
56
|
# Implementation example:
|
59
57
|
#
|
@@ -61,40 +59,47 @@ module WebTranslateIt
|
|
61
59
|
# locales = configuration.locales # returns an array of locales: ['en', 'fr', 'es', ...]
|
62
60
|
def set_locales(project)
|
63
61
|
self.source_locale = project['source_locale']['code']
|
64
|
-
self.target_locales = project['target_locales'].map{|locale| locale['code']}
|
62
|
+
self.target_locales = project['target_locales'].map { |locale| locale['code'] }
|
65
63
|
end
|
66
|
-
|
64
|
+
|
67
65
|
# Set the project files from the Project API.
|
68
66
|
# Implementation example:
|
69
67
|
#
|
70
68
|
# configuration = WebTranslateIt::Configuration.new
|
71
69
|
# files = configuration.files # returns an array of TranslationFile
|
72
|
-
def set_files(project)
|
70
|
+
def set_files(project) # rubocop:todo Metrics/AbcSize
|
73
71
|
self.files = []
|
74
72
|
project['project_files'].each do |project_file|
|
75
73
|
if project_file['name'].nil? or project_file['name'].strip == ''
|
76
74
|
puts "File #{project_file['id']} not set up"
|
75
|
+
elsif ignore_files&.any? { |glob| File.fnmatch(glob, project_file['name']) }
|
76
|
+
puts "Ignoring #{project_file['name']}"
|
77
77
|
else
|
78
|
-
|
78
|
+
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
79
|
end
|
80
80
|
end
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
# Set locales to ignore from the configuration file, if set.
|
84
84
|
def set_locales_to_ignore(configuration)
|
85
|
-
self.ignore_locales = Array(configuration['ignore_locales']).map
|
85
|
+
self.ignore_locales = Array(configuration['ignore_locales']).map(&:to_s)
|
86
86
|
end
|
87
87
|
|
88
88
|
# Set locales to specifically pull from the configuration file, if set
|
89
89
|
def set_locales_needed(configuration)
|
90
|
-
self.needed_locales = Array(configuration['needed_locales']).map
|
90
|
+
self.needed_locales = Array(configuration['needed_locales']).map(&:to_s)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Set files to ignore from the configuration file, if set.
|
94
|
+
def set_ignore_files(configuration)
|
95
|
+
self.ignore_files = Array(configuration['ignore_files']).map(&:to_s)
|
91
96
|
end
|
92
|
-
|
97
|
+
|
93
98
|
# Convenience method which returns the endpoint for fetching a list of locales for a project.
|
94
99
|
def api_url
|
95
100
|
"/api/projects/#{api_key}.yaml"
|
96
101
|
end
|
97
|
-
|
102
|
+
|
98
103
|
# Returns a logger. If RAILS_DEFAULT_LOGGER is defined, use it, else, define a new logger.
|
99
104
|
def logger
|
100
105
|
if defined?(Rails.logger)
|
@@ -111,7 +116,7 @@ module WebTranslateIt
|
|
111
116
|
private
|
112
117
|
|
113
118
|
def parse_erb_in_configuration
|
114
|
-
ERB.new(File.read(File.expand_path(path_to_config_file,
|
119
|
+
ERB.new(File.read(File.expand_path(path_to_config_file, path))).result
|
115
120
|
end
|
116
121
|
end
|
117
122
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module WebTranslateIt
|
3
2
|
class Connection
|
4
3
|
require 'net/http'
|
@@ -6,12 +5,12 @@ module WebTranslateIt
|
|
6
5
|
require 'openssl'
|
7
6
|
require 'uri'
|
8
7
|
require 'ostruct'
|
9
|
-
|
8
|
+
|
10
9
|
@@api_key = nil
|
11
10
|
@@http_connection = nil
|
12
11
|
@@debug = false
|
13
12
|
@@silent = false
|
14
|
-
|
13
|
+
|
15
14
|
#
|
16
15
|
# Initialize and yield a HTTPS Keep-Alive connection to WebTranslateIt.com
|
17
16
|
#
|
@@ -27,7 +26,7 @@ module WebTranslateIt
|
|
27
26
|
# http_connection.request(request)
|
28
27
|
# end
|
29
28
|
#
|
30
|
-
def initialize(api_key)
|
29
|
+
def initialize(api_key) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength
|
31
30
|
@@api_key = api_key
|
32
31
|
proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : OpenStruct.new
|
33
32
|
http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
|
@@ -39,7 +38,7 @@ module WebTranslateIt
|
|
39
38
|
@@http_connection = http.start
|
40
39
|
yield @@http_connection if block_given?
|
41
40
|
rescue OpenSSL::SSL::SSLError
|
42
|
-
puts
|
41
|
+
puts 'Unable to verify SSL certificate.' unless @@silent
|
43
42
|
http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
|
44
43
|
http.set_debug_output($stderr) if @@debug
|
45
44
|
http.use_ssl = true
|
@@ -51,7 +50,7 @@ module WebTranslateIt
|
|
51
50
|
puts $!
|
52
51
|
end
|
53
52
|
end
|
54
|
-
|
53
|
+
|
55
54
|
def self.http_connection
|
56
55
|
@@http_connection
|
57
56
|
end
|
@@ -59,11 +58,11 @@ module WebTranslateIt
|
|
59
58
|
def self.turn_debug_on
|
60
59
|
@@debug = true
|
61
60
|
end
|
62
|
-
|
61
|
+
|
63
62
|
def self.turn_silent_on
|
64
63
|
@@silent = true
|
65
64
|
end
|
66
|
-
|
65
|
+
|
67
66
|
def self.api_key
|
68
67
|
@@api_key
|
69
68
|
end
|
@@ -1,8 +1,6 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module WebTranslateIt
|
3
2
|
class Project
|
4
|
-
|
5
|
-
def self.fetch_info(api_key)
|
3
|
+
def self.fetch_info(api_key) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
|
6
4
|
success = true
|
7
5
|
tries ||= 3
|
8
6
|
begin
|
@@ -10,16 +8,14 @@ module WebTranslateIt
|
|
10
8
|
request = Net::HTTP::Get.new("/api/projects/#{api_key}.yaml")
|
11
9
|
WebTranslateIt::Util.add_fields(request)
|
12
10
|
response = http.request(request)
|
13
|
-
if response.is_a?(Net::HTTPSuccess)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
exit 1
|
19
|
-
end
|
11
|
+
return response.body if response.is_a?(Net::HTTPSuccess)
|
12
|
+
|
13
|
+
puts 'An error occured while fetching the project information:'
|
14
|
+
puts StringUtil.failure(response.body)
|
15
|
+
exit 1
|
20
16
|
end
|
21
17
|
rescue Timeout::Error
|
22
|
-
puts
|
18
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
23
19
|
if (tries -= 1) > 0
|
24
20
|
sleep(5)
|
25
21
|
retry
|
@@ -31,8 +27,8 @@ module WebTranslateIt
|
|
31
27
|
end
|
32
28
|
success
|
33
29
|
end
|
34
|
-
|
35
|
-
def self.fetch_stats(api_key)
|
30
|
+
|
31
|
+
def self.fetch_stats(api_key) # rubocop:todo Metrics/MethodLength
|
36
32
|
success = true
|
37
33
|
tries ||= 3
|
38
34
|
begin
|
@@ -42,7 +38,7 @@ module WebTranslateIt
|
|
42
38
|
return Util.handle_response(http.request(request), true)
|
43
39
|
end
|
44
40
|
rescue Timeout::Error
|
45
|
-
puts
|
41
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
46
42
|
if (tries -= 1) > 0
|
47
43
|
sleep(5)
|
48
44
|
retry
|
@@ -52,8 +48,8 @@ module WebTranslateIt
|
|
52
48
|
end
|
53
49
|
success
|
54
50
|
end
|
55
|
-
|
56
|
-
def self.create_locale(locale_code)
|
51
|
+
|
52
|
+
def self.create_locale(locale_code) # rubocop:todo Metrics/MethodLength
|
57
53
|
success = true
|
58
54
|
tries ||= 3
|
59
55
|
begin
|
@@ -62,7 +58,7 @@ module WebTranslateIt
|
|
62
58
|
request.set_form_data({ 'id' => locale_code }, ';')
|
63
59
|
Util.handle_response(Connection.http_connection.request(request), true)
|
64
60
|
rescue Timeout::Error
|
65
|
-
puts
|
61
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
66
62
|
if (tries -= 1) > 0
|
67
63
|
sleep(5)
|
68
64
|
retry
|
@@ -72,8 +68,8 @@ module WebTranslateIt
|
|
72
68
|
end
|
73
69
|
success
|
74
70
|
end
|
75
|
-
|
76
|
-
def self.delete_locale(locale_code)
|
71
|
+
|
72
|
+
def self.delete_locale(locale_code) # rubocop:todo Metrics/MethodLength
|
77
73
|
success = true
|
78
74
|
tries ||= 3
|
79
75
|
begin
|
@@ -81,7 +77,7 @@ module WebTranslateIt
|
|
81
77
|
WebTranslateIt::Util.add_fields(request)
|
82
78
|
Util.handle_response(Connection.http_connection.request(request), true)
|
83
79
|
rescue Timeout::Error
|
84
|
-
puts
|
80
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
85
81
|
if (tries -= 1) > 0
|
86
82
|
sleep(5)
|
87
83
|
retry
|
@@ -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,7 +77,7 @@ module WebTranslateIt
|
|
78
77
|
end
|
79
78
|
return strings
|
80
79
|
rescue Timeout::Error
|
81
|
-
puts
|
80
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
82
81
|
if (tries -= 1) > 0
|
83
82
|
sleep(5)
|
84
83
|
retry
|
@@ -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
|
107
106
|
success = true
|
108
107
|
tries ||= 3
|
109
108
|
request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{id}.yaml")
|
@@ -111,11 +110,12 @@ 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
|
118
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
119
119
|
if (tries -= 1) > 0
|
120
120
|
sleep(5)
|
121
121
|
retry
|
@@ -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,16 +150,16 @@ 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
|
162
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
163
163
|
if (tries -= 1) > 0
|
164
164
|
sleep(5)
|
165
165
|
retry
|
@@ -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,24 +179,25 @@ 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
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
200
201
|
if (tries -= 1) > 0
|
201
202
|
sleep(5)
|
202
203
|
retry
|
@@ -206,28 +207,28 @@ 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
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
231
232
|
if (tries -= 1) > 0
|
232
233
|
sleep(5)
|
233
234
|
retry
|
@@ -237,23 +238,23 @@ 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
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
257
258
|
if (tries -= 1) > 0
|
258
259
|
sleep(5)
|
259
260
|
retry
|
@@ -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)
|