source: extensions/grum_plugins_classes-2/translate.class.inc.php @ 14919

Revision 3395, 4.5 KB checked in by grum, 11 years ago (diff)

Add plugin Grum Plugins Class-2

  • Property svn:executable set to *
Line 
1<?php
2
3/* -----------------------------------------------------------------------------
4  class name: translate
5  class version: 2.0.1
6  date: 2008-05-25
7  ------------------------------------------------------------------------------
8  author: grum at grum.dnsalias.com
9  << May the Little SpaceFrog be with you >>
10  ------------------------------------------------------------------------------
11
12   this classes provides base functions to manage call to "translate.google.com"
13
14   release 2.x use durect call to Google Translate AJAX API, so all functions
15    from PHP class are removed (except for "can_translate")
16   class call API in HTML header, and provide a .js file manage API call
17
18    - constructor translate()
19    - (public) function can_translate($from, $to)   //v1.1
20
21  version 1.1.1
22    - google have changed HTML code for translation page ; change search string of translation result
23    - bug corrected : if language given with uppercase, force them to lowercase
24  version 2.0.0
25    - use of Google Translate javascript API
26        >>  http://code.google.com/apis/ajaxlanguage/
27
28   ---------------------------------------------------------------------- */
29class translate
30{
31  protected $language_list;
32
33  public function translate()
34  {
35    //alloweds from->to transations languages
36    $this->language_list=array_flip(
37      array(
38        'ar', //arabic
39        'bg', //Bulgarian
40        'zh', //Chinese (simplified)
41        'hr', //Croatian
42        'cs', //Czech
43        'da', //Danish
44        "nl", //Dutch
45        'en', //English
46        'fi', //Finnish
47        "fr", //French
48        "de", //German
49        "el", //Greek
50        'hi', //Hindi
51        "it", //Italian
52        "ja", //Japanese
53        "ko", //Korean
54        'no', //Norwegian
55        'pl', //Polish
56        "pt", //Portuguese
57        'ro', //Romanian
58        "ru", //Russian
59        "es", //Spanish
60        'sv' //Swedish
61      )
62    );
63    add_event_handler('loc_end_page_header', array(&$this, 'load_JS'));
64  }
65
66  public function load_JS()
67  {
68    global $template;
69
70    $googleload='
71<script type="text/javascript" src="http://www.google.com/jsapi"></script>
72<script type="text/javascript" src="plugins/'.basename(dirname(__FILE__)).'/google_translate.js"></script>';
73
74    $template->append('head_elements', $googleload);
75
76
77  }
78
79  public function can_translate($from, $to)
80  {
81    if(isset($this->language_list[strtolower($from)])&&isset($this->language_list[strtolower($to)]))
82    {
83      return(true);
84    }
85    else
86    {
87      return(false);
88    }
89  }
90 
91
92/*
93  theses methods are removed for direct use of the Google AJAX API (better choice for
94  performance)
95
96  how to :
97    create one instance of this classe
98    classe can be used by server to know authorized language to be translated
99    all translations have to be made with javascript functions
100
101  =8<===========================================================================
102
103  function set_languages($lang)
104  {
105    $pair=explode('|', strtolower($lang));
106
107    if(isset($this->language_list[$pair[0]])&&isset($this->language_list[$pair[1]]))
108    {
109      $this->from_to_lang=strtolower($lang);
110    }
111    return($this->from_to_lang);
112  }
113
114  function get_languages()
115  {
116    return($this->from_to_lang);
117  }
118
119  function set_input_charset($charset)
120  {
121    $this->input_charset=$charset;
122  }
123
124  function get_input_charset($charset)
125  {
126    return($this->input_charset);
127  }
128
129  function set_output_charset($charset)
130  {
131    $this->output_charset=$charset;
132  }
133
134  function get_output_charset($charset)
135  {
136    return($this->output_charset);
137  }
138
139  function do_translation($text)
140  {
141    if(ini_get('allow_url_fopen')!="1")
142    {
143      return("");
144    }
145
146    $req="http://translate.google.com/translate_t?text=".urlencode($text).
147         "&langpair=".strtolower($this->from_to_lang);
148    if($this->input_charset!="")
149    {
150      $req.="&ie=".$this->input_charset;
151    }
152    if($this->output_charset!="")
153    {
154      $req.="&oe=".$this->output_charset;
155    }
156
157    $handle=fopen($req, "r");
158    if($handle)
159    {
160      $contents="";
161      while (!feof($handle))
162      {
163        $contents .= fread($handle, 4196);
164      }
165      fclose($handle);
166
167      $search="<div id=result_box dir=\"ltr\">";
168      $p = strpos($contents, $search);
169      if($p>0)
170      {
171        $contents=substr($contents, $p+strlen($search));
172        $search="</div>";
173        $p = strpos($contents, $search);
174        $contents=substr($contents, 0, $p);
175      }
176      else
177      {
178        $contents="";
179      }
180
181      return($contents);
182    }
183    else
184    {
185      return("");
186    }
187  }
188*/
189
190} //class
191
192?>
Note: See TracBrowser for help on using the repository browser.