summaryrefslogtreecommitdiff
path: root/ajax_translate.php
blob: 91763e627683497b7b35726a3e6b762d98204b48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
 * @package languages
 * @subpackage functions
 * @version $Header$
 *
 * Copyright (c) 2005 bitweaver.org
 * Copyright (c) 2004-2005, Christian Fowler, et. al.
 * All Rights Reserved. See below for details and a complete list of authors.
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
 */

/**
 * Initialization
 */
$gLightweightScan = TRUE;
require_once( '../kernel/includes/setup_inc.php' );

$translation = NULL;

if( !empty( $_REQUEST['lang'] ) && !empty( $_REQUEST['source_hash'] ) ) {
	if( $masterString = $gBitLanguage->getMasterString( $_REQUEST['source_hash'] ) ) {
		// convert smarty to tags so it is shielded from translation
		$preppedMaster = preg_replace( '/\{/', '<smarty ', $masterString );
		// needs to be a full tag so we can cleanly de-tagify after translation
		$preppedMaster = preg_replace( '/}/', '></smarty>', $preppedMaster );

		$googleUrl = "https://www.googleapis.com/language/translate/v2?key=".$gBitSystem->getConfig('google_api_key')."&source=en&target=".$_REQUEST['lang']."&q=".urlencode( $preppedMaster );

		if( $fh = fopen( $googleUrl, "r" ) ) {
			$jsonResponse = fread( $fh, 8192 );
			$data = json_decode( $jsonResponse );
			fclose( $fh );
		}
		if( !empty( $data->data->translations[0]->translatedText ) ) {
			$translation = urldecode( $data->data->translations[0]->translatedText );
			if( $translation != $masterString ) {
				//detagify
				$preppedTranslation = preg_replace( '/<smarty /', '{', $translation );
				// needs to be a full tag so we can cleanly de-tagify after translation
				$preppedTranslation = preg_replace( '/><\/smarty>/', '}', $preppedTranslation );
				
				print json_encode( array( 'lang_code' => $_REQUEST['lang'], 'source_hash' => $_REQUEST['source_hash'], 'translation' => $preppedTranslation ) );
			}
		}
	}
}