lita-translation 1.0.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92de845747ee164159ffba572f90f692d6d42ea6
4
- data.tar.gz: 2681a8075c783578e9d115ca7975083401c59b77
3
+ metadata.gz: c6b394891aa7a2a8e8584c5ab87415405a5c5397
4
+ data.tar.gz: be41483e5ca7729b0097879a665e46fb55e860a7
5
5
  SHA512:
6
- metadata.gz: 4408c35c04ec73dd30b1de26772be8ce47dc16aa7e227dd3151fa9e9c5c284e186f77c7150eaf8dfd8ce09e369adbed8b8659d39c1024a3afc3123da3b9a40ac
7
- data.tar.gz: 8eeb31021dbb354d75886894b7835540c6597afffb6d311d36859367075b57865f2603ab1bba7e184255a02ee67ecde83dd58d24e46fa65704a22f2bdec3631a
6
+ metadata.gz: 8dfe633f4303ee1d47181dc4162e2fd64a37047386644e6bb7b5182fb2c7471a26471c051f9df750571f3f74e1f0fe9b1a079193244c66b836a7e212d714c560
7
+ data.tar.gz: 5e5eac53fd6e6464898b48db5b853736001a580459220105d50ee9b68bf0506a43ae5883e3c5924506a896c5c7708dbe963466e9ba3a730dca3f052536ce7dbc
data/README.md CHANGED
@@ -36,8 +36,8 @@ Microsoft uses ISO-639 codes to identify languages. Klingon is available.
36
36
 
37
37
  * languages - List language codes supported by Microsoft's Translator API
38
38
  * determine '[text]' - Determine language of the given text
39
- * translate '[text]' to [code] (from [code]) - Translate the given text from one language to another
40
- * translate me to [code] (from [code]) - Begin auto-translating all user's speech to the given language
39
+ * translate '[text]' to \[code\] (from [code]) - Translate the given text from one language to another
40
+ * translate me to \[code\] (from [code]) - Begin auto-translating all user's speech to the given language
41
41
  * stop translating me - End any auto-translation
42
42
 
43
43
  Source language is optional during translation. Microsoft will attempt to detect the source language if none is supplied.
@@ -4,7 +4,8 @@ class MSTranslator
4
4
  OAUTH_URI = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
5
5
  API_URI = "https://api.microsofttranslator.com/V2/Ajax.svc/"
6
6
 
7
- REST_LANGUAGES = "GetLanguagesForTranslate"
7
+ REST_LANGCODES = "GetLanguagesForTranslate"
8
+ REST_LANGNAMES = "GetLanguageNames"
8
9
  REST_DETECTION = "Detect"
9
10
  REST_TRANSLATE = "Translate"
10
11
 
@@ -51,9 +52,28 @@ class MSTranslator
51
52
  end
52
53
  end
53
54
 
54
- def languages()
55
+ def languageCodes()
55
56
  result = @http.get do |req|
56
- req.url API_URI+REST_LANGUAGES
57
+ req.url API_URI+REST_LANGCODES
58
+ req.headers['Authorization'] = "Bearer "+@redis.get("token")
59
+ end
60
+ if apiSuccess?(result)
61
+ TranslationResult.new(
62
+ true,
63
+ JSON.parse(result.body.slice(3..result.body.length)).join(",")
64
+ )
65
+ else
66
+ TranslationResult.new(
67
+ false,
68
+ result.body.slice(3..result.body.length)
69
+ )
70
+ end
71
+ end
72
+
73
+ def languageNames(codes)
74
+ codes = '['+codes.split(",").map { |code| '"'+code+'"' }.join(",")+']'
75
+ result = @http.get do |req|
76
+ req.url API_URI+REST_LANGNAMES, {:locale => "en", :languageCodes => codes}
57
77
  req.headers['Authorization'] = "Bearer "+@redis.get("token")
58
78
  end
59
79
  if apiSuccess?(result)
@@ -6,6 +6,37 @@ module Lita
6
6
  config :client_id, type: String, required: true
7
7
  config :client_secret, type: String, required: true
8
8
 
9
+ def updateStoredLang(codes, names)
10
+ languages = {}
11
+ codes.split(",").zip(names.split(",")).each { |pair|
12
+ languages[pair[0]] = pair[1]
13
+ }
14
+ redis.set("languages", languages.to_json)
15
+ languages
16
+ end
17
+
18
+ on :connected, :init_lang
19
+ def init_lang(payload)
20
+ translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
21
+ if translator.staleToken?
22
+ translator.grabAccessToken
23
+ end
24
+
25
+ result = translator.languageCodes
26
+ if !result.success
27
+ return
28
+ end
29
+
30
+ codes = result.message
31
+ result = translator.languageNames(codes)
32
+ if !result.success
33
+ return
34
+ end
35
+
36
+ names = result.message
37
+ updateStoredLang(codes, names)
38
+ end
39
+
9
40
  def tokenAvailable?(response, translator)
10
41
  if translator.staleToken?
11
42
  response.reply(t("replies.access_token.attempt"))
@@ -27,13 +58,25 @@ module Lita
27
58
  def languages(response)
28
59
  translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
29
60
  if tokenAvailable?(response, translator)
