-
-
Notifications
You must be signed in to change notification settings - Fork 42
Reimplement usage of Google Translate API as a named utility to be able to provide different services #468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
erral
wants to merge
50
commits into
master
Choose a base branch
from
erral-issue-467
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
9962a26
convert the function that uses google translate to an adapter based l…
erral d9af445
implement adapter ordering
erral 6b4ad2b
implement availability of service
erral ab88212
changelog
erral 70699ec
black
erral 823f4fc
isort
erral 327d9ac
format
erral 2feb159
remove empty
erral 7089813
tests
erral edfc222
isort
erral adb4fe4
isort
erral 84266a9
lint
erral e9e4aa0
lint
erral e55b2af
Merge branch 'master' into erral-issue-467
erral 94f1de0
convert to utilities
erral 3b183a8
changelog
erral 0930656
remove unneeded·
erral f4646c4
Merge branch 'master' into erral-issue-467
erral b5d0d5c
only translate non-empty values
erral fb21293
add plone.restapi test requirements
erral 1fcec1a
Merge branch 'master' into erral-issue-467
erral cb5fb6e
Merge branch 'master' into erral-issue-467
erral 29a0845
Adds the volto.blocks behavior to LRF if `plone.volto` is installed.
wesleybl 29bf865
Move the function `add volto blocks_behavior_to_lrf` to the class `Se…
wesleybl 498b884
zpretty
erral 3e80c75
fix dependnecies
erral 24ea463
breaking
erral 2b8dade
utlity
erral 38fa588
lint
erral 9911ee8
mark changes as breaking
erral 35ceca2
remove more google traces
erral 931dc60
lint
erral 5e99984
fixes
erral 6db00c1
remove unneeded
erral 987229e
remove unneeded
erral 00d149f
remove unneeded
erral cdc4734
test new endpoint
erral 7f5885a
rename
erral f7f5307
lint
erral 6ef0dac
add missing test-dependency
erral dd69e06
load plone.rest to have the plone:service registration available
erral 177365e
register the endpoints only when p.a.multilingual is installed
erral 8cfb7ea
setup.py
erral 26c4f37
remove unneeded
erral 352a48e
load plone.restapi for tests
erral c1649f4
fix: load controlpanel permissions
erral c54cf5d
remove gtranslate icon and use plone provided translate icon
erral 2e4dbeb
restore tinymce detection
erral b1c2819
Merge branch 'master' into erral-issue-467
erral 2251b07
zpretty
erral File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Reimplement usage of Google Translate API as utility, to be able to provide different translation services | ||
| [erral] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| from plone.app.multilingual.interfaces import IMultiLanguageExtraOptionsSchema | ||
| from plone.registry.interfaces import IRegistry | ||
| from zope.component import getUtility | ||
|
|
||
| import json | ||
| import urllib | ||
|
|
||
|
|
||
| class GoogleCloudTranslationAPIFactory: | ||
| """implement the external translation using Google Cloud Translation API""" | ||
|
|
||
| order = 999 | ||
|
erral marked this conversation as resolved.
Outdated
|
||
|
|
||
| def is_available(self): | ||
| registry = getUtility(IRegistry) | ||
| settings = registry.forInterface( | ||
| IMultiLanguageExtraOptionsSchema, prefix="plone" | ||
| ) | ||
| key = settings.google_translation_key | ||
| return key is not None and len(key.strip()) > 0 | ||
|
|
||
| def available_languages(self): | ||
| # All languages are supported | ||
| return [] | ||
|
|
||
| def translate_content(self, content, source_language, target_language): | ||
| registry = getUtility(IRegistry) | ||
| settings = registry.forInterface( | ||
| IMultiLanguageExtraOptionsSchema, prefix="plone" | ||
| ) | ||
|
|
||
| question = content | ||
| length = len(question) | ||
| translated = "" | ||
| url = "https://www.googleapis.com/language/translate/v2" | ||
| temp_question = question | ||
| while length > 400: | ||
| temp_question = question[:399] | ||
| index = temp_question.rfind(" ") | ||
| temp_question = temp_question[:index] | ||
| question = question[index:] | ||
| length = len(question) | ||
| data = { | ||
| "key": settings.google_translation_key, | ||
| "target": target_language, | ||
| "source": source_language, | ||
| "q": temp_question, | ||
| } | ||
| params = urllib.parse.urlencode(data) | ||
|
|
||
| retorn = urllib.request.urlopen(url + "?" + params) | ||
| translated += json.loads(retorn.read())["data"]["translations"][0][ | ||
| "translatedText" | ||
| ] | ||
|
|
||
| data = { | ||
| "key": settings.google_translation_key, | ||
| "target": target_language, | ||
| "source": source_language, | ||
| "q": temp_question, | ||
| } | ||
| params = urllib.parse.urlencode(data) | ||
|
|
||
| retorn = urllib.request.urlopen(url + "?" + params) | ||
| translated += json.loads(retorn.read())["data"]["translations"][0][ | ||
| "translatedText" | ||
| ] | ||
| return translated | ||
|
|
||
|
|
||
| GoogleCloudTranslationAPI = GoogleCloudTranslationAPIFactory() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <configure | ||
| xmlns="http://namespaces.zope.org/zope" | ||
| xmlns:plone="http://namespaces.plone.org/plone" | ||
| i18n_domain="plone" | ||
| > | ||
|
|
||
| <include package="plone.restapi" /> | ||
|
|
||
|
|
||
| <plone:service | ||
| method="GET" | ||
| factory=".translation_services.TranslationServices" | ||
| for="Products.CMFPlone.interfaces.IPloneSiteRoot" | ||
| permission="cmf.ModifyPortalContent" | ||
| name="@translation-services" | ||
| /> | ||
|
|
||
| <plone:service | ||
| method="POST" | ||
| factory=".translate_text.TranslateTextService" | ||
| for="Products.CMFPlone.interfaces.IPloneSiteRoot" | ||
| permission="cmf.ModifyPortalContent" | ||
| name="@translate-text" | ||
| /> | ||
|
|
||
| </configure> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from plone.app.multilingual.browser.translate import translate_text | ||
| from plone.restapi.deserializer import json_body | ||
| from plone.restapi.services import Service | ||
|
|
||
|
|
||
| class TranslateTextService(Service): | ||
| """this endpoints tries to translate the given text into the given language, using the previously registered ExternalTranslationServices""" | ||
|
|
||
| def reply(self): | ||
| body = json_body(self.request) | ||
| source_language = body.get("source_language") | ||
| target_language = body.get("target_language") | ||
| original_text = body.get("original_text") | ||
| service = body.get("service") | ||
|
|
||
| translation = translate_text( | ||
| original_text, source_language, target_language, service | ||
| ) | ||
|
|
||
| if translation is None: | ||
| self.request.response.setStatus(400) | ||
| return dict( | ||
| error=dict( | ||
| type="Translation service not available", | ||
| message="The requested translation service is not available.", | ||
| ) | ||
| ) | ||
|
|
||
| return {"data": translation} |
21 changes: 21 additions & 0 deletions
21
src/plone/app/multilingual/restapi/translation_services.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from plone.app.multilingual.interfaces import IExternalTranslationService | ||
| from plone.restapi.services import Service | ||
| from zope.component import getUtilitiesFor | ||
|
|
||
|
|
||
| class TranslationServices(Service): | ||
| """an endpoint to return all translation services registered in a portal that provide the IExternalTranslationService interface""" | ||
|
|
||
| def reply(self): | ||
| result = [] | ||
|
|
||
| for name, adapter in getUtilitiesFor(IExternalTranslationService): | ||
| item = {} | ||
| item["order"] = adapter.order | ||
| item["is_available"] = adapter.is_available() | ||
| item["available_languages"] = adapter.available_languages() | ||
| item["name"] = name | ||
|
|
||
| result.append(item) | ||
|
|
||
| return sorted(result, key=lambda x: x["order"], reverse=True) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.