Skip to content

Commit 3ebab0e

Browse files
committed
Translate cms language files
1 parent 486f75c commit 3ebab0e

File tree

1 file changed

+185
-12
lines changed

1 file changed

+185
-12
lines changed

src/Controllers/TranslatorController.php

Lines changed: 185 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ class TranslatorController extends Controller
1111
private $user = null;
1212
private $helper;
1313

14-
private $app_id = null;
15-
private $app_key = null;
14+
private $app_id = null;
15+
private $app_key = null;
16+
private $google_lang_ary = ['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'];
17+
private $baidu_lang_ary = ['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'];
1618

1719
public function __construct()
1820
{
@@ -29,19 +31,20 @@ public function checkUser()
2931

3032
public function create($form_data, $page, $plugin_settings)
3133
{
32-
// set a default select box value to the form
33-
// any better idea than change the $_GET?
34+
// set a default select box value to the form without change the blade includes
35+
// any better idea than change the $_GET? request()->merge() not working
36+
3437
$_GET['translate_to'] = $_COOKIE['translate_to'] ?? $this->helper->s('template.frontend_language');
35-
$data['translate_from'] = $_COOKIE['translate_from'] ?? 'en';
38+
$_GET['translate_from'] = $_COOKIE['translate_from'] ?? 'en';
3639

3740
$_GET['append_source_content'] = $_COOKIE['append_source_content'] ?? 'yes';
3841

3942
$_GET['translate_result_add_to_field'] = $_COOKIE['translate_result_add_to_field'] ?? 'main_content';
4043

4144
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'];
45+
$data['translate_languages'] = $this->baidu_lang_ary;
4346
} 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'];
47+
$data['translate_languages'] = $this->google_lang_ary;
4548
}
4649

4750
return $data;
@@ -58,9 +61,13 @@ public function edit($id, $page, $plugin_settings)
5861
$page->translate_result_add_to_field = $_COOKIE['translate_result_add_to_field'] ?? 'main_content';
5962

6063
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'];
64+
$data['translate_languages'] = $this->baidu_lang_ary;
6265
} else {
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'];
66+
$data['translate_languages'] = $this->google_lang_ary;
67+
}
68+
69+
if ('translateCmsLangFile' == request()->action) {
70+
$this->translateCmsLangFile();
6471
}
6572

6673
return $data;
@@ -76,7 +83,7 @@ public function update($form_data, $page, $plugin_settings)
7683
if ('' == trim($form_data['translate_content'])) {
7784
return false;
7885
}
79-
if ('baidu' == strtolower($plugin_settings['api_provider'])) {
86+
if ('baidu' == $plugin_settings['api_provider']) {
8087
$this->app_id = $plugin_settings['app_id'];
8188
$this->app_key = $plugin_settings['app_key'];
8289

@@ -98,14 +105,15 @@ public function update($form_data, $page, $plugin_settings)
98105

99106
return false;
100107
}
101-
} elseif ('google_free' == strtolower($plugin_settings['api_provider'])) {
108+
} elseif ('google_free' == $plugin_settings['api_provider']) {
102109
if ('google_free_002' == $plugin_settings['app_key']) {
103110
// https://github.com/Stichoza/google-translate-php
104111
// Need the end-user install via composer first
105112
try {
106113
$tr = new \Stichoza\GoogleTranslate\GoogleTranslate($form_data['translate_to'], $form_data['translate_from'], ['verify' => false]);
107114
$api_result = $tr->translate($form_data['translate_content']);
108115
} catch (\Exception $e) {
116+
echo $plugin_settings['app_key'].'<hr>';
109117
exit($e->getMessage());
110118
}
111119
} else {
@@ -119,6 +127,7 @@ public function update($form_data, $page, $plugin_settings)
119127
}
120128
$api_result = $tr->translate($form_data['translate_from'], $form_data['translate_to'], $form_data['translate_content'], 2);
121129
} catch (\Exception $e) {
130+
echo $plugin_settings['app_key'].'<hr>';
122131
exit($e->getMessage());
123132
}
124133
}
@@ -133,7 +142,7 @@ public function update($form_data, $page, $plugin_settings)
133142
$new_content = $page[$form_data['translate_result_add_to_field']].$translate_result;
134143
}
135144

