web_translate_it 1.10.2 → 2.0.0.rc2
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.
- data/history.md +13 -0
- data/lib/web_translate_it.rb +2 -0
- data/lib/web_translate_it/string.rb +197 -33
- data/lib/web_translate_it/term.rb +175 -29
- data/lib/web_translate_it/term_translation.rb +68 -14
- data/lib/web_translate_it/translation.rb +46 -24
- data/lib/web_translate_it/util.rb +1 -2
- data/lib/web_translate_it/util/hash_util.rb +24 -4
- data/readme.md +20 -22
- data/spec/web_translate_it/string_spec.rb +127 -0
- data/spec/web_translate_it/term_spec.rb +114 -0
- data/version +1 -0
- metadata +94 -81
- data/spec/web_translate_it/configuration_spec.rb +0 -60
- data/spec/web_translate_it/translation_file_spec.rb +0 -52
- data/spec/web_translate_it/util_spec.rb +0 -10
- data/spec/web_translate_it_spec.rb +0 -26
- data/version.yml +0 -4
@@ -2,41 +2,95 @@
|
|
2
2
|
module WebTranslateIt
|
3
3
|
class TermTranslation
|
4
4
|
require 'net/https'
|
5
|
+
require 'json'
|
5
6
|
|
6
|
-
attr_accessor :id, :locale, :
|
7
|
+
attr_accessor :api_key, :id, :locale, :text, :description, :status, :new_record, :term_id
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
# Initialize a new WebTranslateIt::TermTranslation
|
10
|
+
# Mandatory parameter is `api_key`
|
11
|
+
#
|
12
|
+
# Implementation Example:
|
13
|
+
#
|
14
|
+
# WebTranslateIt::TermTranslation.new('secret_api_token', { "text" => "Super!" })
|
15
|
+
#
|
16
|
+
# to instantiate a new TermTranslation.
|
17
|
+
#
|
18
|
+
|
19
|
+
def initialize(api_key, params = {})
|
20
|
+
self.api_key = api_key
|
21
|
+
self.id = params["id"] || nil
|
22
|
+
self.locale = params["locale"] || nil
|
23
|
+
self.text = params["text"] || nil
|
24
|
+
self.description = params["description"] || nil
|
25
|
+
self.status = params["status"] || nil
|
26
|
+
self.term_id = params["term_id"] || nil
|
27
|
+
self.new_record = true
|
28
|
+
end
|
29
|
+
|
30
|
+
# Update or Create a WebTranslateIt::TermTranslation
|
31
|
+
#
|
32
|
+
# Implementation Example:
|
33
|
+
#
|
34
|
+
# translation = WebTranslateIt::TermTranslation.new('secret_api_token', { "term_id" => "1234", "text" => "Super!" })
|
35
|
+
# translation.text = "I changed it!"
|
36
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
37
|
+
# translation.save(connection)
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
|
41
|
+
def save(http_connection)
|
42
|
+
if self.new_record
|
43
|
+
self.create(http_connection)
|
44
|
+
else
|
45
|
+
self.update(http_connection)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_hash
|
50
|
+
{
|
51
|
+
"id" => id,
|
52
|
+
"locale" => locale,
|
53
|
+
"text" => text,
|
54
|
+
"description" => description,
|
55
|
+
"status" => status
|
56
|
+
}
|
12
57
|
end
|
13
58
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
request = Net::HTTP::
|
59
|
+
protected
|
60
|
+
|
61
|
+
def create(http_connection)
|
62
|
+
request = Net::HTTP::Post.new("/api/projects/#{self.api_key}/terms/#{self.term_id}/locales/#{self.locale}/translations")
|
18
63
|
request.add_field("X-Client-Name", "web_translate_it")
|
19
64
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
65
|
+
request.add_field("Content-Type", "application/json")
|
66
|
+
|
67
|
+
request.body = self.to_hash.to_json
|
20
68
|
|
21
69
|
begin
|
22
|
-
Util.handle_response(http_connection.request(request), true)
|
70
|
+
response = Util.handle_response(http_connection.request(request), true)
|
71
|
+
response = YAML.load(response)
|
72
|
+
self.id = response["id"]
|
73
|
+
self.new_record = false
|
74
|
+
return true
|
75
|
+
|
23
76
|
rescue Timeout::Error
|
24
77
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
25
78
|
sleep(5)
|
26
79
|
retry
|
27
80
|
end
|
28
81
|
end
|
29
|
-
|
30
|
-
def
|
31
|
-
request = Net::HTTP::
|
82
|
+
|
83
|
+
def update(http_connection)
|
84
|
+
request = Net::HTTP::Put.new("/api/projects/#{self.api_key}/terms/#{self.id}/locales/#{self.locale}/translations/#{self.id}")
|
32
85
|
request.add_field("X-Client-Name", "web_translate_it")
|
33
86
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
34
87
|
request.add_field("Content-Type", "application/json")
|
35
88
|
|
36
|
-
request.body =
|
89
|
+
request.body = self.to_hash.to_json
|
37
90
|
|
38
91
|
begin
|
39
92
|
Util.handle_response(http_connection.request(request), true)
|
93
|
+
|
40
94
|
rescue Timeout::Error
|
41
95
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
42
96
|
sleep(5)
|
@@ -2,38 +2,53 @@
|
|
2
2
|
module WebTranslateIt
|
3
3
|
class Translation
|
4
4
|
require 'net/https'
|
5
|
+
require 'json'
|
5
6
|
|
6
|
-
attr_accessor :id, :locale, :
|
7
|
+
attr_accessor :api_key, :id, :locale, :text, :status, :created_at, :updated_at, :version, :string_id
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
# Initialize a new WebTranslateIt::Translation
|
10
|
+
# Mandatory parameters are `api_key` and { "string_id" => "1234" }
|
11
|
+
#
|
12
|
+
# Implementation Example:
|
13
|
+
#
|
14
|
+
# WebTranslateIt::Translation.new('secret_api_token', { "string_id" => "1234", "text" => "Super!" })
|
15
|
+
#
|
16
|
+
# to instantiate a new Translation without any text.
|
17
|
+
#
|
13
18
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
19
|
+
def initialize(api_key, params = {})
|
20
|
+
self.api_key = api_key
|
21
|
+
self.id = params["id"] || nil
|
22
|
+
self.locale = params["locale"] || nil
|
23
|
+
self.text = params["text"] || nil
|
24
|
+
self.status = params["status"] || "status_unproofread"
|
25
|
+
self.created_at = params["created_at"] || nil
|
26
|
+
self.updated_at = params["updated_at"] || nil
|
27
|
+
self.version = params["version"] || nil
|
28
|
+
if params["string"]
|
29
|
+
self.string_id = params["string"]["id"]
|
30
|
+
else
|
31
|
+
self.string_id = nil
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
30
|
-
|
31
|
-
|
35
|
+
# Save a WebTranslateIt::Translation
|
36
|
+
#
|
37
|
+
# Implementation Example:
|
38
|
+
#
|
39
|
+
# translation = WebTranslateIt::Translation.new('secret_api_token', { "string_id" => "1234", "text" => "Super!" })
|
40
|
+
# translation.text = "I changed it!"
|
41
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
42
|
+
# translation.save(connection)
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
|
46
|
+
def save(http_connection)
|
47
|
+
request = Net::HTTP::Post.new("/api/projects/#{self.api_key}/strings/#{self.string_id}/locales/#{self.locale}/translations")
|
32
48
|
request.add_field("X-Client-Name", "web_translate_it")
|
33
49
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
34
50
|
request.add_field("Content-Type", "application/json")
|
35
|
-
|
36
|
-
request.body = params.to_json
|
51
|
+
request.body = self.to_hash.to_json
|
37
52
|
|
38
53
|
begin
|
39
54
|
Util.handle_response(http_connection.request(request), true)
|
@@ -43,6 +58,13 @@ module WebTranslateIt
|
|
43
58
|
retry
|
44
59
|
end
|
45
60
|
end
|
46
|
-
|
61
|
+
|
62
|
+
def to_hash
|
63
|
+
{
|
64
|
+
"locale" => locale,
|
65
|
+
"text" => text,
|
66
|
+
"status" => status
|
67
|
+
}
|
68
|
+
end
|
47
69
|
end
|
48
70
|
end
|
@@ -11,8 +11,7 @@ module WebTranslateIt
|
|
11
11
|
# Return a string representing the gem version
|
12
12
|
# For example "1.8.3"
|
13
13
|
def self.version
|
14
|
-
|
15
|
-
[hash[:major], hash[:minor], hash[:patch]].join('.')
|
14
|
+
IO.read(File.expand_path('../../../version', __FILE__))
|
16
15
|
end
|
17
16
|
|
18
17
|
# Yields a HTTP connection over SSL to Web Translate It.
|
@@ -1,7 +1,27 @@
|
|
1
1
|
class HashUtil
|
2
|
-
def self.
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
def self.to_params(hash)
|
3
|
+
params = ''
|
4
|
+
stack = []
|
5
|
+
|
6
|
+
hash.each do |k, v|
|
7
|
+
if v.is_a?(Hash)
|
8
|
+
stack << [k,v]
|
9
|
+
else
|
10
|
+
params << "#{k}=#{v}&"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
stack.each do |parent, hash|
|
15
|
+
hash.each do |k, v|
|
16
|
+
if v.is_a?(Hash)
|
17
|
+
stack << ["#{parent}[#{k}]", v]
|
18
|
+
else
|
19
|
+
params << "#{parent}[#{k}]=#{v}&"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
params.chop! # trailing &
|
25
|
+
params
|
6
26
|
end
|
7
27
|
end
|
data/readme.md
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
Web Translate It
|
2
|
-
================
|
1
|
+
# Web Translate It
|
3
2
|
|
4
3
|
[Homepage](https://webtranslateit.com) |
|
5
4
|
[RDoc](http://yardoc.org/docs/AtelierConvivialite-webtranslateit) |
|
@@ -7,18 +6,22 @@ Web Translate It
|
|
7
6
|
[Report a bug](http://github.com/AtelierConvivialite/webtranslateit/issues) |
|
8
7
|
[Support](http://help.webtranslateit.com)
|
9
8
|
|
10
|
-
`web_translate_it` is a
|
9
|
+
`web_translate_it` is a tool to sync your language files with [WebTranslateIt.com](https://webtranslateit.com), a web-based translation software.
|
11
10
|
|
12
11
|

|
13
12
|
|
14
|
-
This
|
13
|
+
## This rubygem provides:
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
1. A command-line executable `wti` to sync your files between your computer/server and WebTranslateIt.com. It can run in a terminal (Linux, MacOS X) or in cmd.exe (Windows). This is what this page is all about.
|
16
|
+
2. A synchronisation server to help your translation team update your language files from a web interface. To learn how to install it, go to [web_translate_it_server](https://github.com/AtelierConvivialite/web_translate_it_server).
|
17
|
+
3. Ruby libraries to write programs connecting to WebTranslateIt.com’s API. For more information, go to [Extras](https://github.com/AtelierConvivialite/webtranslateit/wiki/Extras).
|
18
|
+
4. A rack middleware you can use in your Rails app to automatically fetch new translations from Web Translate It.
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
This readme focusses on the most commonly used functionality of this rubygem: the `wti` command-line executable.
|
21
|
+
|
22
|
+
---
|
23
|
+
|
24
|
+
## Installation
|
22
25
|
|
23
26
|
You will need ruby to run wti. On Linux or a Mac, it’s already installed. Install [RubyInstaller](http://rubyinstaller.org/) if you’re using Windows. [See detailed installation instructions for Windows users](https://github.com/AtelierConvivialite/webtranslateit/wiki/Install-wti-on-Windows).
|
24
27
|
|
@@ -26,20 +29,18 @@ You will need ruby to run wti. On Linux or a Mac, it’s already installed. Inst
|
|
26
29
|
|
27
30
|
At this point you should have the `wti` executable working.
|
28
31
|
|
29
|
-
Configuration
|
30
|
-
-------------
|
32
|
+
## Configuration
|
31
33
|
|
32
34
|
Now that the tool is installed, you’ll have to configure your project:
|
33
35
|
|
34
36
|
wti init
|
35
37
|
|
36
|
-
The
|
38
|
+
The command will ask you to enter:
|
37
39
|
|
38
|
-
*
|
39
|
-
*
|
40
|
+
* Your Web Translate It API key. You can find it in your project settings,
|
41
|
+
* Where to save the configuration file (by default it will create a `.wti` in your project root directory).
|
40
42
|
|
41
|
-
Usage
|
42
|
-
-----
|
43
|
+
## Usage
|
43
44
|
|
44
45
|
Execute `wti --help` to see the usage:
|
45
46
|
|
@@ -76,8 +77,7 @@ Append `--help` for each command for more information. For instance:
|
|
76
77
|
--label, -b <s>: Apply a label to the changes
|
77
78
|
--help, -h: Show this message
|
78
79
|
|
79
|
-
Sample Commands
|
80
|
-
---------------
|
80
|
+
## Sample Commands
|
81
81
|
|
82
82
|
<table>
|
83
83
|
<tr>
|
@@ -146,8 +146,7 @@ Sample Commands
|
|
146
146
|
</tr>
|
147
147
|
</table>
|
148
148
|
|
149
|
-
Hooks
|
150
|
-
-----
|
149
|
+
## Hooks
|
151
150
|
|
152
151
|
It is sometimes useful to hook a command or a script before or after a push or a pull. One use-case would be to launch a build after pulling language files. You can do that by implementing hooks in your `.wti` file.
|
153
152
|
|
@@ -160,8 +159,7 @@ There are 4 hooks:
|
|
160
159
|
|
161
160
|
Check the [sample `.wti`](https://github.com/AtelierConvivialite/webtranslateit/blob/master/examples/.wti#L9..L13) file for implementation.
|
162
161
|
|
163
|
-
Web Translate It
|
164
|
-
----------------------------------------
|
162
|
+
## Web Translate It Server
|
165
163
|
|
166
164
|
This feature was extracted out to a separate gem. See [web_translate_it_server](https://github.com/AtelierConvivialite/web_translate_it_server).
|
167
165
|
|
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WebTranslateIt::String do
|
4
|
+
|
5
|
+
let(:api_key) { "19875cfdd4f169b33e0ffab32cfb0bbb9e33d653" }
|
6
|
+
|
7
|
+
describe "#initialize" do
|
8
|
+
it "should assign api_key and many parameters" do
|
9
|
+
string = WebTranslateIt::String.new(api_key, { "id" => 1234, "key" => "bacon"})
|
10
|
+
string.api_key.should == api_key
|
11
|
+
string.id.should == 1234
|
12
|
+
string.key.should == "bacon"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#save" do
|
17
|
+
it "should create a new String" do
|
18
|
+
WebTranslateIt::Util.http_connection do |connection|
|
19
|
+
string = WebTranslateIt::String.new(api_key, { "key" => "bacon" })
|
20
|
+
string.save(connection)
|
21
|
+
string.id.should_not be_nil
|
22
|
+
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
|
23
|
+
string_fetched.should_not be_nil
|
24
|
+
string_fetched.should be_a(WebTranslateIt::String)
|
25
|
+
string_fetched.id.should == string.id
|
26
|
+
string.delete(connection)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should update an existing String" do
|
31
|
+
WebTranslateIt::Util.http_connection do |connection|
|
32
|
+
string = WebTranslateIt::String.new(api_key, { "key" => "bacony" })
|
33
|
+
string.save(connection)
|
34
|
+
string.key = "bacon changed"
|
35
|
+
string.save(connection)
|
36
|
+
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
|
37
|
+
string_fetched.key.should == "bacon changed"
|
38
|
+
string.delete(connection)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should create a new String with Translation" do
|
43
|
+
translation1 = WebTranslateIt::Translation.new(api_key, { "locale" => "en", "text" => "Hello" })
|
44
|
+
translation2 = WebTranslateIt::Translation.new(api_key, { "locale" => "fr", "text" => "Bonjour" })
|
45
|
+
|
46
|
+
string = WebTranslateIt::String.new(api_key, { "key" => "bacon", "translations" => [translation1, translation2] })
|
47
|
+
WebTranslateIt::Util.http_connection do |connection|
|
48
|
+
string.save(connection)
|
49
|
+
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
|
50
|
+
string_fetched.translation_for(connection, "en").should_not be_nil
|
51
|
+
string_fetched.translation_for(connection, "en").text.should == "Hello"
|
52
|
+
string_fetched.translation_for(connection, "fr").should_not be_nil
|
53
|
+
string_fetched.translation_for(connection, "fr").text.should == "Bonjour"
|
54
|
+
string_fetched.translation_for(connection, "es").should be_nil
|
55
|
+
string.delete(connection)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should update a String and save its Translation" do
|
60
|
+
translation1 = WebTranslateIt::Translation.new(api_key, { "locale" => "en", "text" => "Hello" })
|
61
|
+
translation2 = WebTranslateIt::Translation.new(api_key, { "locale" => "fr", "text" => "Bonjour" })
|
62
|
+
|
63
|
+
string = WebTranslateIt::String.new(api_key, { "key" => "bacon" })
|
64
|
+
WebTranslateIt::Util.http_connection do |connection|
|
65
|
+
string.save(connection)
|
66
|
+
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
|
67
|
+
string_fetched.translation_for(connection, "fr").should be_nil
|
68
|
+
|
69
|
+
string_fetched.translations = [translation1, translation2]
|
70
|
+
string_fetched.save(connection)
|
71
|
+
|
72
|
+
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
|
73
|
+
string_fetched.translation_for(connection, "en").should_not be_nil
|
74
|
+
string_fetched.translation_for(connection, "en").text.should == "Hello"
|
75
|
+
string_fetched.translation_for(connection, "fr").should_not be_nil
|
76
|
+
string_fetched.translation_for(connection, "fr").text.should == "Bonjour"
|
77
|
+
string.delete(connection)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "#delete" do
|
83
|
+
it "should delete a String" do
|
84
|
+
string = WebTranslateIt::String.new(api_key, { "key" => "bacon" })
|
85
|
+
WebTranslateIt::Util.http_connection do |connection|
|
86
|
+
string.save(connection)
|
87
|
+
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
|
88
|
+
string_fetched.should_not be_nil
|
89
|
+
|
90
|
+
string_fetched.delete(connection)
|
91
|
+
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
|
92
|
+
string_fetched.should be_nil
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "#find_all" do
|
98
|
+
it "should find many strings" do
|
99
|
+
WebTranslateIt::Util.http_connection do |connection|
|
100
|
+
string1 = WebTranslateIt::String.new(api_key, { "key" => "bacon" })
|
101
|
+
string1.save(connection)
|
102
|
+
string2 = WebTranslateIt::String.new(api_key, { "key" => "tart" })
|
103
|
+
string2.save(connection)
|
104
|
+
string3 = WebTranslateIt::String.new(api_key, { "key" => "bacon tart" })
|
105
|
+
string3.save(connection)
|
106
|
+
|
107
|
+
strings = WebTranslateIt::String.find_all(connection, api_key, { "key" => "bacon" })
|
108
|
+
strings.count.should == 2
|
109
|
+
strings[0].key.should == "bacon"
|
110
|
+
strings[1].key.should == "bacon tart"
|
111
|
+
|
112
|
+
strings = WebTranslateIt::String.find_all(connection, api_key, { "key" => "tart" })
|
113
|
+
strings.count.should == 2
|
114
|
+
strings[0].key.should == "tart"
|
115
|
+
strings[1].key.should == "bacon tart"
|
116
|
+
|
117
|
+
strings = WebTranslateIt::String.find_all(connection, api_key, { "key" => "bacon tart" })
|
118
|
+
strings.count.should == 1
|
119
|
+
strings[0].key.should == "bacon tart"
|
120
|
+
|
121
|
+
string1.delete(connection)
|
122
|
+
string2.delete(connection)
|
123
|
+
string3.delete(connection)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|