web_translate_it 1.10.2 → 2.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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, :api_key
7
+ attr_accessor :api_key, :id, :locale, :text, :description, :status, :new_record, :term_id
7
8
 
8
- def initialize(id, locale, api_key)
9
- self.id = id
10
- self.locale = locale
11
- self.api_key = api_key
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
- def show(http_connection, options = {})
15
- options.reverse_merge!(:format => 'yaml')
16
-
17
- request = Net::HTTP::Get.new("/api/projects/#{self.api_key}/terms/#{self.id}/locales/#{self.locale}/translations.#{options[:format]}")
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 create(http_connection, params = {})
31
- request = Net::HTTP::Post.new("/api/projects/#{self.api_key}/terms/#{self.id}/locales/#{self.locale}/translations")
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 = params.to_json
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, :api_key
7
+ attr_accessor :api_key, :id, :locale, :text, :status, :created_at, :updated_at, :version, :string_id
7
8
 
8
- def initialize(id, locale, api_key)
9
- self.id = id
10
- self.locale = locale
11
- self.api_key = api_key
12
- end
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 show(http_connection, options = {})
15
- options.reverse_merge!(:format => 'yaml')
16
-
17
- request = Net::HTTP::Get.new("/api/projects/#{self.api_key}/strings/#{self.id}/locales/#{self.locale}/translations.#{options[:format]}")
18
- request.add_field("X-Client-Name", "web_translate_it")
19
- request.add_field("X-Client-Version", WebTranslateIt::Util.version)
20
-
21
- begin
22
- Util.handle_response(http_connection.request(request), true)
23
- rescue Timeout::Error
24
- puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
25
- sleep(5)
26
- retry
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
- def create(http_connection, params = {})
31
- request = Net::HTTP::Post.new("/api/projects/#{self.api_key}/strings/#{self.id}/locales/#{self.locale}/translations")
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
- hash = YAML.load_file File.expand_path('../../../version.yml', __FILE__)
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.to_param(hash, namespace = nil) # taken from Rails
3
- hash.collect do |key, value|
4
- value.to_query(namespace ? "#{namespace}[#{key}]" : key)
5
- end.sort * '&'
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 rubygem providing tools to sync your language files with [Web Translate It](https://webtranslateit.com), a web-based translation software.
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
  ![Web Translate It](http://f.cl.ly/items/2X3m0h0g0I1O1U07163o/wti_example.jpg)
13
12
 
14
- This gem provides:
13
+ ## This rubygem provides:
15
14
 
16
- * a command-line executable `wti`, to sync your files between your computer/server and WebTranslateIt.com,
17
- * a synchronisation server to help your translation team update your language files from a web interface,
18
- * a rack middleware you can use within your Rails app to automatically fetch new translations from Web Translate It.
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
- Installation
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 tool will prompt for:
38
+ The command will ask you to enter:
37
39
 
38
- * your Web Translate It API key. You can find it in your project settings ([where are the project settings?](http://help.webtranslateit.com/kb/troubleshooting/where-are-my-project-settings)),
39
- * where to save the configuration file (by default in `config/translations.yml`).
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 Synchronisation Console
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