forked from duckduckgo/zeroclickinfo-spice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'topic/translate' of https://github.com/ghedo/zeroclicki…
…nfo-spice into translate
- Loading branch information
Showing
9 changed files
with
827 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package DDG::Spice::Translate::Basic; | ||
|
||
use DDG::Spice; | ||
|
||
attribution github => ['https://github.com/ghedo', 'ghedo' ], | ||
web => ['http://ghedini.me', 'Alessandro Ghedini']; | ||
|
||
my $dicts = 'arabic|ar|chinese|zh|czech|cz|english|en|french|fr|greek|gr|italian|it|japanese|ja|korean|ko|polish|pl|portuguese|pt|romanian|ro|spanish|es|turkish|tr'; | ||
|
||
spice to => 'http://example.com'; | ||
spice from => '(.+)\/(.+)'; | ||
spice wrap_string_callback => 1; | ||
|
||
triggers query_lc => qr/^translate (.+) from ($dicts) to ($dicts)$/; | ||
|
||
handle matches => sub { | ||
my ($words, $from, $to) = @_; | ||
|
||
$from = shorten_lang($from); | ||
$to = shorten_lang($to); | ||
|
||
my $dict = $from.$to; | ||
|
||
return ($dict, $words) | ||
}; | ||
|
||
sub shorten_lang { | ||
my ($lang) = @_; | ||
|
||
my $langs = { | ||
'arabic' => 'ar', | ||
'chinese' => 'zh', | ||
'czech' => 'cz', | ||
'english' => 'en', | ||
'french' => 'fr', | ||
'greek' => 'gr', | ||
'italian' => 'it', | ||
'japanese' => 'ja', | ||
'korean' => 'ko', | ||
'polish' => 'pl', | ||
'portuguese' => 'pt', | ||
'romanian' => 'ro', | ||
'spanish' => 'es', | ||
'turkish' => 'tr' | ||
}; | ||
|
||
return $langs -> {$lang} ? $langs -> {$lang} : $lang; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package DDG::Spice::Translate::Detect; | ||
|
||
use DDG::Spice; | ||
|
||
attribution github => ['https://github.com/ghedo', 'ghedo' ], | ||
web => ['http://ghedini.me', 'Alessandro Ghedini']; | ||
|
||
my $dicts = 'arabic|ar|chinese|zh|czech|cz|english|en|french|fr|greek|gr|italian|it|japanese|ja|korean|ko|polish|pl|portuguese|pt|romanian|ro|spanish|es|turkish|tr'; | ||
|
||
spice to => 'http://ws.detectlanguage.com/0.2/detect?q=$1&key={{ENV{DDG_SPICE_DETECTLANGUAGE_APIKEY}}}'; | ||
spice from => '(.+)\/(.+)'; | ||
spice wrap_jsonp_callback => 1; | ||
|
||
triggers query_lc => qr/^translate (.+) to ($dicts)$/; | ||
|
||
handle matches => sub { | ||
my ($word, $to) = @_; | ||
|
||
return ($word, shorten_lang($to)) | ||
}; | ||
|
||
sub shorten_lang { | ||
my ($lang) = @_; | ||
|
||
my $langs = { | ||
'arabic' => 'ar', | ||
'chinese' => 'zh', | ||
'czech' => 'cz', | ||
'english' => 'en', | ||
'french' => 'fr', | ||
'greek' => 'gr', | ||
'italian' => 'it', | ||
'japanese' => 'ja', | ||
'korean' => 'ko', | ||
'polish' => 'pl', | ||
'portuguese' => 'pt', | ||
'romanian' => 'ro', | ||
'spanish' => 'es', | ||
'turkish' => 'tr' | ||
}; | ||
|
||
return $langs -> {$lang} ? $langs -> {$lang} : $lang; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package DDG::Spice::Translate::MyMemory; | ||
|
||
use DDG::Spice; | ||
|
||
attribution github => ['https://github.com/ghedo', 'ghedo' ], | ||
web => ['http://ghedini.me', 'Alessandro Ghedini']; | ||
|
||
spice to => '"http://mymemory.translated.net/api/get?q=$3&langpair=$1|$2"'; | ||
spice from => '(.+)\/(.+)\/(.+)'; | ||
spice wrap_jsonp_callback => 1; | ||
|
||
triggers start => '///***never trigger***///'; | ||
|
||
handle remainder => sub { return }; | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package DDG::Spice::Translate::Wordreference; | ||
|
||
use DDG::Spice; | ||
|
||
attribution github => ['https://github.com/ghedo', 'ghedo' ], | ||
web => ['http://ghedini.me', 'Alessandro Ghedini']; | ||
|
||
spice to => 'http://api.wordreference.com/0.8/{{ENV{DDG_SPICE_WORDREFERENCE_APIKEY}}}/json/$1/$2?callback={{callback}}'; | ||
spice from => '(.+)\/(.+)'; | ||
|
||
triggers start => '///***never trigger***///'; | ||
|
||
handle remainder => sub { return }; | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
var langs = { | ||
'ar': 'Arabic', | ||
'zh': 'Chinese', | ||
'cz': 'Czech', | ||
'en': 'English', | ||
'fr': 'French', | ||
'gr': 'Greek', | ||
'it': 'Italian', | ||
'ja': 'Japanese', | ||
'ko': 'Korean', | ||
'pl': 'Polish', | ||
'pt': 'Portuguese', | ||
'ro': 'Romanian', | ||
'es': 'Spanish', | ||
'tr': 'Turkish' | ||
}; | ||
|
||
var translations = []; | ||
|
||
function ddg_spice_translate_basic(ir) { | ||
params = get_params(); | ||
dict = params[0]; | ||
words = params[1]; | ||
|
||
from = dict.slice(0, 2); | ||
to = dict.slice(-2); | ||
|
||
script = ''; | ||
callbk = undefined; | ||
|
||
if (words.split('%20').length > 1) { | ||
base = '/js/spice/translate/my_memory/'; | ||
|
||
nrj(base + from + '/' + to + '/' + words); | ||
} else { | ||
base = '/js/spice/translate/wordreference/'; | ||
|
||
nrj(base + dict + '/' + words); | ||
} | ||
} | ||
|
||
function get_params() { | ||
scripts = document.getElementsByTagName('script'); | ||
|
||
for (i = 0; i < scripts.length; i++) { | ||
regex = /translate\/([a-z]+)\/(.+)\/(.+)/; | ||
match = scripts[i].src.match(regex); | ||
|
||
if (match != undefined) { | ||
return [match[2], match[3]]; | ||
} | ||
} | ||
|
||
return ['', '']; | ||
} | ||
|
||
/* MyMemory */ | ||
function ddg_spice_translate_my_memory(ir) { | ||
items = new Array(); | ||
|
||
params = get_params_my_memory(); | ||
dict = params[0]; | ||
word = unescape(params[1]); | ||
from = dict.slice(0, 2); | ||
to = dict.slice(-2); | ||
|
||
if ((word == '') || (dict == '')) | ||
return; | ||
|
||
items[0] = new Array(); | ||
items[0]["h"] = langs[to] + ' translations for <i>' + word + '</i>'; | ||
items[0]['s'] = 'MyMemory'; | ||
items[0]['u'] = 'http://mymemory.translated.net/s.php?q=' | ||
+ word + '&sl=' + from + '&tl=' + to; | ||
items[0]["force_big_header"] = true; | ||
|
||
text = '<ul>'; | ||
|
||
text += format_translations_my_memory(ir.matches); | ||
|
||
text += '</ul>'; | ||
|
||
items[0]['a'] = text; | ||
|
||
nra(items); | ||
} | ||
|
||
function get_params_my_memory() { | ||
scripts = document.getElementsByTagName('script'); | ||
|
||
for (i = 0; i < scripts.length; i++) { | ||
regex = /translate\/my_memory\/(.+)\/(.+)/; | ||
match = scripts[i].src.match(regex); | ||
|
||
if (match != undefined) { | ||
return [match[1], match[2]]; | ||
} | ||
} | ||
|
||
return ['', '']; | ||
} | ||
|
||
function format_translations_my_memory(ts) { | ||
text = ''; | ||
|
||
for (i in ts) { | ||
origi = ts[i].segment; | ||
first = ts[i].translation; | ||
|
||
if (origi != first) | ||
text += format_translation_my_memory(first); | ||
} | ||
|
||
return text; | ||
} | ||
|
||
|
||
function format_translation_my_memory(t) { | ||
if (t == undefined) | ||
return ''; | ||
|
||
if (translations.indexOf(t) != -1) | ||
return ''; | ||
else | ||
translations.push(t); | ||
|
||
text = '<li><i>' + t + '</i>'; | ||
|
||
text += '</li>'; | ||
|
||
return text; | ||
} | ||
|
||
/* Wordreference */ | ||
function ddg_spice_translate_wordreference(ir) { | ||
items = new Array(); | ||
|
||
params = get_params_wordreference(); | ||
dict = params[0]; | ||
word = params[1]; | ||
to = dict.slice(-2); | ||
|
||
if ((word == '') || (dict == '')) | ||
return; | ||
|
||
items[0] = new Array(); | ||
items[0]["h"] = langs[to] + ' translations for <i>' + word + '</i>'; | ||
items[0]['s'] = 'Wordreference.com'; | ||
items[0]['u'] = 'http://wordreference.com/' + dict + '/' + word; | ||
items[0]["force_big_header"] = true; | ||
|
||
if (ir["Error"]) | ||
return; | ||
|
||
text = '<ul>'; | ||
|
||
text += format_term_wordreference(ir.term0); | ||
|
||
if (ir.term1 != undefined) | ||
text += format_term_wordreference(ir.term1); | ||
|
||
text += '</ul>'; | ||
|
||
items[0]['a'] = text; | ||
|
||
nra(items); | ||
} | ||
|
||
function get_params_wordreference() { | ||
scripts = document.getElementsByTagName('script'); | ||
|
||
for (i = 0; i < scripts.length; i++) { | ||
regex = /translate\/wordreference\/(.+)\/(.+)/; | ||
match = scripts[i].src.match(regex); | ||
|
||
if (match != undefined) { | ||
return [match[1], match[2]]; | ||
} | ||
} | ||
|
||
return ['', '']; | ||
} | ||
|
||
function format_term_wordreference(term) { | ||
text = format_translations_wordreference(term.PrincipalTranslations); | ||
|
||
if (term.AdditionalTranslations) | ||
text += format_translations_wordreference(term.AdditionalTranslations); | ||
|
||
return text; | ||
} | ||
|
||
function format_translations_wordreference(ts) { | ||
text = ''; | ||
|
||
for (i in ts) { | ||
origi = ts[i].OriginalTerm; | ||
first = ts[i].FirstTranslation; | ||
secnd = ts[i].SecondTranslation; | ||
|
||
if (origi.term != first.term) | ||
text += format_translation_wordreference(first); | ||
|
||
if ((secnd != undefined) && (origi.term != secnd.term)) | ||
text += format_translation_wordreference(secnd); | ||
} | ||
|
||
return text; | ||
} | ||
|
||
function format_translation_wordreference(t) { | ||
if (t == undefined) | ||
return ''; | ||
|
||
if (translations.indexOf(t.term) != -1) | ||
return ''; | ||
else | ||
translations.push(t.term); | ||
|
||
text = '<li><i>' + t.term + '</i>'; | ||
|
||
text += '</li>'; | ||
|
||
return text; | ||
} |
Oops, something went wrong.