@@ -11,7 +11,7 @@ class TranslatorController extends Controller
11
11
private $ user = null ;
12
12
private $ helper ;
13
13
14
- private $ app_id = null ;
14
+ private $ app_id = null ;
15
15
private $ app_key = null ;
16
16
17
17
public function __construct ()
@@ -31,18 +31,19 @@ public function create($form_data, $page, $plugin_settings)
31
31
{
32
32
// set a default select box value to the form
33
33
// any better idea than change the $_GET?
34
- $ _GET ['translate_to ' ] = $ _COOKIE ['translate_to ' ] ?? $ this ->helper ->s ('template.frontend_language ' );
34
+ $ _GET ['translate_to ' ] = $ _COOKIE ['translate_to ' ] ?? $ this ->helper ->s ('template.frontend_language ' );
35
35
$ data ['translate_from ' ] = $ _COOKIE ['translate_from ' ] ?? 'en ' ;
36
36
37
37
$ _GET ['append_source_content ' ] = $ _COOKIE ['append_source_content ' ] ?? 'yes ' ;
38
38
39
39
$ _GET ['translate_result_add_to_field ' ] = $ _COOKIE ['translate_result_add_to_field ' ] ?? 'main_content ' ;
40
40
41
- if ($ plugin_settings [ ' api_provider ' ] == ' baidu ' ) {
42
- $ data ['translate_languages ' ] = [" en " => " English " , " zh " => " Chinese " , " epa " => " Spanish " , " ara " => " Arabic " , " jp " => " Japanese " , " hi " => " Hindi " , " pt " => " Portuguese " , " fra " => " French " , " ru " => " Russian " , " de " => " German " , " kor " => " Korean " , " it " => " Italian " , " th " => " Thai " ];
41
+ if (' baidu ' == strtolower ( $ plugin_settings [ ' api_provider ' ]) ) {
42
+ $ data ['translate_languages ' ] = [' en ' => ' English ' , ' zh ' => ' Chinese ' , ' epa ' => ' Spanish ' , ' ara ' => ' Arabic ' , ' jp ' => ' Japanese ' , ' hi ' => ' Hindi ' , ' pt ' => ' Portuguese ' , ' fra ' => ' French ' , ' ru ' => ' Russian ' , ' de ' => ' German ' , ' kor ' => ' Korean ' , ' it ' => ' Italian ' , ' th ' => ' Thai ' ];
43
43
} else {
44
- $ data ['translate_languages ' ] = [" en " => " English " , " zh " => " Chinese " , " es " => " Spanish " , " ar " => " Arabic " , " ja " => " Japanese " , " hi " => " Hindi " , " pt " => " Portuguese " , " fr " => " French " , " ru " => " Russian " , " de " => " German " , " ko " => " Korean " , " it " => " Italian " , " la " => " Latin " ];
44
+ $ data ['translate_languages ' ] = [' en ' => ' English ' , ' zh ' => ' Chinese ' , ' es ' => ' Spanish ' , ' ar ' => ' Arabic ' , ' ja ' => ' Japanese ' , ' hi ' => ' Hindi ' , ' pt ' => ' Portuguese ' , ' fr ' => ' French ' , ' ru ' => ' Russian ' , ' de ' => ' German ' , ' ko ' => ' Korean ' , ' it ' => ' Italian ' , ' la ' => ' Latin ' ];
45
45
}
46
+
46
47
return $ data ;
47
48
}
48
49
@@ -56,11 +57,12 @@ public function edit($id, $page, $plugin_settings)
56
57
57
58
$ page ->translate_result_add_to_field = $ _COOKIE ['translate_result_add_to_field ' ] ?? 'main_content ' ;
58
59
59
- if ($ plugin_settings [ ' api_provider ' ] == ' baidu ' ) {
60
- $ data ['translate_languages ' ] = [" en " => " English " , " zh " => " Chinese " , " epa " => " Spanish " , " ara " => " Arabic " , " jp " => " Japanese " , " hi " => " Hindi " , " pt " => " Portuguese " , " fra " => " French " , " ru " => " Russian " , " de " => " German " , " kor " => " Korean " , " it " => " Italian " , " th " => " Thai " ];
60
+ if (' baidu ' == strtolower ( $ plugin_settings [ ' api_provider ' ]) ) {
61
+ $ data ['translate_languages ' ] = [' en ' => ' English ' , ' zh ' => ' Chinese ' , ' epa ' => ' Spanish ' , ' ara ' => ' Arabic ' , ' jp ' => ' Japanese ' , ' hi ' => ' Hindi ' , ' pt ' => ' Portuguese ' , ' fra ' => ' French ' , ' ru ' => ' Russian ' , ' de ' => ' German ' , ' kor ' => ' Korean ' , ' it ' => ' Italian ' , ' th ' => ' Thai ' ];
61
62
} else {
62
- $ data ['translate_languages ' ] = [" en " => " English " , " zh " => " Chinese " , " es " => " Spanish " , " ar " => " Arabic " , " ja " => " Japanese " , " hi " => " Hindi " , " pt " => " Portuguese " , " fr " => " French " , " ru " => " Russian " , " de " => " German " , " ko " => " Korean " , " it " => " Italian " , " la " => " Latin " ];
63
+ $ data ['translate_languages ' ] = [' en ' => ' English ' , ' zh ' => ' Chinese ' , ' es ' => ' Spanish ' , ' ar ' => ' Arabic ' , ' ja ' => ' Japanese ' , ' hi ' => ' Hindi ' , ' pt ' => ' Portuguese ' , ' fr ' => ' French ' , ' ru ' => ' Russian ' , ' de ' => ' German ' , ' ko ' => ' Korean ' , ' it ' => ' Italian ' , ' la ' => ' Latin ' ];
63
64
}
65
+
64
66
return $ data ;
65
67
}
66
68
@@ -71,34 +73,60 @@ public function store($form_data, $page, $plugin_settings)
71
73
72
74
public function update ($ form_data , $ page , $ plugin_settings )
73
75
{
74
- if (trim ($ form_data ['translate_content ' ]) == '' ) {
76
+ if ('' == trim ($ form_data ['translate_content ' ])) {
75
77
return false ;
76
78
}
77
- if ($ plugin_settings [ ' api_provider ' ] == ' baidu ' ) {
78
- $ this ->app_id = $ plugin_settings ['app_id ' ];
79
+ if (' baidu ' == $ plugin_settings [ ' api_provider ' ] ) {
80
+ $ this ->app_id = $ plugin_settings ['app_id ' ];
79
81
$ this ->app_key = $ plugin_settings ['app_key ' ];
80
82
81
83
$ api_result = $ this ->baiduTranslate ($ form_data ['translate_content ' ], $ form_data ['translate_from ' ], $ form_data ['translate_to ' ]);
82
84
if (isset ($ api_result ['trans_result ' ][0 ]['dst ' ])) {
83
85
$ translate_result = '<div class="translate-content"> ' ;
84
86
foreach ($ api_result ['trans_result ' ] as $ rs ) {
85
- if ($ form_data [ ' append_source_content ' ] == ' yes ' ) {
86
- $ translate_result .= '<div class="src"> ' . $ rs ['src ' ] . '</div> ' ;
87
+ if (' yes ' == $ form_data [ ' append_source_content ' ] ) {
88
+ $ translate_result .= '<div class="src"> ' . $ rs ['src ' ]. '</div> ' ;
87
89
}
88
- $ translate_result .= '' . $ rs ['dst ' ] . '<br/><br/> ' ;
90
+ $ translate_result .= '' . $ rs ['dst ' ]. '<br/><br/> ' ;
89
91
}
90
92
$ translate_result .= '</div> ' ;
91
- $ new_content = $ page [$ form_data ['translate_result_add_to_field ' ]] . $ translate_result ;
93
+ $ new_content = $ page [$ form_data ['translate_result_add_to_field ' ]]. $ translate_result ;
92
94
} else {
93
95
if (request ()->debug ) {
94
96
$ this ->helper ->debug ($ api_result );
95
97
}
98
+
96
99
return false ;
97
100
}
98
- }
101
+ } elseif ('google_free ' == $ plugin_settings ['api_provider ' ]) {
102
+ if ('google_free_002 ' == $ plugin_settings ['app_key ' ]) {
103
+ // https://github.com/Stichoza/google-translate-php
104
+ // Need the end-user install via composer first
99
105
106
+ $ tr = new \Stichoza \GoogleTranslate \GoogleTranslate ($ form_data ['translate_to ' ], $ form_data ['translate_from ' ], ['verify ' => false ]);
107
+ $ api_result = $ tr ->translate ($ form_data ['translate_content ' ]);
108
+ } else {
109
+ // https://github.com/dejurin/php-google-translate-for-free
110
+ // Need copy the class code to the GoogleTranslateForFree.php
100
111
101
- // $this->helper->debug($api_result);
112
+ $ tr = new GoogleTranslateForFree ();
113
+ $ api_result = $ tr ->translate ($ form_data ['translate_from ' ], $ form_data ['translate_to ' ], $ form_data ['translate_content ' ], 2 ).$ plugin_settings ['app_key ' ];
114
+ }
115
+
116
+ if ($ api_result ) {
117
+ $ translate_result = '<div class="translate-content"> ' .nl2br ($ api_result ).'</div> ' ;
118
+
119
+ if ('yes ' == $ form_data ['append_source_content ' ]) {
120
+ $ translate_result .= '<div class="pt-3 source-content"><hr class="source-hr" /> ' .nl2br ($ form_data ['translate_content ' ]).'</div> ' ;
121
+ }
122
+
123
+ $ new_content = $ page [$ form_data ['translate_result_add_to_field ' ]].$ translate_result ;
124
+ }
125
+
126
+ $ this ->helper ->debug ([$ new_content , $ form_data ]);
127
+ }
128
+
129
+ $ this ->helper ->debug ($ api_result );
102
130
if (isset ($ new_content )) {
103
131
$ page ->update ([$ form_data ['translate_result_add_to_field ' ]=>$ new_content ]);
104
132
}
@@ -120,14 +148,13 @@ public function update($form_data, $page, $plugin_settings)
120
148
* Other methods.
121
149
*/
122
150
123
-
124
151
// baidu FanYi translate
125
152
public function baiduTranslate ($ query , $ from , $ to )
126
153
{
127
154
$ app_id = $ this ->app_id ;
128
155
$ app_key = $ this ->app_key ;
129
156
if (strlen ($ app_id ) < 10 || strlen ($ app_key ) < 10 ) {
130
- exit (" Please edit the app_id & app_key in the translator plugin setting page, you can get them from https://api.fanyi.baidu.com/ " );
157
+ exit (' Please edit the app_id & app_key in the translator plugin setting page, you can get them from https://api.fanyi.baidu.com/ ' );
131
158
}
132
159
133
160
$ args = [
@@ -153,8 +180,6 @@ public function buildSign($query, $appID, $salt, $secKey)
153
180
return $ ret ;
154
181
}
155
182
156
-
157
-
158
183
//发起网络请求
159
184
public function baiduCall ($ url , $ args =null , $ method ='post ' , $ testflag = 0 , $ timeout = 10 , $ headers =[])
160
185
{
@@ -174,8 +199,6 @@ public function baiduCall($url, $args=null, $method='post', $testflag = 0, $time
174
199
return $ ret ;
175
200
}
176
201
177
-
178
-
179
202
public function baiduCallOnce ($ url , $ args =null , $ method ='post ' , $ withCookie = false , $ timeout = 10 , $ headers =[])
180
203
{
181
204
$ ch = curl_init ();
@@ -208,8 +231,6 @@ public function baiduCallOnce($url, $args=null, $method='post', $withCookie = fa
208
231
return $ r ;
209
232
}
210
233
211
-
212
-
213
234
public function baiduConvert (&$ args )
214
235
{
215
236
$ data = '' ;
0 commit comments