forked from toolstack/gp-machine-translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gp-machine-translate.js
56 lines (51 loc) · 1.62 KB
/
gp-machine-translate.js
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
$gp.machine_translate = function( $ ) { return {
current: null,
init: function( table ) {
$gp.init();
$gp.machine_translate.table = table;
$gp.machine_translate.install_hooks();
},
install_hooks: function() {
$( $gp.machine_translate.table ).on( 'click', 'a.gp_machine_translate', $gp.machine_translate.hooks.machine_translate )
},
machine_translate: function( link ) {
original_text = link.parents( '.textareas' ).siblings( '.original' ).text();
if( !original_text ) {
original_text = link.parents( '.textareas' ).siblings( 'p:last' ).children( '.original' ).html();
}
if( !original_text ) {
return;
}
$gp.notices.notice( 'Translating via Machine Translate…' );
var data = {
'action': 'gp_machine_translate',
'query': '',
'locale': gp_machine_translate.locale,
'original': original_text,
};
jQuery.ajax( {
url: gp_machine_translate.ajaxurl,
type: 'post',
data: data,
datatype: 'json',
})
.always( function( result ) {
if( ! result.error && result.data.translatedText != '' ) {
link.parent( 'div' ).parent( 'div' ).children( 'textarea' ).html( result.data.translatedText[0] ).focus();
$gp.notices.success( 'Translated!' );
} else {
$gp.notices.error( 'Error in translating via Machine Translate: ' + result.error.message + ': ' + result.error.reason );
link.parent( 'p' ).siblings( 'textarea' ).focus();
}
});
},
hooks: {
machine_translate: function() {
$gp.machine_translate.machine_translate( $( this ) );
return false;
}
}
}}(jQuery);
jQuery( function( $ ) {
$gp.machine_translate.init( $( '#translations' ) );
});