Zend Framework Source Code Snippets

Google Translate Adapter

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
109
110111
112
113
114
115116
117
118
119
120121
122
123
124
125126
127
128
129
130131
132
133
134
135136
137
138
139
140141
142
143
144
145146
147
<?php
/**
 * This is experimental translate adapter using Google Translate services
 * Tries to translate from language specified in $options['source'] to language given in $locale
 * and caches the result *
 * @category   Zend
 * @package    Zend_Translate
 * @author     Michal "Techi" Vrchota <michal.vrchota@gmail.com>
 * @license    http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License v 3.0 */
 
/** Zend_Locale */
require_once 'Zend/Locale.php';
 /** Zend_Translate_Adapter */
require_once 'Zend/Translate/Adapter.php';
 
 
class Techi_Translate_Adapter_Google extends Zend_Translate_Adapter{
        /**
     * Generates the adapter
     *
     * @param  array               $data     Translation data     * @param  string|Zend_Locale  $locale   OPTIONAL Locale/Language to set, identical with locale identifier,
     *                                       see Zend_Locale for more information
     * @param  array               $options  OPTIONAL Options to set
     */
        public function __construct($data, $locale = null, array $options = array())        {
                parent::__construct($data, $locale, $options);
        }
        
        /**         * Translate message
         *
         * @param string $messageId
         * @param Zend_Locale|string $locale
         * @return string         */
 
        public function translate($messageId, $locale = null)
        {
                if ($locale === null) {                        $locale = $this->_options['locale'];
                }
                if (!Zend_Locale::isLocale($locale, true)) {
                        if (!Zend_Locale::isLocale($locale, false)) {
                                // language does not exist, return original string                                return $messageId;
                        }
                        $locale = new Zend_Locale($locale);
                }
                 $source = $this->_options['source'];
 
                if ($source == $locale)
                {
                        return $messageId;                }
 
 
                $frontendOptions = array(
                'lifetime' => 9999999999, // infinity?                'automatic_serialization' => true
                );
 
                $backendOptions = array(
                'cache_dir' => './tmp/' // Directory where to put the cache files                );
 
                // getting a Zend_Cache_Core object
                $cache = Zend_Cache::factory('Core',
                'File',                $frontendOptions,
                $backendOptions);
 
                $langpair = $source.'|'.$locale;
                 $cacheId = 'translation_'.str_replace('|','_',$langpair).'_'.MD5($messageId);
 
                if(!$result = $cache->load($cacheId)) {
 
  
                        $client = new Zend_Http_Client('http://ajax.googleapis.com/ajax/services/language/translate', array(
                        'maxredirects' => 0,
                        'timeout'      => 30));
                         $client->setParameterGet(array(
                        'v' => '1.0',
                        'q' => $messageId,
                        'langpair' => $langpair
                        )); 
                        $response = $client->request();
 
 
                         $data = $response->getBody();
 
                        $server_result = json_decode($data);
 
                        $status = $server_result->responseStatus; // should be 200                        $details = $server_result->responseDetails;
 
                        $result = $server_result->responseData->translatedText;
 
                         $cache->save($result, $cacheId, array('translation'));
 
                }
 
                return $result; 
        }
 
        /**
     * Load translation data     *
     * @param  string|array  $data
     * @param  string        $locale  Locale/Language to add data for, identical with locale identifier,
     *                                see Zend_Locale for more information
     * @param  array         $options OPTIONAL Options to use     */
        protected function _loadTranslationData($data, $locale, array $options = array())
        {
                $options = $options + $this->_options;
                if (($options['clear'] == true) ||  !isset($this->_translate[$locale])) {                        $this->_translate[$locale] = array();
                }
 
 
                $this->_translate[$locale] = $data + $this->_translate[$locale] + array($locale);        }
 
        /**
     * returns the adapters name
     *     * @return string
     */
        public function toString()
        {
                return "Google";        }
}

Comments

umpirsky umpirsky
3 years ago

Nice work! Is it fast?

Btw, if you like gettext, and want to have translations in your pocket and change them, but still lazy to translate to all languages, try my po file translator http://translate.umpirsky.com/

snetts snetts
1 year ago

This is a great Language Adapter for the Zend Framework. I am pretty new to Zend programming, so could you please elaborate on how to apply it to an application. Thanks.

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Translate adapter using Google Translate API

$this->_translate = new Zend_Translate('Techi_Translate_Adapter_Google', array(), $this->_locale, array('source' => 'cs'));

Snippet details

Created:
Techi Techi
3 years ago
Edited:
Techi Techi
3 years ago
Revision Id:
34
Edit Message:
Initial Release
Tags:
zend_translate
Comments:
2
Views:
5233
Points:
1 (1 votes)

History

r34

Initial Release

Techi Techi
3 years ago