Skip to content

Example: Language Translation

Owen Swerkstrom edited this page Jul 13, 2015 · 15 revisions

Example: a language translation service

The manifest for a language translation Motion service might look like so:

{
    “name”: “translate”,
    “description”: “Translates a message from one language to another”,
    “requests”: [ “subject”, “body” ],
    “requires”: [ ],
    “modifies”: [ “subject”, “body”, “language” ],
    “configurable”: true,
    “endpoint”: “https://translate.example.com/translate/”
}

Note that this service has "configurable": true. That means that if we then POST a blank document to it, the service will create a new configuration instance, and serve up a human-interactive page with which may be used to select the desired configuration settings.

The mechanisms used to store these settings within the service are not defined by Motion; we care only that we now have a path at which our settings may be changed and to which we will POST our messages to be processed.

C:POST /translate/translate/ HTTP/1.1
C:Content-Length: 0
C:
S:HTTP/1.1 201 Created
S:Location: /translate/options/a1e9
S:

We may now POST the required fields from our message data to the service, and receive the translated versions.

C:POST /translate/options/a1e9 HTTP/1.1
C:Content-Type: application/json
C:
C:{
C:    “subject”: “Bonjour!”,
C:    “body”: {
C:        “text”: “j'oublie tout”,
C:        “html”: “<html><body>j'oub...
C:    }
C:}
C:
S:HTTP/1.1 200 Ok
S:Content-Type: application/json
S:
S:{
S:    “language”: “en”,
S:    “subject”: “Hello!”,
S:    “body”: {
S:        “text”: “I forget everything”,
S:        “html”: “<html><body>I forg...
S:    }
S:}

Refinements

An improvement over this initial design might be to create two services, a language identifier and a language translator. The first would only determine the language used in the message.

C:POST /whatlanguage HTTP/1.1
C:Content-Type: application/json
C:
C:{
C:    “subject”: “Bonjour!”,
C:    “body”: {
C:        “text”: “j'oublie tout”,
C:        “html”: “<html><body>j'oub...
C:    }
C:}
C:
S:HTTP/1.1 200 Ok
S:Content-Type: application/json
S:
S:{
S:    “language”: “en”
S:}

This way, an MTA (or archiver, or mail client, or anything) would then have the option to act in a number of ways based on what language is detected, including using the full translation service.

if (language == “de” ||
    language == “fr” ||
    language == “es”
) {
   translate(en);
} else {
   /* Is this spam? */
   delete();
}
Clone this wiki locally