Skip to content

Commit 41696ab

Browse files
erralstevepiercy
andauthored
document how to use Google Translate to translate content (#1723)
* document how to use Google Translate to translate content * add a note saying that one needs to enter something in the API key field * Review of PR --------- Co-authored-by: Steve Piercy <web@stevepiercy.com>
1 parent 4ae0e9a commit 41696ab

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

docs/i18n-l10n/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,6 @@ language-negotiation-volto
160160
translating-content
161161
contributing-translations
162162
resync-translations
163+
use-an-external-translation-service
163164
```
164165

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
myst:
3+
html_meta:
4+
"description": "When translating content items in Plone, you can connect to an external translation service to translate your content."
5+
"property=og:description": "When translating content items in Plone, you can connect to an external translation service to translate your content."
6+
"property=og:title": "Use an external translation service to translate content"
7+
"keywords": "Plone, Internationalization, i18n, language, translate, content, localization, l10n"
8+
---
9+
10+
(use-an-external-translation-service-label)=
11+
12+
# Use an external translation service to translate content
13+
14+
When translating content items in Plone, you can connect to an external translation service to translate your content.
15+
16+
17+
## Using Google Cloud Translation API
18+
19+
The `plone.app.multilingual` product that turns Plone into a multilingual-content site supports [Google Cloud Translation API](https://cloud.google.com/translate/docs/reference/rest), which allows the content editor to use its translations.
20+
21+
To use this service as a site administrator, you need to create a project in Google Cloud, enable the Cloud Translation API, and create an API key under the Credentials of the Google Cloud Console.
22+
You should enter this API key in the {guilabel}`Multilingual Settings` control panel in Plone.
23+
24+
After doing so, as a content editor, when you edit a translation of a given content page, an icon will display next to the original content.
25+
When you click this icon, it invokes the Google Cloud Translation API, and the translation obtained through the service will be entered automatically in the corresponding field.
26+
27+
```{note}
28+
The usage of Google Cloud Translation API may create extra cost for the site administrator.
29+
See [Cloud Translation pricing](https://cloud.google.com/translate/pricing) for details.
30+
```
31+
32+
33+
## Using other translation services
34+
35+
If you want to use another service beside Google Cloud Translation API, you will need to override the view that calls Google Cloud Translation API.
36+
37+
To do so, `plone.app.multilingual` registers a view called `gtranslation_service`.
38+
Its code is in [`plone.app.multilingual.brwoser.translate.gtranslation_service_dexterity`](https://github.com/plone/plone.app.multilingual/blob/7aedd0ab71d3edf5d1fb4cb86b9f611d428ed76b/src/plone/app/multilingual/browser/translate.py#L52).
39+
This view gets three parameters:
40+
41+
`context_uid`
42+
: The UID of the object to be translated.
43+
44+
`field`
45+
: The name of the field of the object that needs to be translated.
46+
This view's job is to extract the value of that field from the object.
47+
48+
`lang_source`
49+
: The source language code.
50+
51+
The first part of the view—that which gets the object and the field content to be translated—can be copied from the original code.
52+
You need to write only the call to the translation service.
53+
The required code would be something like the following example:
54+
55+
```python
56+
class TranslateUsingMyService(BrowserView):
57+
def __call__(self):
58+
if self.request.method != "POST" and not (
59+
"field" in self.request.form.keys()
60+
and "lang_source" in self.request.form.keys()
61+
):
62+
return _("Need a field")
63+
else:
64+
manager = ITranslationManager(self.context)
65+
context_uid = self.request.form.get("context_uid", None)
66+
if context_uid is None:
67+
# try with context if no translation uid is present
68+
manager = ITranslationManager(self.context)
69+
else:
70+
catalog = getToolByName(self.context, "portal_catalog")
71+
brains = catalog(UID=context_uid)
72+
if len(brains):
73+
context = brains[0].getObject()
74+
manager = ITranslationManager(context)
75+
else:
76+
manager = ITranslationManager(self.context)
77+
78+
registry = getUtility(IRegistry)
79+
settings = registry.forInterface(
80+
IMultiLanguageExtraOptionsSchema, prefix="plone"
81+
)
82+
lang_target = ILanguage(self.context).get_language()
83+
lang_source = self.request.form["lang_source"]
84+
orig_object = manager.get_translation(lang_source)
85+
field = self.request.form["field"].split(".")[-1]
86+
if hasattr(orig_object, field):
87+
question = getattr(orig_object, field, "")
88+
if hasattr(question, "raw"):
89+
question = question.raw
90+
else:
91+
return _("Invalid field")
92+
93+
# And here do the call to the external translation service
94+
return call_to_my_service(question, lang_target, lang_source)
95+
```
96+
97+
```{note}
98+
Due to the way that the Google Translate integration is built in `plone.app.multilingual`, you will need to enter something in the {guilable}`Google Translate API Key` field in the {guilable}`Multilingual Settings`
99+
control panel of your site.
100+
It doesn't need to be a valid Google Translate API Key; it can be a random string.
101+
```

0 commit comments

Comments
 (0)