136-
// $this->helper->debug([$new_content, $form_data]);
145+
// $this->helper->debug([$new_content,$form_data]);
137146
}
138147

139148
// $this->helper->debug($api_result);
@@ -158,6 +167,170 @@ public function update($form_data, $page, $plugin_settings)
158167
* Other methods.
159168
*/
160169

170+
// translate cms.php array one by one via baidu ai
171+
public function translateCmsLangFile()
172+
{
173+
$this->app_id = $this->helper->s('plugin.page-tab-translator.app_id');
174+
$this->app_key = $this->helper->s('plugin.page-tab-translator.app_key');
175+
$translate_from = request()->translate_from ?? 'en';
176+
$translate_to = request()->translate_to ?? 'es';
177+
$file = request()->file ?? 'cms.php';
178+
if (! isset($this->baidu_lang_ary[$translate_to])) {
179+
exit('wrong $translate_to '.$translate_to);
180+
}
181+
if ('zh' == $translate_to || 'en' == $translate_to) {
182+
exit('Not allowed $translate_to '.$translate_to);
183+
}
184+
$source_file = base_path('resources/lang/vendor/laravel-cms/'.$translate_from.'/'.$file);
185+
$target_file = base_path('resources/lang/vendor/laravel-cms/'.$translate_to.'/'.$file);
186+
if (! file_exists($source_file)) {
187+
exit($source_file.' not exists');
188+
}
189+
$source_lang_ary = include $source_file;
190+
$except_ary = [':Name', ':name', ':latest', ':current', ':number', '$helper->s(', ':BROKER'];
191+
// $except_ary = [':Name',':name',':latest',':current',':number','$helper->s('];
192+
193+
$variable_prefix = '567.8.';
194+
195+
$search_ary = array_values($except_ary);
196+
$replace_ary = array_map(function ($v) use ($variable_prefix) {
197+
return $variable_prefix.$v.'';
198+
}, array_keys($except_ary));
199+
200+
$i = 0;
201+
$new_lang_ary = [];
202+
foreach ($source_lang_ary as $key=> $source_lang_str) {
203+
if (! strpos($source_lang_str, ':')) {
204+
// continue;
205+
}
206+
207+
$source_lang_str = str_replace($search_ary, $replace_ary, $source_lang_str);
208+
209+
$api_result = $this->baiduTranslate($source_lang_str, $translate_from, $translate_to);
210+
211+
if (isset($api_result['trans_result'][0]['dst'])) {
212+
$translate_result = '';
213+
foreach ($api_result['trans_result'] as $rs) {
214+
$translate_result .= ''.trim($rs['dst'])."\n";
215+
}
216+
217+
$new_lang_ary[$key] = trim($translate_result);
218+
219+
if ('page_number22' == $key) {
220+
$this->helper->debug($api_result);
221+
}
222+
}
223+
224+
if ($i > 3) {
225+
// break;
226+
// $this->helper->debug($new_lang_ary);
227+
}
228+
229+
++$i;
230+
}
231+
232+
$new_lang_str = "<?php \n# This language file was automatically translated by Google AI, please edit it manually if you feel it not accurate \n\n return ".var_export($new_lang_ary, true)."; \n";
233+
234+
$search_ary = $replace_ary;
235+
$replace_ary = array_values($except_ary);
236+
$new_lang_str = str_replace($search_ary, $replace_ary, $new_lang_str);
237+
238+
// $this->helper->debug([$search_ary, $replace_ary,$new_lang_str]);
239+
240+
if (! file_exists(dirname($target_file))) {
241+
mkdir(dirname($target_file), 0755);
242+
}
243+
244+
file_put_contents($target_file, $new_lang_str);
245+
echo '<hr>Generated language file: '.$target_file;
246+
exit();
247+
}
248+
249+
public function translateCmsLangFile_google()
250+
{
251+
$this->app_id = $this->helper->s('plugin.page-tab-translator.app_id');
252+
$this->app_key = $this->helper->s('plugin.page-tab-translator.app_key');
253+
$break_str = '#';
254+
$break_str_2 = '000 ';
255+
// $google_lang_ary = [ 'ar'=>'Arabic', 'hi'=>'Hindi', 'pt'=>'Portuguese', 'de'=>'German', 'ko'=>'Korean', 'it'=>'Italian', 'la'=>'Latin'];
256+
257+
$translate_from = request()->translate_from ?? 'en';
258+
$translate_to = request()->translate_to ?? 'es';
259+
$file = request()->file ?? 'cms.php';
260+
if (! isset($this->google_lang_ary[$translate_to])) {
261+
exit('wrong $translate_to '.$translate_to);
262+
}
263+
if ('zh' == $translate_to || 'en' == $translate_to) {
264+
exit('Not allowed $translate_to '.$translate_to);
265+
}
266+
267+
$source_file = base_path('resources/lang/vendor/laravel-cms/'.$translate_from.'/'.$file);
268+
$target_file = base_path('resources/lang/vendor/laravel-cms/'.$translate_to.'/'.$file);
269+
if (! file_exists($source_file)) {
270+
exit($source_file.' not exists');
271+
}
272+
$source_lang_ary = include $source_file;
273+
$except_ary = [':Name', ':name', ':latest', ':current', ':number', '$helper->s(', ':BROKER'];
274+
// $except_ary = [':Name',':name',':latest',':current',':number','$helper->s('];
275+
276+
$variable_prefix = '2020778899';
277+
278+
$source_lang_str = implode($break_str, array_values($source_lang_ary));
279+
280+
$search_ary = array_values($except_ary);
281+
$replace_ary = array_map(function ($v) use ($variable_prefix) {
282+
return $variable_prefix.$v.'';
283+
}, array_keys($except_ary));
284+
$source_lang_str = str_replace($search_ary, $replace_ary, $source_lang_str);
285+
286+
$api_result = $this->baiduTranslate($source_lang_str, $translate_from, $translate_to);
287+
288+
try {
289+
$tr = new GoogleTranslateForFree();
290+
$api_result = $tr->translate($translate_from, $translate_to, $source_lang_str, 2);
291+
} catch (\Exception $e) {
292+
exit($e->getMessage());
293+
}
294+
295+
// $api_result = str_replace($break_str_2, $break_str, $api_result);
296+
$translated_ary = explode($break_str, $api_result);
297+
if (count($translated_ary) != count($source_lang_ary)) {
298+
echo 'count($translated_ary) '.count($translated_ary).' not match count($source_lang_ary) '.count($source_lang_ary);
299+
$i = 0;
300+
foreach ($source_lang_ary as $key=>$val) {
301+
echo $key.' => '.$val.' === '.($translated_ary[$i] ?? 'NONE').'<br>'."\n";
302+
++$i;
303+
}
304+
305+
$this->helper->debug([$translated_ary, $source_lang_ary, $api_result]);
306+
}
307+
$search_ary = $replace_ary;
308+
$replace_ary = array_values($except_ary);
309+
$new_api_result = str_replace($search_ary, $replace_ary, $api_result);
310+
$new_api_result = str_replace(':BROKER', '', $new_api_result);
311+
312+
$translated_ary = explode($break_str, $new_api_result);
313+
$i =0;
314+
$new_lang_ary = [];
315+
foreach ($source_lang_ary as $key=>$val) {
316+
echo $key.' => '.$val.' === '.$translated_ary[$i].'<br>'."\n";
317+
$new_lang_ary[$key] = trim($translated_ary[$i]);
318+
++$i;
319+
}
320+
321+
// $this->helper->debug([$search_ary, $replace_ary, $new_lang_ary]);
322+
323+
$new_lang_str = "<?php \n# This language file was automatically translated by Google AI, please edit it manually if you feel it not accurate \n\n return ".var_export($new_lang_ary, true)."; \n";
324+
325+
if (! file_exists(dirname($target_file))) {
326+
mkdir(dirname($target_file), 0755);
327+
}
328+
329+
file_put_contents($target_file, $new_lang_str);
330+
echo '<hr>Generated language file: '.$target_file;
331+
exit();
332+
}
333+
161334
// baidu FanYi translate
162335
public function baiduTranslate($query, $from, $to)
163336
{

0 commit comments

Comments
 (0)