-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.diff
135 lines (133 loc) · 4.87 KB
/
translate.diff
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
diff -r ec1ef8f1722d ubiquity/standard-feeds/general.js
--- a/ubiquity/standard-feeds/general.js Thu Aug 27 00:26:22 2009 +0900
+++ b/ubiquity/standard-feeds/general.js Thu Aug 27 00:26:01 2009 +0530
@@ -269,8 +269,34 @@
pblock ? CmdUtils.previewAjax(pblock, options) : jQuery.ajax(options);
}
+function googleLanguageDetect(text, callback, pblock) {
+ var self = this;
+ var options = {
+ type: "GET",
+ url: "http://ajax.googleapis.com/ajax/services/language/detect",
+ data: {v: "1.0", q: text, },
+ dataType: "json",
+ success: function onDetectionSuccess(data) {
+ switch (data.responseStatus) {
+ case 200: callback(data.responseData.language); return;
+ case 400: {
+ displayMessage(data.responseDetails, self); return;
+ }
+ default: displayMessage(data.responseDetails, self);
+ }
+ },
+ error: function onDetectionError(xhr) {
+ Utils.reportInfo("google language detection: " +
+ xhr.status + " " + xhr.statusText);
+ },
+ };
+ pblock ? CmdUtils.previewAjax(pblock, options) : jQuery.ajax(options);
+}
+
CmdUtils.CreateCommand({
- DEFAULT_LANG_PREF: "extensions.ubiquity.default_translation_lang",
+ DEFAULT_LANG_PREF : "extensions.ubiquity.default_translation_lang",
+ ALT_LANG_PREF : "extensions.ubiquity.alt_translation_lang",
+
names: ["translate"],
arguments: [
{role: "object", nountype: noun_arb_text, label: "text"},
@@ -291,7 +317,28 @@
execute: function translate_execute({object: {html}, goal, source}) {
var sl = source.data || "";
var tl = goal.data || this._getDefaultLang();
- if (html && html.length <= GTRANSLATE_LIMIT)
+ if (html && html.length <= GTRANSLATE_LIMIT) {
+ var altLang = this._getAlternativeLang();
+ if (altLang) {
+ googleLanguageDetect.call(
+ this,
+ html,
+ function translate_execute_onDetection(sourceLangCode) {
+ if (sourceLangCode == tl) {
+ tl = altLang;
+ }
+ googleTranslate.call(
+ this,
+ html,
+ {from: sl, to: tl},
+ function translate_execute_onTranslate({translatedText}) {
+ CmdUtils.setSelection(translatedText);
+ }
+ );
+ }
+ );
+ return;
+ }
googleTranslate.call(
this,
html,
@@ -299,6 +346,7 @@
function translate_execute_onTranslate({translatedText}) {
CmdUtils.setSelection(translatedText);
});
+ }
else
Utils.openUrlInBrowser(
"http://translate.google.com/translate" +
@@ -329,6 +377,40 @@
var phtml = pblock.innerHTML = _(
"Replaces the selected text with the <b>${toLang}</b> translation:",
{toLang: toLang});
+
+ pblock.innerHTML = html;
+ var altLang = this._getAlternativeLang();
+ if (altLang) {
+ googleLanguageDetect.call(
+ this,
+ html,
+ function translate_preview_onDetection(sourceLangCode) {
+ if (sourceLangCode == toLangCode) {
+ toLangCode = altLang;
+ toLang = noun_type_lang_google.getLangName(toLangCode);
+ phtml = _("Replaces the selected text with the <b>${toLang}</b> translation:",
+ {toLang: toLang});
+ }
+ googleTranslate.call(
+ this,
+ html,
+ {from: fromLangCode, to: toLangCode},
+ function translate_preview_onTranslate(
+ {translatedText, detectedSourceLanguage: dsl})
+ {
+ pblock.innerHTML = (phtml + "<br/><br/>" + translatedText +
+ (dsl ? ("<p>(" + dsl + " \u2192 " + toLangCode + ")</p>")
+ : ""
+ )
+ );
+ },
+ pblock
+ );
+ },
+ pblock
+ );
+ return;
+ }
googleTranslate.call(
this,
html,
@@ -355,6 +437,16 @@
return (noun_type_lang_google.getLangName(defaultLang)
? defaultLang
: "en");
+ },
+
+ // Returns the alternative language for translation. This alternative language is used
+ // as target language while inputed text is in default language.
+ _getAlternativeLang: function translate__getAlternativeLang() {
+ var {prefs} = Application;
+ var alterLang = prefs.getValue(this.ALT_LANG_PREF, "");
+ return (noun_type_lang_google.getLangName(alterLang)
+ ? alterLang
+ : "");
}
});