-
Notifications
You must be signed in to change notification settings - Fork 0
/
makena_translator.php
198 lines (159 loc) · 6.46 KB
/
makena_translator.php
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/html_dom_parser_src/simple_html_dom.php');
require($_SERVER['DOCUMENT_ROOT'] . '/transl_src/GoogleTranslate.php');
/**
* This is a class which is created for translating websites (special for "MAKENA")
*/
class DefTranslator
{
private $html_DOM_code;
private $lang_in;
private $lang_out;
private $whole_body_text_arr = array();
private $entity_patterns = array('/…/','/–/','/ /','/&(r|l)aquo;/');
private $entity_replacement = array('...',' - ',' ','"');
private $lang_alph_regex;
private $transl_class;
function url_get_contents ($Url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function __construct($UrlName, $TargetLang = 'en', $SourceLang = 'ru')
{
if ( ! class_exists( 'simple_html_dom_node' ) ) die( 'simple_html_dom_parser was not found' );
if ( preg_match('/^https?:\/\/.*/', $UrlName) )
{
if ( ! function_exists('curl_init') ) die( 'CURL is not installed!' );
$this->html_DOM_code = str_get_html( $this->url_get_contents($UrlName) );
}
else
$this->html_DOM_code = str_get_html( file_get_contents($UrlName) );
$this->lang_in = $SourceLang;
$this->lang_out = $TargetLang;
switch ($this->lang_in)
{
case 'ru':
$this->lang_alph_regex = '[а-яА-ЯёЁ]';
break;
case 'en':
$this->lang_alph_regex = '[a-zA-Z]';
break;
default:
$this->lang_alph_regex = '[а-яА-ЯёЁ]';
break;
}
}
private function DOM_get_body_text($DOM_webpage) { return $DOM_webpage->find('body',0)->find('text'); }
private function string_modificate($string) { return preg_replace($this->entity_patterns, $this->entity_replacement, trim($string)); }
private function contains_source($string) { return preg_match( "/{$this->lang_alph_regex}/", $string ); }
private function is_white_or_empty($string) { return ctype_space($string) || $string === "" ; }
private function array_modificate($DOM_page_body_text)
{
$fixed_arr = array();
for ($raw = 0, $size = count($DOM_page_body_text); $raw < $size; $raw++)
if ( ! $this->is_white_or_empty( $DOM_page_body_text[$raw] ) && $this->contains_source( $DOM_page_body_text[$raw] ) )
$fixed_arr[] = $this->string_modificate($DOM_page_body_text[$raw]->plaintext);
return $fixed_arr;
}
private function GetTranslatedText($text) { return $this->transl_class->translate($this->lang_in, $this->lang_out, $text); }
private function array_get_trans($source_arr)
{
$mod_source_content_string = implode("\n", $source_arr);
if (mb_strlen($mod_source_content_string) <= 5000)
return explode("\n",$this->GetTranslatedText($mod_source_content_string));
//separation
$sep_mod_source_content_string_arr = array();
while (mb_strlen($mod_source_content_string) > 5000)
{
$sep_mod_source_content_string_arr[] = substr( $mod_source_content_string, 0, strpos($mod_source_content_string, "\n", 4500) + 1);
$mod_source_content_string = substr( $mod_source_content_string, strpos($mod_source_content_string, "\n", 4500) );
}
$sep_mod_source_content_string_arr[] = $mod_source_content_string;
//translating and merging
$target_content_string = $this->GetTranslatedText($sep_mod_source_content_string_arr[0]);
for ($raw = 1, $size = count($sep_mod_source_content_string_arr); $raw < $size; $raw++)
$target_content_string .= "\n" . $this->GetTranslatedText($sep_mod_source_content_string_arr[$raw]);
//initializing
return explode("\n",$target_content_string);
}
private function TranslateUnbodyContent()
{
$outbody_string_source = '';
$outbody_string_target_arr = array();
$outbody_DOM_code = $this->html_DOM_code->find('*[placeholder],meta[name=description],title');
foreach ( $outbody_DOM_code as $value )
{
if ( $this->contains_source( $value->{'placeholder'} ) )
{
$outbody_string_source .= "\n" . $value->{'placeholder'};
continue;
}
if ( $this->contains_source( $value->{'content'} ) )
{
$outbody_string_source .= "\n" . $value->{'content'};
continue;
}
if ( $this->contains_source( $value->innertext ) )
$outbody_string_source .= "\n" . $value->innertext;
}
$outbody_string_target_arr = explode("\n", $this->GetTranslatedText($outbody_string_source));
$outbody_string_target_arr_counter = 0;
foreach ( $outbody_DOM_code as $value )
{
if ( $this->contains_source( $value->{'placeholder'} ) )
{
$value->{'placeholder'} = $outbody_string_target_arr[$outbody_string_target_arr_counter];
$outbody_string_target_arr_counter++;
continue;
}
if ( $this->contains_source( $value->{'content'} ) )
{
$value->{'content'} = $outbody_string_target_arr[$outbody_string_target_arr_counter];
$outbody_string_target_arr_counter++;
continue;
}
if ( $this->contains_source( $value->innertext ) )
{
$value->innertext = $outbody_string_target_arr[$outbody_string_target_arr_counter];
$outbody_string_target_arr_counter++;
}
}
}
private function DOM_get_changed_content($DOM_body_text,$translated_array)
{
for ($source_raw = 0, $target_raw = 0, $s_1 = count($DOM_body_text), $s_2 = count($translated_array);
$source_raw < $s_1 && $target_raw < $s_2;
$source_raw++)
if ( ! $this->is_white_or_empty( $DOM_body_text[$source_raw] ) && $this->contains_source( $DOM_body_text[$source_raw] ) )
{
$DOM_body_text[$source_raw]->innertext = $translated_array[$target_raw];
$target_raw++;
}
return $DOM_body_text;
}
private function is_transl_avaliable()
{
if ( $this->html_DOM_code === false ) return false; //can't reach resource
if ( ! class_exists( 'GoogleTranslate' ) ) return false;
if ( $this->lang_in !== 'en' && $this->lang_in !== 'ru' ) return false; // source langs are ru and en only
return true;
}
private function is_transl_needed() { return $this->lang_in !== $this->lang_out; }
public function Translate()
{
if ( $this->is_transl_avaliable() === false || $this->is_transl_needed() === false) return false;
$this->transl_class = new GoogleTranslate();
$DOM_body_text = $this->DOM_get_body_text( $this->html_DOM_code );
$this->whole_body_text_arr = $this->DOM_get_changed_content( $DOM_body_text, $this->array_get_trans( $this->array_modificate( $DOM_body_text ) ) );
$this->TranslateUnbodyContent();
return true;
}
public function GetTranslatedPage() { return $this->html_DOM_code->save(); }
}
?>