microsoft_translator 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/.travis.yml +4 -0
- data/README.md +57 -3
- data/lib/microsoft_translator/client.rb +29 -6
- data/lib/microsoft_translator/version.rb +1 -1
- metadata +4 -3
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm use 1.9.3
|
1
|
+
rvm use 1.9.3@microsoft_translator --create
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,9 +1,30 @@
|
|
1
|
-
# MicrosoftTranslator
|
1
|
+
# MicrosoftTranslator [![Build Status](https://secure.travis-ci.org/ikayzo/microsoft_translator.png?branch=master)](http://travis-ci.org/ikayzo/microsoft_translator)
|
2
2
|
|
3
|
-
|
3
|
+
Ruby wrapper for Microsoft Translate HTTP API.
|
4
|
+
|
5
|
+
Still a work-in-progress. Currently only supports translating one string
|
6
|
+
of text at a time.
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
10
|
+
Before translating things from your ruby application you first need to
|
11
|
+
sign up for the Microsoft Translator API in the Windows Azure
|
12
|
+
Datamarket.
|
13
|
+
|
14
|
+
https://datamarket.azure.com/dataset/1899a118-d202-492c-aa16-ba21c33c06cb
|
15
|
+
|
16
|
+
Don't worry, they have a free tier! (up to 2 million translated characters/month) Once you sign up for the Translator
|
17
|
+
API you will also need to register your application with the Azure
|
18
|
+
Datamarket.
|
19
|
+
|
20
|
+
https://datamarket.azure.com/developer/applications/
|
21
|
+
|
22
|
+
Also, you shouldn't stress about what to put for the _*Redirect URI*_. For the purposes of
|
23
|
+
this gem you won't be using it so your project's homepage will work just
|
24
|
+
fine. You'll use the _*Client ID*_ and _*Client secret*_ to authenticate
|
25
|
+
your requests to the API. Once this is done you'll install it like you
|
26
|
+
would any other gem...
|
27
|
+
|
7
28
|
Add this line to your application's Gemfile:
|
8
29
|
|
9
30
|
gem 'microsoft_translator'
|
@@ -18,10 +39,43 @@ Or install it yourself as:
|
|
18
39
|
|
19
40
|
## Usage
|
20
41
|
|
21
|
-
|
42
|
+
Create a MicrosoftTranslator::Client with your Client ID & secret.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
translator = MicrosoftTranslator::Client.new('your_client_id', 'your_client_secret')
|
46
|
+
```
|
47
|
+
|
48
|
+
### Translation
|
49
|
+
|
50
|
+
To translate pass in the foreign text along with the language codes for
|
51
|
+
the language you are going from/to and the content type. The content
|
52
|
+
type is either "text/plain" or "text/html"
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
spanish = "hasta luego muchacha"
|
56
|
+
translator.translate(spanish,"es","en","text/html")
|
57
|
+
# => "until then girl"
|
58
|
+
```
|
59
|
+
|
60
|
+
### Language Detection
|
61
|
+
|
62
|
+
To detect the language simply pass in the foreign text. The language
|
63
|
+
code will be returned.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
translator.detect("Quiero un burrito")
|
67
|
+
# => "es"
|
68
|
+
```
|
69
|
+
That's about it! This is a list of the supported languages by the Microsoft
|
70
|
+
Translate API http://www.microsofttranslator.com/help/?FORM=R5FD and
|
71
|
+
here are all the language codes as a helpful reference.
|
72
|
+
http://www.loc.gov/standards/iso639-2/php/code_list.php
|
22
73
|
|
23
74
|
## Contributing
|
24
75
|
|
76
|
+
There are still quiet a few other methods available in the API that need
|
77
|
+
to be covered. http://msdn.microsoft.com/en-us/library/ff512419.aspx
|
78
|
+
|
25
79
|
1. Fork it
|
26
80
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
81
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
@@ -10,11 +10,27 @@ module MicrosoftTranslator
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def translate(text, from_lang, to_lang, content_type)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
begin
|
14
|
+
response = RestClient.get(
|
15
|
+
"http://api.microsofttranslator.com/V2/Http.svc/Translate",
|
16
|
+
translate_params(text, from_lang, to_lang, content_type)
|
17
|
+
)
|
18
|
+
parse_xml(response.body)
|
19
|
+
rescue RestClient::BadRequest
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def detect(text)
|
25
|
+
begin
|
26
|
+
response = RestClient.get(
|
27
|
+
"http://api.microsofttranslator.com/V2/Http.svc/Detect",
|
28
|
+
detect_params(text)
|
29
|
+
)
|
30
|
+
parse_xml(response.body)
|
31
|
+
rescue RestClient::BadRequest
|
32
|
+
false
|
33
|
+
end
|
18
34
|
end
|
19
35
|
|
20
36
|
private
|
@@ -31,7 +47,14 @@ module MicrosoftTranslator
|
|
31
47
|
"to" => to_lang,
|
32
48
|
"contentType" => content_type
|
33
49
|
})
|
34
|
-
|
50
|
+
hash
|
51
|
+
end
|
52
|
+
|
53
|
+
def detect_params(text)
|
54
|
+
hash = base_params
|
55
|
+
hash.store(:params,{
|
56
|
+
"text" => text
|
57
|
+
})
|
35
58
|
hash
|
36
59
|
end
|
37
60
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: microsoft_translator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-05-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rest-client
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- .gitignore
|
86
86
|
- .rspec
|
87
87
|
- .rvmrc
|
88
|
+
- .travis.yml
|
88
89
|
- Gemfile
|
89
90
|
- LICENSE
|
90
91
|
- README.md
|
@@ -118,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
119
|
version: '0'
|
119
120
|
requirements: []
|
120
121
|
rubyforge_project:
|
121
|
-
rubygems_version: 1.8.
|
122
|
+
rubygems_version: 1.8.25
|
122
123
|
signing_key:
|
123
124
|
specification_version: 3
|
124
125
|
summary: Use Microsoft Translate API in your ruby program
|