Content translation endpoint for plone.restapi
In early March, Kitconcept organised the Beethoven Sprint in Bonn to work on plone.restapi the new REST API product for Plone where we participated both as attendees and sponsors.
During the sprint I worked on providing and endpoint to expose all content-translation related features of Plone. As you may know Plone 5 ships already with plone.app.multilingual which provides translation support for Plone content. Using this product the end user can create content as translation of some other content and create a parallel site in multiple languages. This allows to create direct links from one language to another and some other features that make Plone perfect for multilingual sites, as we showed in Plone Conference 2017 last month in Barcelona.
I had already worked for a client in a similar feature, because we needed to import content from one site to another one and then link the content, so I implemented it quite easily. But later I entered in a test-setup-hell because of the multilingual story in Plone 4. In Plone 4 you can have Archetypes and Dexterity, so the multilingual content story can be implemented using LinguaPlone or plone.app.multilingual. Moreover, different versions of plone.app.multilingual (1.x and 2.x) can be used in Plone 4 and even Plone 4 can be used without any multilingual product installed. And plone.restapi needed to work on plain Plone 4.
After the sprint I had no much time to look at it, and some weeks before the Plone Conference I took some time to rethink and rewrite the feature.
I decided to implement this feature on plone.restapi but only for Plone 5, because Plone 5 has out-of-the-box support for plone.app.multilingual.
And what happens with Plone 4? Don't worry, I created two additional products for Plone 4: collective.restapi.linguaplone and collective.restapi.pam. With those products you get the same features available for Plone 5 in Plone 4 and for both LinguaPlone and plone.app.multilingual.
The API is the same in those 3 cases:
- Issue a GET request to the content's @translations endpoint to get the available translations
- Issue a POST request to the content's @translations endpoint providing the URL of the translation
- Issue a DELETE request to the content's @translations endpoint providing the language for which the translations should be deleted.
Using requests library, these queries can be done like this:
# get the translations
import requests
requests.get('http://myplone.com/en/test-document/@translations', headers={'Accept':'application/json'}, auth=('admin', 'secret'))
# add a translation
import requests
requests.post('http://myplone.com/en/test-document/@translations', headers={'Accept':'application/json'}, json={'id': 'http://myplone.com/es/test-document'}, auth=('admin', 'secret'))
# delete a translations
import requests
requests.delete('http://myplone.com/en/test-document/@translations', headers={'Accept':'application/json'}, json={'language': 'es'}, auth=('admin', 'secret'))
The full documentation about the endpoint is available on read the docs in the following links:
- plone.restapi @translations endpoint documentation
- collective.restapi.linguaplone documentation
- collective.restapi.pam documentation
In theses times when front-end development is getting more and more importance, Plone is ready to provide a REST API to interact with all its features. But plone.restapi is not limited to front-end developers and JavaScript based developers. It is also useful to export and import content in an easy way. Some of our clients are already using it to interact programatically with Plone.
You may be interested in these other articles
JakiZU! promoting food sustainability
Oct 28, 2024