Skip to content

Commit 0199aac

Browse files
authored
Merge branch '6.0' into dependabot/github_actions/actions/cache-5
2 parents 3d87058 + 0fb2b20 commit 0199aac

File tree

4 files changed

+76
-71
lines changed

4 files changed

+76
-71
lines changed

docs/_static/translate.svg

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/backend/upgrading/version-specific-migration/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ upgrade-to-python3
2727
upgrade-zodb-to-python3
2828
upgrade-to-60
2929
upgrade-to-61
30-
upgrade-to-62
3130
migrate-to-volto
3231
```

docs/backend/upgrading/version-specific-migration/upgrade-to-62.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

docs/i18n-l10n/use-an-external-translation-service.md

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,88 @@ myst:
1414
When translating content items in Plone, you can connect to an external translation service to translate your content.
1515

1616

17-
The `plone.app.multilingual` product that turns Plone into a multilingual content site supports a pluggable way to hook any translation service into Plone.
17+
## Using Google Cloud Translation API
1818

19-
To do so, one has to implement a utility that implements an `IExternalTranslationService` interface.
19+
The `plone.app.multilingual` product that turns Plone into a multilingual-content site supports [Google Cloud Translation API](https://docs.cloud.google.com/translate/docs/reference/rest), which allows the content editor to use its translations.
2020

21-
This utility class must implement the `IExternalTranslationService` interface from `plone.app.multilingual.interfaces` and it should provide at least these methods and an attribute:
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.
2223

23-
`is_available()`
24-
: Returns `True` if the service is enabled and ready.
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.
2526

26-
`available_languages()`
27-
: Returns a list of supported language codes or pairs that can be used (source, target).
28-
29-
`translate_content(content, source_language, target_language)`
30-
: Performs the translation and returns the translated text.
31-
32-
`order`
33-
: The order in which this utility will be executed.
34-
This way, one can prioritize some services over others with given conditions.
35-
36-
After doing so, as a content editor, when you edit a translation of a given content page, a translate icon <img alt="Translate icon" src="../_static/translate.svg" class="inline"> will display next to the original content.
37-
38-
When you click this icon, it will invoke the translation utility, and the translation obtained through the service will be entered automatically in the corresponding field.
39-
40-
Plone does not implement this interface by itself in any of its utilities.
41-
42-
You'll need to use an external package that offers this service as described in {ref}`pre-configured-services-label`, or create your own utility.
43-
44-
(pre-configured-services-label)=
45-
46-
## Using the translation service with pre-configured services
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+
```
4731

48-
To use some external tools, the Plone community has implemented a package called [`collective.translators`](https://github.com/collective/collective.translators) that implements this functionality for AWS, Deepl, Deepseek, Google Translate, Libre Translate, and Ollama.
4932

50-
Each of those services provides a control panel to tweak the configuration, including API keys, languages, service endpoints, and other configuration items.
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+
```
5196

5297
```{note}
53-
The usage of some of those services may create extra cost for the site administrator.
54-
Check the terms of use of each of the tools for details.
98+
Due to the way that the Google Translate integration is built in `plone.app.multilingual`, you will need to enter something in the {guilabel}`Google Translate API Key` field in the {guilabel}`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.
55101
```

0 commit comments

Comments
 (0)