Skip to content

Commit 8405f4f

Browse files
committed
Handle google translate max character limit 5000
1 parent 09262d0 commit 8405f4f

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Translate content online by Baidu AI or Google AI
22

33
- This is an Amila Laravel CMS Plugin
4-
- Automatically translate content online via Baidu FanYi API or Google Translate API
4+
- Automatically translate content online via Baidu FanYi API or Google Translate API or Google Translate Free Version
55

66
## Install it via the backend
77

@@ -66,7 +66,7 @@ php artisan laravelcms --action=clear
6666

6767
```json
6868
{
69-
"api_provider": "google",
69+
"api_provider": "google", // NOT SUPPORT YET
7070
"app_id": "AizSxUKysHBsds2", // Translation API KEY
7171
"app_key": "AizSxUKysHBsds2" // Translation API KEY
7272
}
@@ -95,6 +95,8 @@ php artisan laravelcms --action=clear
9595
}
9696
```
9797

98+
- Limitations of free Google translate: (1) Google only allows a maximum of 5000 characters to be translated at once. If you want to translate a longer text, you can split it to shorter parts, and translate them one-by-one. (2) 503,429,403 error it is most likely that Google has banned your external IP address and/or requires you to solve a CAPTCHA. This is not a bug in this package. Google has become stricter, and it seems like they keep lowering the number of allowed requests per IP per a certain amount of time. You may need to wait 12-36 hours. [More details](https://github.com/Stichoza/google-translate-php#known-limitations)
99+
98100
## Improve this plugin & documents
99101

100102
- You are very welcome to improve this plugin and how to use documents

src/Controllers/TranslatorController.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,25 @@ public function update($form_data, $page, $plugin_settings)
102102
if ('google_free_002' == $plugin_settings['app_key']) {
103103
// https://github.com/Stichoza/google-translate-php
104104
// Need the end-user install via composer first
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']);
105+
try {
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+
} catch (\Exception $e) {
109+
exit($e->getMessage());
110+
}
108111
} else {
109112
// https://github.com/dejurin/php-google-translate-for-free
110113
// Need copy the class code to the GoogleTranslateForFree.php
111-
112-
$tr = new GoogleTranslateForFree();
113-
$api_result = $tr->translate($form_data['translate_from'], $form_data['translate_to'], $form_data['translate_content'], 2);
114+
try {
115+
$tr = new GoogleTranslateForFree();
116+
// google_free translate max character limit 5000
117+
if (strlen(urlencode($form_data['translate_content'])) >= 5000) {
118+
$form_data['translate_content'] = urldecode(substr(urlencode($form_data['translate_content']), 0, 4999));
119+
}
120+
$api_result = $tr->translate($form_data['translate_from'], $form_data['translate_to'], $form_data['translate_content'], 2).$plugin_settings['app_key'];
121+
} catch (\Exception $e) {
122+
exit($e->getMessage());
123+
}
114124
}
115125

116126
if ($api_result) {

src/database/migrations/2020_01_19_114400_update_plugin_settings_table.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public function up()
2727
'sort_value' => 20,
2828
'abstract' => 'Automatically translate content via Google AI or Baidu AI. <a href="https://github.com/AlexStack/Laravel-CMS-Plugin-Translator#where-can-i-get-the-app_id--app_key" target="_blank"><i class="fas fa-link mr-1"></i>Tutorial</a>',
2929
'param_value' => '{
30+
"api_provider": "Google_Free",
31+
"app_id": "google_free_001",
32+
"app_key": "google_free_001",
3033
"plugin_name": "Content Translator",
3134
"blade_file": "translator",
3235
"tab_name": "<i class=\'fas fa-language mr-1\'></i>__(translator)",
33-
"php_class": "App\\\\LaravelCms\\\\Plugins\\\\Translator\\\\Controllers\\\\TranslatorController",
34-
"api_provider": "Google_Free",
35-
"app_id": "google_free_001",
36-
"app_key": "google_free_001"
36+
"php_class": "App\\\\LaravelCms\\\\Plugins\\\\Translator\\\\Controllers\\\\TranslatorController"
3737
}',
3838
];
3939
LaravelCmsSetting::UpdateOrCreate(

0 commit comments

Comments
 (0)