30
- result = translator.languages
31
- if result.success
32
- response.reply(t("replies.languages"))
33
- else
61
+ codeResult = translator.languageCodes
62
+ if !codeResult.success
34
63
  response.reply(t("replies.failure"))
64
+ response.reply(codeResult.message)
65
+ return
35
66
  end
36
- response.reply(result.message)
67
+
68
+ nameResult = translator.languageNames(codeResult.message)
69
+ if !nameResult.success
70
+ response.reply(t("replies.failure"))
71
+ response.reply(nameResult.message)
72
+ return
73
+ end
74
+
75
+ languages = updateStoredLang(codeResult.message, nameResult.message)
76
+ response.reply_privately(t("replies.languages"))
77
+ languages.each{ |code, name|
78
+ response.reply_privately("#{code}(#{name})")
79
+ }
37
80
  end
38
81
  end
39
82
 
@@ -46,7 +89,10 @@ module Lita
46
89
  result = translator.detect(response.matches.pop[0])
47
90
  if result.success
48
91
  code = result.message
49
- response.reply(t("replies.determine", code: code))
92
+ code[0] = ''
93
+ code = code[0..-2]
94
+ name = JSON.parse(redis.get("languages"))[code]
95
+ response.reply(t("replies.determine", code: code, name: name))
50
96
  else
51
97
  response.reply(t("replies.failure"))
52
98
  response.reply(result.message)
@@ -54,7 +100,7 @@ module Lita
54
100
  end
55
101
  end
56
102
 
57
- route /^translate me to (\w+)( from (\w+))?$/, :auto_start, help: {
103
+ route /^translate me to ([-\w]+)( from ([-\w]+))?$/, :auto_start, help: {
58
104
  t("help.auto_start.usage") => t("help.auto_start.description")
59
105
  }
60
106
  def auto_start(response)
@@ -62,9 +108,13 @@ module Lita
62
108
  if tokenAvailable?(response, translator)
63
109
  to = response.matches.flatten[0]
64
110
  from = response.matches.flatten[2]
111
+ name = JSON.parse(redis.get("languages"))[to]
112
+ if name.nil?
113
+ response.reply(t("replies.unknown_code", code: to))
114
+ end
65
115
  redis.set(response.user.id+":to", to)
66
116
  redis.set(response.user.id+":from", from)
67
- response.reply(t("replies.auto_start", code: to, user: response.user.name))
117
+ response.reply(t("replies.auto_start", code: to, name: name, user: response.user.name))
68
118
  end
69
119
  end
70
120
 
@@ -77,7 +127,7 @@ module Lita
77
127
  response.reply(t("replies.auto_end", user: response.user.name))
78
128
  end
79
129
 
80
- route /^translate '(.+)' to (\w+)( from (\w+))?$/, :tran_lang, help: {
130
+ route /^translate '(.+)' to ([-\w]+)( from ([-\w]+))?$/, :tran_lang, help: {
81
131
  t("help.translate.usage") => t("help.translate.description")
82
132
  }
83
133
  def tran_lang(response)
@@ -86,9 +136,10 @@ module Lita
86
136
  text = response.matches.flatten[0]
87
137
  to = response.matches.flatten[1]
88
138
  from = response.matches.flatten[3]
139
+ name = JSON.parse(redis.get("languages"))[to]
89
140
  result = translator.translate(text, to, from)
90
141
  if result.success
91
- response.reply(t("replies.translate", translated: result.message))
142
+ response.reply(t("replies.translate", code: to, name: name, translated: result.message))
92
143
  else
93
144
  response.reply(t("replies.failure"))
94
145
  response.reply(result.message)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-translation"
3
- spec.version = "1.0.0"
3
+ spec.version = "1.0.1"
4
4
  spec.authors = ["Michael Chua"]
5
5
  spec.email = ["chua.mbt@gmail.com"]
6
6
  spec.description = %q{Language translation plugin that uses Microsoft's Translator API. }
@@ -5,7 +5,7 @@ en:
5
5
  help:
6
6
  languages:
7
7
  usage: "languages"
8
- description: "List language codes supported by Microsoft's Translator API"
8
+ description: "List languages supported by Microsoft's Translator API"
9
9
  determine:
10
10
  usage: "determine '[text]'"
11
11
  description: "Determine language of the given text"
@@ -23,10 +23,11 @@ en:
23
23
  attempt: "Requesting an access token..."
24
24
  success: "Got an access token for Microsoft's Translator API!"
25
25
  fail: "Could not get an access token!"
26
- languages: "The following language codes are supported: "
27
- determine: "Language code: %{code}"
28
- translate: "Translation: %{translated}"
26
+ languages: "The following languages are supported: "
27
+ determine: "Language: %{name}(%{code})"
28
+ translate: "Translation in %{name}(%{code}: %{translated}"
29
29
  failure: "API Failure: "
30
- auto_start: "Beginning translation to %{code} for %{user}."
30
+ auto_start: "Beginning translation to %{name}(%{code}) for %{user}."
31
31
  auto: "%{user}: %{translated}."
32
- auto_end: "Ceasing translation for %{user}."
32
+ auto_end: "Ceasing translation for %{user}."
33
+ unknown_code: "Warning: %{code} is unrecognized. The service may not be able to translate with the given code."
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-translation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Chua
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-18 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita