mirror of
https://github.com/Hopiu/django-rosetta.git
synced 2026-04-20 13:11:14 +00:00
included the Microsoft translator API client
This commit is contained in:
commit
46c6ff05a3
16 changed files with 462 additions and 34 deletions
|
|
@ -54,8 +54,8 @@ Rosetta can be configured via the following parameters, to be defined in your pr
|
|||
|
||||
* ``ROSETTA_MESSAGES_PER_PAGE``: Number of messages to display per page. Defaults to ``10``.
|
||||
* ``ROSETTA_ENABLE_TRANSLATION_SUGGESTIONS``: Enable AJAX translation suggestions. Defaults to ``False``.
|
||||
* ``BING_APP_ID``: Translation suggestions used to come from the Google Translation API service, but free service has been discontinued, and the next best thing is Microsoft `Bing's Translation API <http://msdn.microsoft.com/en-us/library/ff512404.aspx>`_. To use this service you must first `obtain an AppID key <https://ssl.bing.com/webmaster/Developers/AppIds/>`_, then specify the key here. Defaults to ``None``.
|
||||
* ``YANDEX_TRANSLATE_KEY``: Translation suggestions from Yandex `Yandex.Translate API <http://api.yandex.com/translate/>`_. To use this service you must first `obtain an AppID key <https://translate.yandex.com/apikeys>`_, then specify the key here. Defaults to ``None``.
|
||||
* ``AZURE_CLIENT_ID`` and ``AZURE_CLIENT_SECRET``: Translation suggestions using the Microsoft Azure API. To use this service, you must first `register for the service <https://datamarket.azure.com/dataset/5BA839F1-12CE-4CCE-BF57-A49D98D29A44>`_, then specify the 'Customer ID' and 'Primary Account Key' respectively, which you can find on your `account information page on Azure Marketplace <https://datamarket.azure.com/account?lang=en>`_.
|
||||
* ``ROSETTA_MESSAGES_SOURCE_LANGUAGE_CODE`` and ``ROSETTA_MESSAGES_SOURCE_LANGUAGE_NAME``: Change these if the source language in your PO files isn't English. Default to ``'en'`` and ``'English'`` respectively.
|
||||
* ``ROSETTA_WSGI_AUTO_RELOAD`` and ``ROSETTA_UWSGI_AUTO_RELOAD``: When running WSGI daemon mode, using ``mod_wsgi`` 2.0c5 or later, this setting controls whether the contents of the gettext catalog files should be automatically reloaded by the WSGI processes each time they are modified. For performance reasons, this setting should be disabled in production environments. Default to ``False``.
|
||||
* ``ROSETTA_EXCLUDED_APPLICATIONS``: Exclude applications defined in this list from being translated. Defaults to ``()``.
|
||||
|
|
|
|||
|
|
@ -6,11 +6,15 @@ MESSAGES_PER_PAGE = getattr(settings, 'ROSETTA_MESSAGES_PER_PAGE', 10)
|
|||
|
||||
# Enable Google translation suggestions
|
||||
ENABLE_TRANSLATION_SUGGESTIONS = getattr(settings, 'ROSETTA_ENABLE_TRANSLATION_SUGGESTIONS', False)
|
||||
# Can be obtained for free here: https://ssl.bing.com/webmaster/Developers/AppIds/
|
||||
BING_APP_ID = getattr(settings, 'BING_APP_ID', None)
|
||||
|
||||
|
||||
# Can be obtained for free here: https://translate.yandex.com/apikeys
|
||||
YANDEX_TRANSLATE_KEY = getattr(settings, 'YANDEX_TRANSLATE_KEY', None)
|
||||
|
||||
# Can be obtained for free here: https://ssl.bing.com/webmaster/Developers/AppIds/
|
||||
AZURE_CLIENT_ID = getattr(settings, 'AZURE_CLIENT_ID', None)
|
||||
AZURE_CLIENT_SECRET = getattr(settings, 'AZURE_CLIENT_SECRET', None)
|
||||
|
||||
# Displays this language beside the original MSGID in the admin
|
||||
MAIN_LANGUAGE = getattr(settings, 'ROSETTA_MAIN_LANGUAGE', None)
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ google.setOnLoadCallback(function() {
|
|||
$('.hide', $(this).parent()).hide();
|
||||
});
|
||||
|
||||
{% if rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS and rosetta_settings.BING_APP_ID %}
|
||||
{% if rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS and rosetta_settings.AZURE_CLIENT_ID and rosetta_settings.AZURE_CLIENT_SECRET %}
|
||||
$('a.suggest').click(function(e){
|
||||
e.preventDefault();
|
||||
var a = $(this);
|
||||
|
|
@ -16,30 +16,24 @@ google.setOnLoadCallback(function() {
|
|||
var trans=$('textarea',a.parent());
|
||||
var sourceLang = '{{ rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE }}';
|
||||
var destLang = '{{ rosetta_i18n_lang_code }}';
|
||||
var app_id = '{{ rosetta_settings.BING_APP_ID }}';
|
||||
var apiUrl = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate";
|
||||
|
||||
orig = unescape(orig).replace(/<br\s?\/?>/g,'\n').replace(/<code>/,'').replace(/<\/code>/g,'').replace(/>/g,'>').replace(/</g,'<');
|
||||
a.attr('class','suggesting').html('...');
|
||||
window.onTranslationComplete = function(resp) {
|
||||
trans.val(unescape(resp).replace(/'/g,'\'').replace(/"/g,'"').replace(/%\s+(\([^\)]+\))\s*s/g,' %$1s '));
|
||||
a.hide();
|
||||
};
|
||||
window.onTranslationError = function(response){
|
||||
a.text(response);
|
||||
};
|
||||
var apiData = {
|
||||
onerror: 'onTranslationError',
|
||||
appid: app_id,
|
||||
from: sourceLang,
|
||||
to: destLang,
|
||||
oncomplete: "onTranslationComplete",
|
||||
text: orig
|
||||
};
|
||||
$.ajax({
|
||||
url: apiUrl,
|
||||
data: apiData,
|
||||
dataType: 'jsonp'});
|
||||
|
||||
$.getJSON("/rosetta/translate/", {
|
||||
from: sourceLang,
|
||||
to: destLang,
|
||||
text: orig
|
||||
},
|
||||
function(data) {
|
||||
if (data.success){
|
||||
trans.val(unescape(data.translation).replace(/'/g,'\'').replace(/"/g,'"').replace(/%\s+(\([^\)]+\))\s*s/g,' %$1s '));
|
||||
a.hide();
|
||||
} else {
|
||||
a.text(data.error);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
{% endif %}
|
||||
|
||||
|
|
@ -89,7 +83,7 @@ google.setOnLoadCallback(function() {
|
|||
$($('.part',td).get(j)).css('top',textareaY + 'px');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
$('.translation textarea').blur(function() {
|
||||
if($(this).val()) {
|
||||
$('.alert', $(this).parents('tr')).remove();
|
||||
|
|
@ -117,5 +111,5 @@ google.setOnLoadCallback(function() {
|
|||
});
|
||||
|
||||
$('.translation textarea').eq(0).focus();
|
||||
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -9,4 +9,5 @@ urlpatterns = patterns('rosetta.views',
|
|||
url(r'^pick/$', 'list_languages', name='rosetta-pick-file'),
|
||||
url(r'^download/$', 'download_file', name='rosetta-download-file'),
|
||||
url(r'^select/(?P<langid>[\w\-_\.]+)/(?P<idx>\d+)/$', 'lang_sel', name='rosetta-language-selection'),
|
||||
url(r'^translate/$', 'translate_text', name='translate_text'),
|
||||
)
|
||||
|
|
|
|||
0
rosetta/utils/__init__.py
Normal file
0
rosetta/utils/__init__.py
Normal file
5
rosetta/utils/microsofttranslator/.travis.yml
Normal file
5
rosetta/utils/microsofttranslator/.travis.yml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
language: python
|
||||
python:
|
||||
- "2.7"
|
||||
install: python setup.py install
|
||||
script: python setup.py test
|
||||
12
rosetta/utils/microsofttranslator/CHANGELOG
Normal file
12
rosetta/utils/microsofttranslator/CHANGELOG
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
Changelog
|
||||
=========
|
||||
|
||||
Version 0.4
|
||||
-----------
|
||||
* Updated to use the Oauth based token issued by Bing
|
||||
* This release is not backward compatibleas the class signature has changed
|
||||
|
||||
Version 0.3
|
||||
-----------
|
||||
* Encode text as UTF-8 before sending to translator
|
||||
* Improved JSON Error handling
|
||||
32
rosetta/utils/microsofttranslator/LICENSE
Normal file
32
rosetta/utils/microsofttranslator/LICENSE
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
Copyright (c) 2011 by Openlabs Technologies & Consulting (P) Limited.
|
||||
|
||||
Some rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms of the software as well
|
||||
as documentation, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials provided
|
||||
with the distribution.
|
||||
|
||||
* The names of the contributors may not be used to endorse or
|
||||
promote products derived from this software without specific
|
||||
prior written permission.
|
||||
|
||||
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
|
||||
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
||||
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
||||
DAMAGE.
|
||||
2
rosetta/utils/microsofttranslator/MANIFEST.in
Normal file
2
rosetta/utils/microsofttranslator/MANIFEST.in
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
include LICENSE
|
||||
include README.rst
|
||||
45
rosetta/utils/microsofttranslator/README.rst
Normal file
45
rosetta/utils/microsofttranslator/README.rst
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
Microsoft Translator V2 -- Python API
|
||||
=====================================
|
||||
|
||||
:Version: 0.4
|
||||
:Web: http://openlabs.co.in/
|
||||
:keywords: Micrsoft Translator
|
||||
:copyright: Openlabs Technologies & Consulting (P) LTD
|
||||
|
||||
.. image:: https://secure.travis-ci.org/openlabs/Microsoft-Translator-Python-API.png?branch=master
|
||||
:target: http://travis-ci.org/#!/openlabs/Microsoft-Translator-Python-API
|
||||
|
||||
|
||||
This python API implements the Microsoft Translator services which can be used
|
||||
in web or client applications to perform language translation operations. The
|
||||
services support users who are not familiar with the default language of a page
|
||||
or application, or those desiring to communicate with people of a different
|
||||
language group.
|
||||
|
||||
|
||||
Example Usage:
|
||||
::
|
||||
|
||||
>>> from microsofttranslator import Translator
|
||||
>>> translator = Translator('<Your Client ID>', '<Your Client Secret>')
|
||||
>>> print translator.translate("Hello", "pt")
|
||||
"Olá"
|
||||
|
||||
Registering your application
|
||||
----------------------------
|
||||
|
||||
To register your application with Azure DataMarket,
|
||||
visit https://datamarket.azure.com/developer/applications/ using the
|
||||
LiveID credentials from step 1, and click on “Register”. In the
|
||||
“Register your application” dialog box, you can define your own
|
||||
Client ID and Name. The redirect URI is not used for the Microsoft
|
||||
Translator API. However, the redirect URI field is a mandatory field,
|
||||
and you must provide a URI to obtain the access code. A description is
|
||||
optional.
|
||||
|
||||
Take a note of the client ID and the client secret value.
|
||||
|
||||
Bugs and Development on Github
|
||||
------------------------------
|
||||
|
||||
https://github.com/openlabs/Microsoft-Translator-Python-API
|
||||
203
rosetta/utils/microsofttranslator/__init__.py
Normal file
203
rosetta/utils/microsofttranslator/__init__.py
Normal file
|
|
@ -0,0 +1,203 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
__init__
|
||||
|
||||
A translator using the micrsoft translation engine documented here:
|
||||
|
||||
http://msdn.microsoft.com/en-us/library/ff512419.aspx
|
||||
|
||||
:copyright: © 2011 by Openlabs Technologies & Consulting (P) Limited
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
__all__ = ['Translator', 'TranslateApiException']
|
||||
|
||||
import requests
|
||||
import warnings
|
||||
import logging
|
||||
|
||||
|
||||
class ArgumentOutOfRangeException(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message.replace('ArgumentOutOfRangeException: ', '')
|
||||
super(ArgumentOutOfRangeException, self).__init__(self.message)
|
||||
|
||||
|
||||
class TranslateApiException(Exception):
|
||||
def __init__(self, message, *args):
|
||||
self.message = message.replace('TranslateApiException: ', '')
|
||||
super(TranslateApiException, self).__init__(self.message, *args)
|
||||
|
||||
|
||||
class Translator(object):
|
||||
"""Implements AJAX API for the Microsoft Translator service
|
||||
|
||||
:param app_id: A string containing the Bing AppID. (Deprecated)
|
||||
"""
|
||||
|
||||
def __init__(self, client_id, client_secret,
|
||||
scope="http://api.microsofttranslator.com",
|
||||
grant_type="client_credentials", app_id=None, debug=False):
|
||||
"""
|
||||
|
||||
|
||||
:param client_id: The client ID that you specified when you registered
|
||||
your application with Azure DataMarket.
|
||||
:param client_secret: The client secret value that you obtained when
|
||||
you registered your application with Azure
|
||||
DataMarket.
|
||||
:param scope: Defaults to http://api.microsofttranslator.com
|
||||
;param grant_type: Defaults to "client_credentials"
|
||||
:param app_id: Deprecated
|
||||
:param debug: If true, the logging level will be set to debug
|
||||
|
||||
.. versionchanged: 0.4
|
||||
Bing AppID mechanism is deprecated and is no longer supported.
|
||||
See: http://msdn.microsoft.com/en-us/library/hh454950
|
||||
"""
|
||||
if app_id is not None:
|
||||
warnings.warn("""app_id is deprected since v0.4.
|
||||
See: http://msdn.microsoft.com/en-us/library/hh454950
|
||||
""", DeprecationWarning, stacklevel=2)
|
||||
|
||||
self.client_id = client_id
|
||||
self.client_secret = client_secret
|
||||
self.scope = scope
|
||||
self.grant_type = grant_type
|
||||
self.access_token = None
|
||||
self.debug = debug
|
||||
self.logger = logging.getLogger("microsofttranslator")
|
||||
if self.debug:
|
||||
self.logger.setLevel(level=logging.DEBUG)
|
||||
|
||||
def get_access_token(self):
|
||||
"""Bing AppID mechanism is deprecated and is no longer supported.
|
||||
As mentioned above, you must obtain an access token to use the
|
||||
Microsoft Translator API. The access token is more secure, OAuth
|
||||
standard compliant, and more flexible. Users who are using Bing AppID
|
||||
are strongly recommended to get an access token as soon as possible.
|
||||
|
||||
.. note::
|
||||
The value of access token can be used for subsequent calls to the
|
||||
Microsoft Translator API. The access token expires after 10
|
||||
minutes. It is always better to check elapsed time between time at
|
||||
which token issued and current time. If elapsed time exceeds 10
|
||||
minute time period renew access token by following obtaining
|
||||
access token procedure.
|
||||
|
||||
:return: The access token to be used with subsequent requests
|
||||
"""
|
||||
args = {
|
||||
'client_id': self.client_id,
|
||||
'client_secret': self.client_secret,
|
||||
'scope': self.scope,
|
||||
'grant_type': self.grant_type
|
||||
}
|
||||
response = requests.post(
|
||||
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13',
|
||||
data=args
|
||||
).json()
|
||||
|
||||
self.logger.debug(response)
|
||||
|
||||
if "error" in response:
|
||||
raise TranslateApiException(
|
||||
response.get('error_description', 'No Error Description'),
|
||||
response.get('error', 'Unknown Error')
|
||||
)
|
||||
return response['access_token']
|
||||
|
||||
def call(self, url, params):
|
||||
"""Calls the given url with the params urlencoded
|
||||
"""
|
||||
if not self.access_token:
|
||||
self.access_token = self.get_access_token()
|
||||
|
||||
resp = requests.get(
|
||||
"%s" % url,
|
||||
params=params,
|
||||
headers={'Authorization': 'Bearer %s' % self.access_token}
|
||||
)
|
||||
resp.encoding = 'UTF-8-sig'
|
||||
rv = resp.json()
|
||||
#rv = json.loads(response.decode("UTF-8-sig"))
|
||||
|
||||
if isinstance(rv, str) and \
|
||||
rv.startswith("ArgumentOutOfRangeException"):
|
||||
raise ArgumentOutOfRangeException(rv)
|
||||
|
||||
if isinstance(rv, str) and \
|
||||
rv.startswith("TranslateApiException"):
|
||||
raise TranslateApiException(rv)
|
||||
|
||||
return rv
|
||||
|
||||
def translate(self, text, to_lang, from_lang=None,
|
||||
content_type='text/plain', category='general'):
|
||||
"""Translates a text string from one language to another.
|
||||
|
||||
:param text: A string representing the text to translate.
|
||||
:param to_lang: A string representing the language code to
|
||||
translate the text into.
|
||||
:param from_lang: A string representing the language code of the
|
||||
translation text. If left None the response will include the
|
||||
result of language auto-detection. (Default: None)
|
||||
:param content_type: The format of the text being translated.
|
||||
The supported formats are "text/plain" and "text/html". Any HTML
|
||||
needs to be well-formed.
|
||||
:param category: The category of the text to translate. The only
|
||||
supported category is "general".
|
||||
"""
|
||||
params = {
|
||||
'text': text.encode('utf8'),
|
||||
'to': to_lang,
|
||||
'contentType': content_type,
|
||||
'category': category,
|
||||
}
|
||||
if from_lang is not None:
|
||||
params['from'] = from_lang
|
||||
return self.call(
|
||||
"http://api.microsofttranslator.com/V2/Ajax.svc/Translate",
|
||||
params)
|
||||
|
||||
def translate_array(self, texts, to_lang, from_lang=None, **options):
|
||||
"""Translates an array of text strings from one language to another.
|
||||
|
||||
:param texts: A list containing texts for translation.
|
||||
:param to_lang: A string representing the language code to
|
||||
translate the text into.
|
||||
:param from_lang: A string representing the language code of the
|
||||
translation text. If left None the response will include the
|
||||
result of language auto-detection. (Default: None)
|
||||
:param options: A TranslateOptions element containing the values below.
|
||||
They are all optional and default to the most common settings.
|
||||
|
||||
Category: A string containing the category (domain) of the
|
||||
translation. Defaults to "general".
|
||||
ContentType: The format of the text being translated. The
|
||||
supported formats are "text/plain" and "text/html". Any
|
||||
HTML needs to be well-formed.
|
||||
Uri: A string containing the content location of this
|
||||
translation.
|
||||
User: A string used to track the originator of the submission.
|
||||
State: User state to help correlate request and response. The
|
||||
same contents will be returned in the response.
|
||||
"""
|
||||
options = {
|
||||
'Category': "general",
|
||||
'Contenttype': "text/plain",
|
||||
'Uri': '',
|
||||
'User': 'default',
|
||||
'State': ''
|
||||
}.update(options)
|
||||
params = {
|
||||
'texts': json.dumps(texts),
|
||||
'to': to_lang,
|
||||
'options': json.dumps(options),
|
||||
}
|
||||
if from_lang is not None:
|
||||
params['from'] = from_lang
|
||||
|
||||
return self.call(
|
||||
"http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray",
|
||||
params)
|
||||
65
rosetta/utils/microsofttranslator/setup.py
Normal file
65
rosetta/utils/microsofttranslator/setup.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Microsoft translator API
|
||||
|
||||
The Microsoft Translator services can be used in web or client
|
||||
applications to perform language translation operations. The services
|
||||
support users who are not familiar with the default language of a page or
|
||||
application, or those desiring to communicate with people of a different
|
||||
language group.
|
||||
|
||||
This module implements the AJAX API for the Microsoft Translator service.
|
||||
|
||||
An example::
|
||||
|
||||
>>> from microsofttranslator import Translator
|
||||
>>> translator = Translator('<Your API Key>')
|
||||
>>> print translator.translate("Hello", "pt")
|
||||
"Olá"
|
||||
|
||||
The documentation for the service can be obtained here:
|
||||
http://msdn.microsoft.com/en-us/library/ff512423.aspx
|
||||
|
||||
The project is hosted on GitHub where your could fork the project or report
|
||||
issues. Visit https://github.com/openlabs/Microsoft-Translator-Python-API
|
||||
|
||||
:copyright: © 2011 by Openlabs Technologies & Consulting (P) Limited
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import os
|
||||
from setuptools import setup
|
||||
|
||||
def read(fname):
|
||||
return open(os.path.join(os.path.dirname(__file__), fname)).read()
|
||||
|
||||
setup(
|
||||
name = "microsofttranslator",
|
||||
version = "0.4",
|
||||
packages = [
|
||||
'microsofttranslator',
|
||||
],
|
||||
package_dir = {
|
||||
'microsofttranslator': '.'
|
||||
},
|
||||
author = "Openlabs Technologies & Consulting (P) Limited",
|
||||
author_email = "info@openlabs.co.in",
|
||||
description = "Microsoft Translator V2 - Python API",
|
||||
long_description = read('README.rst'),
|
||||
license = "BSD",
|
||||
keywords = "translation microsoft",
|
||||
url = "http://openlabs.co.in/",
|
||||
include_package_data = True,
|
||||
classifiers=[
|
||||
"Development Status :: 4 - Beta",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: BSD License",
|
||||
"Natural Language :: English",
|
||||
"Operating System :: OS Independent",
|
||||
"Topic :: Software Development :: Internationalization",
|
||||
"Topic :: Utilities"
|
||||
],
|
||||
test_suite = "microsofttranslator.test.test_all",
|
||||
install_requires=[
|
||||
'requests >= 1.2.3',
|
||||
]
|
||||
)
|
||||
37
rosetta/utils/microsofttranslator/test.py
Normal file
37
rosetta/utils/microsofttranslator/test.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
test
|
||||
|
||||
Test the translator
|
||||
|
||||
:copyright: (c) 2012 by Openlabs Technologies & Consulting (P) Limited
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import unittest
|
||||
from rosetta.utils.microsofttranslator import Translator, TranslateApiException
|
||||
|
||||
client_id = "translaterpythonapi"
|
||||
client_secret = "FLghnwW4LJmNgEG+EZkL8uE+wb7+6tkOS8eejHg3AaI="
|
||||
|
||||
|
||||
class TestTranslator(unittest.TestCase):
|
||||
|
||||
def test_translate(self):
|
||||
client = Translator(client_id, client_secret, debug=True)
|
||||
self.assertEqual(client.translate("hello", "pt"), u'Ol\xe1')
|
||||
|
||||
def test_invalid_client_id(self):
|
||||
client = Translator("foo", "bar")
|
||||
with self.assertRaises(TranslateApiException):
|
||||
client.translate("hello", "pt")
|
||||
|
||||
|
||||
def test_all():
|
||||
loader = unittest.TestLoader()
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTests(loader.loadTestsFromTestCase(TestTranslator))
|
||||
return suite
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
@ -8,12 +8,17 @@ from django.template import RequestContext
|
|||
from django.utils.encoding import iri_to_uri
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.views.decorators.cache import never_cache
|
||||
|
||||
from rosetta.utils.microsofttranslator import Translator, TranslateApiException
|
||||
|
||||
from rosetta.conf import settings as rosetta_settings
|
||||
from rosetta.polib import pofile
|
||||
from rosetta.poutil import find_pos, pagination_range, timestamp_with_timezone
|
||||
from rosetta.signals import entry_changed, post_save
|
||||
from rosetta.storage import get_storage
|
||||
from rosetta.access import can_translate
|
||||
|
||||
import json
|
||||
import re
|
||||
import rosetta
|
||||
import unicodedata
|
||||
|
|
@ -394,3 +399,25 @@ def lang_sel(request, langid, idx):
|
|||
storage.set('rosetta_i18n_write', False)
|
||||
|
||||
return HttpResponseRedirect(reverse('rosetta-home'))
|
||||
|
||||
def translate_text(request):
|
||||
language_from = request.GET.get('from', None)
|
||||
language_to = request.GET.get('to', None)
|
||||
text = request.GET.get('text', None)
|
||||
|
||||
if language_from == language_to:
|
||||
data = { 'success' : True, 'translation' : text }
|
||||
else:
|
||||
# run the translation:
|
||||
AZURE_CLIENT_ID = getattr(settings, 'AZURE_CLIENT_ID', None)
|
||||
AZURE_CLIENT_SECRET = getattr(settings, 'AZURE_CLIENT_SECRET', None)
|
||||
|
||||
translator = Translator(AZURE_CLIENT_ID, AZURE_CLIENT_SECRET)
|
||||
|
||||
try:
|
||||
translated_text = translator.translate(text, language_to)
|
||||
data = { 'success' : True, 'translation' : translated_text }
|
||||
except TranslateApiException as e:
|
||||
data = { 'success' : False, 'error' : "Translation API Exception: {0}".format(e.message) }
|
||||
|
||||
return HttpResponse(json.dumps(data), mimetype='application/json')
|
||||
|
|
|
|||
|
|
@ -4,42 +4,42 @@ if [ ! -d .venv_13 ]
|
|||
then
|
||||
virtualenv --no-site-packages --distribute --python=python2 .venv_13
|
||||
. .venv_13/bin/activate
|
||||
pip install Django==1.3 coverage python-memcached six microsofttranslator
|
||||
pip install --use-mirrors Django==1.3 coverage python-memcached six requests==2.1.0
|
||||
deactivate
|
||||
fi
|
||||
if [ ! -d .venv_14 ]
|
||||
then
|
||||
virtualenv --no-site-packages --distribute --python=python2 .venv_14
|
||||
. .venv_14/bin/activate
|
||||
pip install Django==1.4 coverage python-memcached six microsofttranslator
|
||||
pip install --use-mirrors Django==1.4 coverage python-memcached six requests==2.1.0
|
||||
deactivate
|
||||
fi
|
||||
if [ ! -d .venv_15 ]
|
||||
then
|
||||
virtualenv --no-site-packages --distribute --python=python2 .venv_15
|
||||
. .venv_15/bin/activate
|
||||
pip install Django==1.5 coverage python-memcached six microsofttranslator
|
||||
pip install --use-mirrors Django==1.5 coverage python-memcached six requests==2.1.0
|
||||
deactivate
|
||||
fi
|
||||
if [ ! -d .venv_15_p3 ]
|
||||
then
|
||||
virtualenv --no-site-packages --distribute --python=python3 .venv_15_p3
|
||||
. .venv_15_p3/bin/activate
|
||||
pip install Django==1.5 coverage python3-memcached six microsofttranslator
|
||||
pip install --use-mirrors Django==1.5 coverage python3-memcached six requests==2.1.0
|
||||
deactivate
|
||||
fi
|
||||
if [ ! -d .venv_16 ]
|
||||
then
|
||||
virtualenv --no-site-packages --distribute --python=python2 .venv_16
|
||||
. .venv_16/bin/activate
|
||||
pip install coverage python-memcached six microsofttranslator Django==1.6.1
|
||||
pip install --use-mirrors coverage python-memcached six Django==1.6.1 requests==2.1.0
|
||||
deactivate
|
||||
fi
|
||||
if [ ! -d .venv_16_p3 ]
|
||||
then
|
||||
virtualenv --no-site-packages --distribute --python=python3 .venv_16_p3
|
||||
. .venv_16_p3/bin/activate
|
||||
pip install coverage python3-memcached six microsofttranslator Django==1.6.1
|
||||
pip install --use-mirrors coverage python3-memcached six Django==1.6.1 requests==2.1.0
|
||||
deactivate
|
||||
fi
|
||||
|
||||
|
|
|
|||
3
setup.py
3
setup.py
|
|
@ -26,6 +26,7 @@ setup(
|
|||
zip_safe=False,
|
||||
install_requires=[
|
||||
'six >=1.2.0',
|
||||
'Django >= 1.3'
|
||||
'Django >= 1.3',
|
||||
'requests >= 2.1.0',
|
||||
]
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue