Skip to content

Commit c77477d

Browse files
author
Paul Olthof
committed
Support for Google Translate. All empty translations can be processed through Google Translate.
1 parent 2bb1b4b commit c77477d

File tree

7 files changed

+74
-1
lines changed

7 files changed

+74
-1
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"extra": {
2626
"branch-alias": {
27-
"dev-master": "0.3.x-dev"
27+
"dev-master": "0.4.x-dev"
2828
}
2929
}
3030
}

resources/lang/en/manager.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
'help' => 'Select your preferred group and locale below and start translating.',
55
'locale_placeholder' => 'Enter the locale you wish to edit. (example: en)',
66
'button' => 'Load',
7+
'google' => 'Google Translate',
78
];

resources/lang/nl/manager.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
'help' => 'Selecteer de groep en de taal van uw voorkeur en start met vertalen.',
55
'locale_placeholder' => 'Geef de taal op die uw wilt bewerken. (bijvoorbeeld: nl)',
66
'button' => 'Inladen',
7+
'google' => 'Google Translate',
78
];

src/Controllers/TranslationsController.php

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Hpolthof\Translation\TranslationException;
44
use Illuminate\Http\Request;
55
use Illuminate\Routing\Controller;
6+
use Stichoza\GoogleTranslate\TranslateClient;
67

78
class TranslationsController extends Controller {
89

@@ -90,4 +91,10 @@ public function postStore(Request $request) {
9091
}
9192
return 'OK';
9293
}
94+
95+
public function postTranslate(Request $request) {
96+
$text = TranslateClient::translate($request->input('origin'), $request->input('target'), $request->input('text'));
97+
$key = $request->input('key');
98+
return compact('key', 'text');
99+
}
93100
}

src/ServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public function boot()
9090
'uses' => 'TranslationsController@postStore',
9191
'as' => 'translations.store',
9292
]);
93+
$router->post('/translate', [
94+
'uses' => 'TranslationsController@postTranslate',
95+
'as' => 'translations.translate',
96+
]);
9397
});
9498
}
9599

views/index.blade.php

+17
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,26 @@
5050
</div>
5151
</div>
5252

53+
<div class="row" ng-if="items.length > 0">
54+
<div class="col-md-offset-2 col-md-6">
55+
<div class="progress">
56+
<div class="progress-bar progress-bar-warning progress-bar-striped" style="width: [[ (translateResult.skip / translateResult.total) * 100 ]]%"></div>
57+
<div class="progress-bar progress-bar-success progress-bar-striped" style="width: [[ (translateResult.success / translateResult.total) * 100 ]]%"></div>
58+
<div class="progress-bar progress-bar-danger progress-bar-striped" style="width: [[ (translateResult.errors / translateResult.total) * 100 ]]%"></div>
59+
<div class="progress-bar progress-bar-info progress-bar-striped" style="width: [[ (translateResult.loading / translateResult.total) * 100 ]]%"></div>
60+
</div>
61+
</div>
62+
<div class="col-md-2">
63+
<button class="btn btn-info form-control" ng-click="translateAll()">
64+
{{ trans('translation::manager.google') }}
65+
</button>
66+
</div>
67+
</div>
68+
5369
<div class="row datarow" ng-repeat="item in items">
5470
<div class="col-md-3 text">
5571
[[ item.name ]]
72+
<span ng-if="item.check == true" class="label label-warning">Unsaved!</span>
5673
</div>
5774
<div class="col-md-4 text">
5875
[[ item.value ]]

views/javascript.blade.php

+43
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,48 @@
1919
$scope.items = [];
2020
}
2121
22+
$scope.translateResult = {'total': 0, 'loading': 0, 'errors': 0, 'skip': 0, 'success': 0};
23+
$scope.translateAll = function() {
24+
$scope.translateResult = {'total': 0, 'loading': 0, 'errors': 0, 'skip': 0, 'success': 0};
25+
$scope.translateResult.total = $scope.items.length;
26+
var requests = 0;
27+
28+
for(key in $scope.items) {
29+
$scope.translateResult.loading++;
30+
if($scope.items[key].translation === null || $scope.items[key].translation === '') {
31+
requests++;
32+
setTimeout(function(key){
33+
$scope.translate($scope.items[key].value, $scope.items[key].name, function(data) {
34+
$scope.translateResult.loading--;
35+
$scope.translateResult.success++;
36+
for(i in $scope.items) {
37+
if($scope.items[i].name === data.key) {
38+
$scope.items[i].translation = data.text;
39+
$scope.items[i].check = true;
40+
}
41+
}
42+
}, function() {
43+
$scope.translateResult.loading--;
44+
$scope.translateResult.errors++;
45+
});
46+
}, 500*requests, key);
47+
} else {
48+
$scope.translateResult.loading--;
49+
$scope.translateResult.skip++;
50+
}
51+
}
52+
};
53+
54+
$scope.translate = function(text, key, success, error) {
55+
$http.post("{{ URL::route('translations.translate') }}", {
56+
'key': key,
57+
'origin': $scope.currentLocale,
58+
'target': $scope.currentEditable,
59+
'text': text
60+
}).success(success)
61+
.error(error);
62+
};
63+
2264
$scope.fetch = function() {
2365
$http.post("{{ URL::route('translations.items') }}", {
2466
'group': $scope.currentGroup,
@@ -42,6 +84,7 @@
4284
.error(function(data, status, headers, config) {
4385
$scope.setMessage(status, 'danger');
4486
});
87+
$scope.items[$index].check = false;
4588
}
4689
4790
$scope.locales = [];

0 commit comments

Comments
 (0)