Added support for the Free Google Translate API (PR #117, thanks @cuchac)

This commit is contained in:
Marco Bonetti 2015-02-18 13:32:03 +01:00
parent 5b881382c2
commit 6b59b42ba1
6 changed files with 29 additions and 0 deletions

View file

@ -1,3 +1,8 @@
Version 0.7.6 (unreleased)
--------------------------
* Added support for the Free Google Translate API (PR #117, thanks @cuchac)
Version 0.7.5
-------------
* Fixed external JavaScript import to be url scheme independent (PR #101, thanks @tsouvarev)

View file

@ -64,6 +64,7 @@ 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``.
* ``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 <http://api.yandex.com/key/form.xml?service=trnsl>`_, then specify the key here. Defaults to ``None``.
* ``ROSETTA_GOOGLE_TRANSLATE``: Translation suggestions via the `Free Google Translate API <http://pythonhosted.org/goslate/>`_. You'll have to `pip install goslate`. Defaults to ``False``.
* ``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``.

View file

@ -1,4 +1,5 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
# Number of messages to display per page.
MESSAGES_PER_PAGE = getattr(settings, 'ROSETTA_MESSAGES_PER_PAGE', 10)
@ -17,6 +18,12 @@ AZURE_CLIENT_SECRET = getattr(settings, 'AZURE_CLIENT_SECRET', None)
# Use Google translator
GOOGLE_TRANSLATE = getattr(settings, 'ROSETTA_GOOGLE_TRANSLATE', None)
if GOOGLE_TRANSLATE:
try:
import goslate # NOQA
except ImportError:
raise ImproperlyConfigured('If you set ROSETTA_GOOGLE_TRANSLATE to True, you must install the `goslate` module.')
# Displays this language beside the original MSGID in the admin
MAIN_LANGUAGE = getattr(settings, 'ROSETTA_MAIN_LANGUAGE', None)

View file

@ -13,6 +13,7 @@ import os
import shutil
import six
import django
import json
try:
@ -58,6 +59,8 @@ class RosettaTestCase(TestCase):
self.__session_engine = settings.SESSION_ENGINE
self.__storage_class = rosetta_settings.STORAGE_CLASS
self.__require_auth = rosetta_settings.ROSETTA_REQUIRES_AUTH
self.__google_translate = rosetta_settings.GOOGLE_TRANSLATE
self.__enable_translation = rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS
shutil.copy(self.dest_file, self.dest_file + '.orig')
@ -66,6 +69,8 @@ class RosettaTestCase(TestCase):
settings.SESSION_ENGINE = self.__session_engine
rosetta_settings.STORAGE_CLASS = self.__storage_class
rosetta_settings.ROSETTA_REQUIRES_AUTH = self.__require_auth
rosetta_settings.GOOGLE_TRANSLATE = self.__google_translate
rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS = self.__enable_translation
shutil.move(self.dest_file + '.orig', self.dest_file)
def test_1_ListLoading(self):
@ -669,6 +674,13 @@ class RosettaTestCase(TestCase):
rosetta_settings.ROSETTA_LANGUAGE_GROUPS = ROSETTA_LANGUAGE_GROUPS
def test_33_pr_116_google_translate(self):
rosetta_settings.GOOGLE_TRANSLATE = True
rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS = True
r = self.client.get(reverse('translate_text') + '?from=en&to=fr&text=Hello,+world!')
self.assertTrue(six.text_type("Bonjour le monde!") in six.text_type(r.content))
# Stubbed access control function
def no_access(user):

View file

@ -80,3 +80,6 @@ STATIC_URL = '/static/'
#ROSETTA_STORAGE_CLASS = 'rosetta.storage.SessionRosettaStorage'
ROSETTA_STORAGE_CLASS = 'rosetta.storage.CacheRosettaStorage'
SECRET_KEY = 'empty'
ROSETTA_GOOGLE_TRANSLATE = True
ROSETTA_ENABLE_TRANSLATION_SUGGESTIONS = True

View file

@ -28,6 +28,7 @@ deps =
polib>=1.0.6
microsofttranslator==0.5
six
goslate
[testenv:gettext]
changedir = rosetta/locale/