Skip to content

Commit

Permalink
Merge pull request #18 from sharoonthomas/torcellite
Browse files Browse the repository at this point in the history
Add detect and get_language capability
  • Loading branch information
Sharoon Thomas committed Dec 16, 2014
2 parents 40a78e8 + 639bcb2 commit 9affd5d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,18 @@ def translate_array(self, texts, to_lang, from_lang=None, **options):
params['from'] = from_lang

return self.call("TranslateArray", params)

def get_languages(self):
"""Fetches the languages supported by Microsoft Translator
Returns list of languages
"""
return self.call('GetLanguagesForTranslate', '')

def detect_language(self, text):
"""Detects language of given string
Returns two letter language - Example : fr
"""
params = {
'text': text.encode('utf8')
}
return self.call('Detect', params)
12 changes: 12 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
client_id = "translaterpythonapi"
client_secret = "FLghnwW4LJmNgEG+EZkL8uE+wb7+6tkOS8eejHg3AaI="

default_languages = [u'en', u'fr', u'de']


class TestTranslator(unittest.TestCase):

Expand All @@ -25,6 +27,16 @@ def test_invalid_client_id(self):
with self.assertRaises(TranslateApiException):
client.translate("hello", "pt")

def test_get_languages(self):
client = Translator(client_id, client_secret, debug=True)
languages = client.get_languages()
self.assertEqual(type(languages), list)
self.assertTrue(set(default_languages).issubset(set(languages)))

def test_detect_language(self):
client = Translator(client_id, client_secret, debug=True)
self.assertEqual(client.detect_language('hello'), u'en')


def test_all():
loader = unittest.TestLoader()
Expand Down

0 comments on commit 9affd5d

Please sign in to comment.