Translate anything

This simple example will create a small form that calls an external translation service to translate the content entered on that form. The result will be displayed underneath the source text that was entered.

The form will be called "translate_anything" and can then be called in a regular SPIP template file using the tag #FORMULAIRE_TRANSLATE_ANYTHING or within an article by using <formulaire|translate_anything>.

As with most CVT forms, it operates using two files:

  • formulaires/translate_anything.html for the HTML section
  • formulaires/translate_anything.php for the PHP analysis and processing functions.

The HTML template

The template for the form will receive two data entry fields of the textarea type: the first for writing the content to be translated, and the second to display the results of the translation once the calculation has been performed. This second field is only displayed when it actually has some content.

  1. <div class="formulaire_spip formulaire_#FORM">
  2.  
  3. [<p class="reponse_formulaire reponse_formulaire_erreur">(#ENV*{message_erreur})</p>]
  4. [<p class="reponse_formulaire reponse_formulaire_ok">(#ENV*{message_ok})</p>]
  5.  
  6. <form action="#ENV{action}" method="post"><div>
  7. #ACTION_FORMULAIRE{#ENV{action}}
  8. <ul>
  9. [(#SET{erreurs,[(#ENV**{erreurs}|table_valeur{traduire})]})]
  10. <li class="editer_traduire obligatoire[ (#GET{erreurs}|oui)erreur]">
  11. <label for="traduire">Source text</label>
  12. [<span class='erreur_message'>(#GET{erreurs})</span>]
  13. <textarea name='traduire' id='champ_traduire'>#ENV{traduire}</textarea>
  14. </li>
  15. [
  16. [(#SET{erreurs,[(#ENV**{erreurs}|table_valeur{traduction})]})]
  17. <li class="editer_traduction[ (#GET{erreurs}|oui)erreur]">
  18. <label for="traduction">Translated text</label>
  19. [<span class='erreur_message'>(#GET{erreurs})</span>]
  20. <textarea name='traduction' id='champ_traduction'>(#ENV{traduction})</textarea>
  21. </li>
  22. ]
  23. </ul>
  24. <p class="boutons"><input type="submit" class="submit" value="Translate" /></p>
  25. </div></form>
  26. </div>

Download

The two fields named "traduire" and "traduction" (source and destination for the translation). The same template could be written using the "Saisies" plugin with the content between <ul> and </ul> represented as follows:

  1. <ul>
  2. [(#SAISIE{textarea, traduire, obligatoire=oui, label=Traduire})]
  3.  
  4. [(#ENV{traduction}|oui)
  5. [(#SAISIE{textarea, traduction, label=Traduction})]
  6. ]
  7. </ul>

Download

Loading, verifying and processing

The "loading" of the form, declared in the formulaires/translate_anything.php file, must specify that is is adding the two "traduire" and "traduction" fields into the template’s context:

  1. function formulaires_translate_anything_charger_dist() {
  2. $contexte = array(
  3. 'traduire' => '',
  4. 'traduction' => '',
  5. );
  6. return $contexte;
  7. }

Download

The "verify" function simply needs to test if there has actually been some content entered into the "traduire" field and return an error is there hasn’t:

  1. function formulaires_translate_anything_verifier_dist() {
  2. $erreurs = array();
  3. if (!_request('traduire')) {
  4. $erreurs['message_erreur'] = "You have not entered any text to translate - is your keyboard broken?";
  5. $erreurs['traduire'] = "Normally that is how you enter text, isn't it?";
  6. }
  7. return $erreurs;
  8. }

Download

It is with the "process" function that things now get a little complicated. The content needs to be sent to a remote service (we use Google Translate in this example), the return data retrieved and processed, and then displayed on our form.

To do all this, the script starts by calculating the URL for the remote service based on that service’s published API. We use SPIP’s parametre_url PHP function to cleanly add the variables to the service’s URL. Thanks to another function, recuperer_page which is used to retrieve the code returned by a call to an URL, the service’s returned data is stored in the $trad variable.

The service returns the data formatted in JSON format, so it must be extricated using the json_decode function. depending on the information returned, the translation will be determined as having been successful or not. The message is adapted depending on this outcome.

  1. // http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en%7Cit
  2. define('URL_GOOGLE_TRANSLATE', "http://ajax.googleapis.com/ajax/services/language/translate");
  3.  
  4. function formulaires_translate_anything_traiter_dist() {
  5.  
  6. // create the google api URL
  7. $texte = _request('traduire');
  8. $url = parametre_url(URL_GOOGLE_TRANSLATE, 'v', '1.0', '&');
  9. $url = parametre_url($url, 'langpair', 'fr|en', '&');
  10. $url = parametre_url($url, 'q', $texte, '&');
  11.  
  12. // load the text as translated by google (returned as JSON code)
  13. include_spip('inc/distant');
  14. $trad = recuperer_page($url);
  15.  
  16. // warning: uses PHP 5.2
  17. $trad = json_decode($trad, true); // true = retour array et non classe
  18.  
  19. // retrieve the results if OK
  20. if ($trad['responseStatus'] != 200) {
  21. set_request('traduction', '');
  22. return array(
  23. "editable" => true,
  24. "message_erreur" => "Bad luck, Google couldn't help!"
  25. );
  26. }
  27.  
  28. // send the data to be loaded
  29. set_request('traduction', $trad['responseData']['translatedText']);
  30.  
  31. // message
  32. return array(
  33. "editable" => true,
  34. "message_ok" => "And here's the translation!",
  35. );
  36. }

Download

The set_request() functions forces the saving of a variable value that can then later be retrieved using _request(). This allows the next loading of the form to retrieve the value of the "traduction" field to send it into the template’s context.

Note: It is possible that a cleaner method could be developed for future versions of SPIP in order to transit the data between the processing and loading phases using a new parameter in the processing return table.

Author Mark Baber Published : Updated : 02/06/10

Translations : English, français