This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
/
jetlagged.js
77 lines (75 loc) · 3.01 KB
/
jetlagged.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Jet Lagged is a jetpack extension allowing to contextually translate some selected text within any
* html page using the google translate API.
*
* Once installed, just select some text, right-click and translate it choosing the target language
* using the "Translate" contextual menu.
*
* If not text is selected, the whole page will be translated.
*
* Please note that in both cases, the original language will be guessed by Google, so you might
* obtain unexpected results, especially with small portions of text or ones containing lot of unusual
* terms.
*
* @author Nicolas Perriault <nperriault@gmail.com>
*/
jetpack.future.import("menu");
jetpack.future.import("selection");
jetpack.menu.context.page.add(function(target)({
label: "Translate to...",
icon: "http://translate.google.com/favicon.ico",
menu: new jetpack.Menu(langs),
command: function(menuItem){
if (!jetpack.selection.text || '' == jetpack.selection.text) {
return target.document.location = 'http://translate.google.com/translate?js=y&hl=fr&ie=UTF-8&u='
+ escape(target.document.location) + '&sl=&tl=' + menuItem.data;
}
var url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='
+ escape(jetpack.selection.text) + '&langpair=|' + menuItem.data;
$.getJSON(url, function(result){
if (result.responseStatus != 200) {
return jetpack.notifications.show('Error while trying to reach the google translation API ('
+ result.responseStatus + ')');
}
return jetpack.selection.html = '<span style="background:yellow;color:black">'
+ result.responseData.translatedText
+ '</span>';
});
}
}));
var langs = [
{ label: 'Arabic', data: 'ar' },
{ label: 'Armenian', data: 'hy' },
{ label: 'Bengali', data: 'bn' },
{ label: 'Bulgarian', data: 'bg' },
{ label: 'Chinese', data: 'zh' },
{ label: 'Danish', data: 'da' },
{ label: 'Dutch', data: 'nl'},
{ label: 'English', data: 'en' },
{ label: 'Filipino', data: 'tl' },
{ label: 'Finnish', data: 'fi' },
{ label: 'French', data: 'fr' },
{ label: 'German', data: 'de' },
{ label: 'Greek', data: 'el' },
{ label: 'Hebrew', data: 'iw' },
{ label: 'Hindi', data: 'hi' },
{ label: 'Hungarian', data: 'hu' },
{ label: 'Irish', data: 'ga' },
{ label: 'Italian', data: 'it' },
{ label: 'Japanese', data: 'ja' },
{ label: 'Norwegian', data: 'no' },
{ label: 'Polish', data: 'pl' },
{ label: 'Portuguese', data: 'pt-pt' },
{ label: 'Romanian', data: 'ro' },
{ label: 'Russian', data: 'ru' },
{ label: 'Serbian', data: 'sr' },
{ label: 'Slovak', data: 'sk' },
{ label: 'Slovenian', data: 'sl' },
{ label: 'Spanish', data: 'es' },
{ label: 'Swedish', data: 'sv' },
{ label: 'Tamil', data: 'ta' },
{ label: 'Thai', data: 'th' },
{ label: 'Turkish', data: 'tr' },
{ label: 'Ukrainian', data: 'uk' },
{ label: 'Vietnamese', data: 'vi' },
];