Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-19195

translation of post's content from any language to any language (dynamically)

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Inactive
    • Affects Version/s: 1.9
    • Fix Version/s: None
    • Component/s: Forum
    • Labels:
      None
    • Difficulty:
      Easy
    • Affected Branches:
      MOODLE_19_STABLE

      Description

      integrate google's translation service into forum's posts to dynamically translating
      the current post from any source language to any destination language. (on the fly)

      was inspired by KDE's brainstorm forum.

      i added a forum's admin setting (system-wide) to enable or disable this feature.
      if it becomes popular we can move it into the forum's setting ? (maybe)

      here is the patch, mostly, one file : "forum/lib.php" ( in three different places )
      (and the settings.php file too + local language forum.php file)

      inside function forum_print_post()
      just after :
      static $strmarkread, $strmarkunread;

      please add:

      if ($CFG->forum_googletranslator)

      { // Google Translator API / Script echo <<<GTRANSLATOR <script type="text/javascript" src="http://www.google.com/jsapi"></script> GTRANSLATOR; } next, just after the lines: echo '</a> ('.get_string('numwords', '', $numwords).')...</div>'; }

      else {
      // Print whole message

      add :

      if ($CFG->forum_googletranslator)

      { // Empty translation anchor (will be filled with translated text) echo '<div id="translation'.$post->id.'"></div>'; }

      echo '<div id="post'.$post->id.'" class="posting">';
      .
      .
      .
      echo '</div>';
      echo $attachedimages;

      next, after the lines :

      echo '<div class="commands">';
      echo implode(' | ', $commands);
      echo '</div>';

      add:

      // Translation
      if ($CFG->forum_googletranslator) {
      echo <<<TRANSLATOR
      <script type="text/javascript">

      var source$post->id = 'en';
      var dest$post->id = 'he';

      function set_source_lang(source) {
      return source$post->id = source;
      }

      // Initialize version 1.0 of Google AJAX API
      google.load("language", "1");

      function translate$post->id(lang) {
      var source = document.getElementById("post$post->id").innerHTML;
      var len = content.length;

      // Google Language API accepts 500 characters per request
      var words = 500;

      // This is for English pages, you can change the
      // sourcelang variable for other languages

      set_dest_lang(lang);
      //var sourcelang = source$post->id;
      document.getElementById("translation$post->id").innerHTML = "";

      for(i=0; i<=(len/words); i++) {
      google.language.translate (source.substr(i*words, words),
      source$post->id, lang, function (result) {
      if (!result.error)

      { document.getElementById("translation$post->id").innerHTML = document.getElementById("translation$post->id").innerHTML + result.translation; }

      }); }

      // Hide the text written in the original language
      document.getElementById("post$post->id").style.display = 'none';
      return false;
      }

      // Switch to the original language

      function original$post->id()

      { document.getElementById("translation$post->id").style.display='none'; document.getElementById("post$post->id").style.display = 'block'; return false; }

      </script>
      <select onchange="source$post->id = this.options[this.selectedIndex].value;">
      <option value="en">???? ??? ????</option>
      <option value="en">English</option>
      <option value="he">?????</option>
      <option value="pt">português</option>
      <option value="fr">français</option>
      <option value="ar">???????</option>
      <option value="it">italiano</option>
      <option value="ru">p??????</option>
      <option value="es">español</option>
      </select>
      <select id="translateto$post->id" onchange="translate$post->id(this.options[this.selectedIndex].value);">
      <option value="he">???? ??? ?????</option>
      <option value="he">?????</option>
      <option value="en">English</option>
      <option value="pt">português</option>
      <option value="fr">français</option>
      <option value="ar">???????</option>
      <option value="it">italiano</option>
      <option value="ru">p??????</option>
      <option value="es">español</option>
      </select>
      <a href="#p$post->id" onclick="original$post->id();">[????? ????? ??????]</a>
      <!-a href="#p$post>id" onclick="translate$post->id(document.getElementById('translateto$post->id').options[document.getElementById('translateto$post->id').selectedIndex].value);">?????</a-->
      <br/>
      TRANSLATOR;

      // Sample list of languages (more available from Google Translate)
      // <option value="en">English</option>
      // <option value="he">?????</option>
      // <option value="de">deutsch</option>
      // <option value="pt">português</option>
      // <option value="fr">français</option>
      // <option value="ja">???</option>
      // <option value="ar">???????</option>
      // <option value="it">italiano</option>
      // <option value="ru">p??????</option>
      // <option value="po">polski</option>
      // <option value="zh-CN">??</option>
      // <option value="es">español</option>
      // <option value="ko">???</option>
      // <option value="nl">nederlands</option>
      // <option value="hi">?????? </option>
      // <option value="el">????????</option>
      // <option value="ro">român?</option>
      }

      one more file, add a new setting to the "forum/setting.php" file, at the end:

      $settings->add(new admin_setting_configcheckbox('forum_googletranslator', get_string('googletranslator', 'forum'),
      get_string('configgoogletranslator', 'forum'), 0));

      and some local language strings (for the admin) inside forum.php
      $string['googletranslator'] = 'enable translation';
      $string['configgoogletranslator'] = 'enables system wide posts translation in forums';

      enjoy

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Votes:
                2 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: