PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Translate/FromToPhrase.pm

https://github.com/robertbrook/zeroclickinfo-spice
Perl | 42 lines | 25 code | 15 blank | 2 comment | 1 complexity | 4f800e5517fa01a0dff34df993d2eaf6 MD5 | raw file
  1. package DDG::Spice::Translate::FromToPhrase;
  2. use DDG::Spice;
  3. use Moo;
  4. with('DDG::SpiceRole::Translate');
  5. attribution github => ['https://github.com/ghedo', 'ghedo' ],
  6. web => ['http://ghedini.me', 'Alessandro Ghedini'];
  7. 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';
  8. spice to => 'http://mymemory.translated.net/api/get?q=$3&langpair=$1|$2&de=office@duckduckgo.com';
  9. spice from => '(.+)\/(.+)\/(.+)';
  10. spice wrap_jsonp_callback => 1;
  11. triggers start => "translate";
  12. handle query_lc => sub {
  13. my $query = $_;
  14. $query =~ s/\s+/ /; #merge multiple spaces
  15. if($query =~ /^translate (.+) from ($langs)(?: to ($langs))?$/) {
  16. my ($phrase, $from, $to) = ($1, $2, $3);
  17. $from = shorten_lang($from);
  18. $to = (defined $to) ? shorten_lang($to) : substr($lang->locale, 0, 2);
  19. my $dict = $from.$to;
  20. # Only use mymemory for multi-word translation
  21. # or non-english translations
  22. return unless ($phrase =~ /\w+\s+\w+/ or $dict !~ /en/);
  23. return ($from, $to, $phrase);
  24. }
  25. return;
  26. };
  27. 1;