Zend Translations: Google


/ Published in: PHP
Save to your folder(s)

Very usefull translation adapter for simple interfaces where creating translation files is just a bit to much.


Copy this code and paste it in your HTML
  1. /**
  2.  * This is experimental translate adapter using Google Translate services
  3.  * Tries to translate from language specified in $options['source'] to language given in $locale
  4.  * and caches the result *
  5.  * @category Zend
  6.  * @package Zend_Translate
  7.  * @author Michal "Techi" Vrchota <[email protected]>
  8.  * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License v 3.0 */
  9.  
  10. /** Zend_Locale */
  11. require_once 'Zend/Locale.php';
  12. /** Zend_Translate_Adapter */
  13. require_once 'Zend/Translate/Adapter.php';
  14.  
  15.  
  16. class Techi_Translate_Adapter_Google extends Zend_Translate_Adapter{
  17. /**
  18.   * Generates the adapter
  19.   *
  20.   * @param array $data Translation data * @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with locale identifier,
  21.   * see Zend_Locale for more information
  22.   * @param array $options OPTIONAL Options to set
  23.   */
  24. public function __construct($data, $locale = null, array $options = array()) {
  25. parent::__construct($data, $locale, $options);
  26. }
  27.  
  28. /** * Translate message
  29.   *
  30.   * @param string $messageId
  31.   * @param Zend_Locale|string $locale
  32.   * @return string */
  33.  
  34. public function translate($messageId, $locale = null)
  35. {
  36. if ($locale === null) { $locale = $this->_options['locale'];
  37. }
  38. if (!Zend_Locale::isLocale($locale, true)) {
  39. if (!Zend_Locale::isLocale($locale, false)) {
  40. // language does not exist, return original string return $messageId;
  41. }
  42. $locale = new Zend_Locale($locale);
  43. }
  44. $source = $this->_options['source'];
  45.  
  46. if ($source == $locale)
  47. {
  48. return $messageId; }
  49.  
  50.  
  51. $frontendOptions = array(
  52. 'lifetime' => 9999999999, // infinity? 'automatic_serialization' => true
  53. );
  54.  
  55. $backendOptions = array(
  56. 'cache_dir' => './tmp/' // Directory where to put the cache files );
  57.  
  58. // getting a Zend_Cache_Core object
  59. $cache = Zend_Cache::factory('Core',
  60. 'File', $frontendOptions,
  61. $backendOptions);
  62.  
  63. $langpair = $source.'|'.$locale;
  64. $cacheId = 'translation_'.str_replace('|','_',$langpair).'_'.MD5($messageId);
  65.  
  66. if(!$result = $cache->load($cacheId)) {
  67.  
  68.  
  69. $client = new Zend_Http_Client('http://ajax.googleapis.com/ajax/services/language/translate', array(
  70. 'maxredirects' => 0,
  71. 'timeout' => 30));
  72. $client->setParameterGet(array(
  73. 'v' => '1.0',
  74. 'q' => $messageId,
  75. 'langpair' => $langpair
  76. ));
  77. $response = $client->request();
  78.  
  79.  
  80. $data = $response->getBody();
  81.  
  82. $server_result = json_decode($data);
  83.  
  84. $status = $server_result->responseStatus; // should be 200 $details = $server_result->responseDetails;
  85.  
  86. $result = $server_result->responseData->translatedText;
  87.  
  88. $cache->save($result, $cacheId, array('translation'));
  89.  
  90. }
  91.  
  92. return $result;
  93. }
  94.  
  95. /**
  96.   * Load translation data *
  97.   * @param string|array $data
  98.   * @param string $locale Locale/Language to add data for, identical with locale identifier,
  99.   * see Zend_Locale for more information
  100.   * @param array $options OPTIONAL Options to use */
  101. protected function _loadTranslationData($data, $locale, array $options = array())
  102. {
  103. $options = $options + $this->_options;
  104. if (($options['clear'] == true) || !isset($this->_translate[$locale])) { $this->_translate[$locale] = array();
  105. }
  106.  
  107.  
  108. $this->_translate[$locale] = $data + $this->_translate[$locale] + array($locale); }
  109.  
  110. /**
  111.   * returns the adapters name
  112.   *
  113.   * @return string
  114.   */
  115. public function toString()
  116. {
  117. return "Google";
  118. }
  119. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.