languages
[ class tree: languages ] [ index: languages ] [ all elements ]

Source for file ajax_translate.php

Documentation is available at ajax_translate.php

  1. <?php
  2. /**
  3.  * @package languages
  4.  * @subpackage functions
  5.  * @version $Header$
  6.  *
  7.  *  Copyright (c) 2005 bitweaver.org
  8.  *  Copyright (c) 2004-2005, Christian Fowler, et. al.
  9.  *  All Rights Reserved. See below for details and a complete list of authors.
  10.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  11.  */
  12.  
  13. /**
  14.  * Initialization
  15.  */
  16. $gLightweightScan = TRUE;
  17. require_once( '../kernel/setup_inc.php' );
  18.  
  19. $translation = NULL;
  20.  
  21. if( !empty( $_REQUEST['lang'] ) && !empty( $_REQUEST['source_hash'] ) ) {
  22.     if( $masterString = $gBitLanguage->getMasterString( $_REQUEST['source_hash'] ) ) {
  23.         // convert smarty to tags so it is shielded from translation
  24.         $preppedMaster = preg_replace( '/\{/', '<smarty ', $masterString );
  25.         // needs to be a full tag so we can cleanly de-tagify after translation
  26.         $preppedMaster = preg_replace( '/}/', '></smarty>', $preppedMaster );
  27.  
  28.         $googleUrl = "https://www.googleapis.com/language/translate/v2?key=".$gBitSystem->getConfig('google_api_key')."&source=en&target=".$_REQUEST['lang']."&q=".urlencode( $preppedMaster );
  29.  
  30.         if( $fh = fopen( $googleUrl, "r" ) ) {
  31.             $jsonResponse = fread( $fh, 8192 );
  32.             $data = json_decode( $jsonResponse );
  33.             fclose( $fh );
  34.         }
  35.         if( !empty( $data->data->translations[0]->translatedText ) ) {
  36.             $translation = urldecode( $data->data->translations[0]->translatedText );
  37.             if( $translation != $masterString ) {
  38.                 //detagify
  39.                 $preppedTranslation = preg_replace( '/<smarty /', '{', $translation );
  40.                 // needs to be a full tag so we can cleanly de-tagify after translation
  41.                 $preppedTranslation = preg_replace( '/><\/smarty>/', '}', $preppedTranslation );
  42.                 
  43.                 print json_encode( array( 'lang_code' => $_REQUEST['lang'], 'source_hash' => $_REQUEST['source_hash'], 'translation' => $preppedTranslation ) );
  44.             }
  45.         }
  46.     }
  47. }

Documentation generated on Wed, 29 Jul 2015 13:55:33 +0000 by phpDocumentor 1.5.0-lsces