Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Google Translator -> wxPerl frontend

by Swalif (Scribe)
on Oct 03, 2010 at 17:32 UTC ( #863191=CUFP: print w/ replies, xml ) Need Help??

Hey monks,
I am learning wxperl & wxglade so I wanted to create a gui frontend to a random module. So why not try google translator. My code is appended in the post but there are some issues if you would help ..* the code is tested only on Ubuntu..
critical:
1- The application sometimes seems unresponsive if trans button is clicked especially at the first try or if the autodetect is chosen. I know there should be some delay because of the connection but I think there is a way to work around this and deal with unresponsive behavior. Maybe I am declaring the instance in the wrong place??(*See my comment on testing upon windows)
2- The application needs internet connection what is the best way to handle this issue?
Normal:
1- the layout will be ugly upon maximize, any suggestion on how to fix the size and disable maximizing..?
2- wxCB_SORT on comboBox will not sort the items alphabetically..
3- Please any suggestions or criticism on the code.. I am still learning. thanks

#!/usr/bin/perl -w -- # generated by wxGlade 0.6.3 on Sun Oct 3 14:38:31 2010 # To get wxPerl visit http://wxPerl.sourceforge.net/ use Wx 0.15 qw[:allclasses]; use strict; package MyFrame1; use Wx qw[:everything]; use base qw(Wx::Frame); use strict; use Lingua::Translate; Lingua::Translate::config ( back_end => 'Google', referer => 'http://dheeb.wordpress.com', format => 'text', userip => '192.168.1.1', ); sub new { my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_ +; $parent = undef unless defined $parent; $id = -1 unless defined $id; $title = "" unless defined $title; $pos = wxDefaultPosition unless defined $pos; $size = wxDefaultSize unless defined $size; $name = "" unless defined $name; # begin wxGlade: MyFrame1::new $style = wxICONIZE|wxCAPTION|wxMINIMIZE|wxCLOSE_BOX|wxMINIMIZE_BOX +|wxMAXIMIZE|wxMAXIMIZE_BOX|wxSYSTEM_MENU|wxRESIZE_BORDER|wxFRAME_NO_T +ASKBAR|wxCLIP_CHILDREN unless defined $style; $self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $sty +le, $name ); $self->{label_1} = Wx::StaticText->new($self, -1, "From : ", wxDef +aultPosition, wxDefaultSize, ); $self->{origin} = Wx::ComboBox->new($self, -1, "", wxDefaultPositi +on, wxDefaultSize, ["AutoDetect", "Arabic", "English", "French", "Dut +ch", "German", "Persian", "Turkish", "Japanese", "Italian", "Slovak", + "Serbian", "Thai", "Hindi", "Hebrew", "Spanish", "Greek", "Russian", + "Swedish", "Croation", "Polish", "Portuguese", "Filipino", "Irish", +"Malay", "Belarusian", "Czech", "Norwegian", "Swahili", "Catalan", "B +ulgarian", "Korean", "Indonesian", "Chinese", "Vietnamese", "Yiddish" +, "Afrikaans"], wxCB_DROPDOWN|wxCB_SIMPLE|wxCB_DROPDOWN|wxCB_READONLY +|wxCB_SORT); $self->{Swap} = Wx::Button->new($self, -1, "Swap <->"); $self->{label_2} = Wx::StaticText->new($self, -1, "To :", wxDefaul +tPosition, wxDefaultSize, ); $self->{destination} = Wx::ComboBox->new($self, -1, "", wxDefaultP +osition, wxDefaultSize, ["Arabic", "English", "French", "Dutch", "Ger +man", "Persian", "Turkish", "Japanese", "Italian", "Slovak", "Serbian +", "Thai", "Hindi", "Hebrew", "Spanish", "Greek", "Russian", "Swedish +", "Croation", "Polish", "Portuguese", "Filipino", "Irish", "Malay", +"Belarusian", "Czech", "Norwegian", "Swahili", "Catalan", "Bulgarian" +, "Korean", "Indonesian", "Chinese", "Vietnamese", "Yiddish", "Afrika +ans", "Ukrainian"], wxCB_DROPDOWN|wxCB_SIMPLE|wxCB_DROPDOWN|wxCB_READ +ONLY|wxCB_SORT); $self->{Trans} = Wx::Button->new($self, -1, "Trans!"); $self->{origin_text} = Wx::TextCtrl->new($self, -1, "", wxDefaultP +osition, wxDefaultSize, wxTE_MULTILINE|wxHSCROLL); $self->{translated_text} = Wx::TextCtrl->new($self, -1, "", wxDefa +ultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY); $self->{Credits} = Wx::Button->new($self, -1, "Credits"); $self->{Exit} = Wx::Button->new($self, -1, "Exit"); $self->__set_properties(); $self->__do_layout(); Wx::Event::EVT_BUTTON($self, $self->{Swap}->GetId, \&OnSwap); Wx::Event::EVT_BUTTON($self, $self->{Trans}->GetId, \&OnTrans); Wx::Event::EVT_BUTTON($self, $self->{Credits}->GetId, \&OnCredit); Wx::Event::EVT_BUTTON($self, $self->{Exit}->GetId, \&OnExit); # end wxGlade return $self; } sub __set_properties { my $self = shift; # begin wxGlade: MyFrame1::__set_properties $self->SetTitle("GTranslate"); $self->{origin}->SetSelection(0); $self->{destination}->SetSelection(0); $self->{origin_text}->SetMinSize(Wx::Size->new(428, 77)); # end wxGlade } sub __do_layout { my $self = shift; # begin wxGlade: MyFrame1::__do_layout $self->{sizer_2} = Wx::BoxSizer->new(wxVERTICAL); $self->{sizer_3} = Wx::BoxSizer->new(wxVERTICAL); $self->{sizer_4} = Wx::BoxSizer->new(wxHORIZONTAL); $self->{sizer_5} = Wx::BoxSizer->new(wxHORIZONTAL); $self->{sizer_5}->Add($self->{label_1}, 0, wxALIGN_CENTER_HORIZONT +AL|wxALIGN_CENTER_VERTICAL, 0); $self->{sizer_5}->Add($self->{origin}, 0, wxALIGN_CENTER_HORIZONTA +L|wxALIGN_CENTER_VERTICAL, 0); $self->{sizer_5}->Add($self->{Swap}, 0, wxALIGN_CENTER_HORIZONTAL| +wxALIGN_CENTER_VERTICAL, 0); $self->{sizer_5}->Add($self->{label_2}, 0, wxALIGN_CENTER_HORIZONT +AL|wxALIGN_CENTER_VERTICAL, 0); $self->{sizer_5}->Add($self->{destination}, 0, wxALIGN_CENTER_HORI +ZONTAL|wxALIGN_CENTER_VERTICAL, 0); $self->{sizer_5}->Add($self->{Trans}, 0, wxALIGN_CENTER_HORIZONTAL +|wxALIGN_CENTER_VERTICAL, 0); $self->{sizer_3}->Add($self->{sizer_5}, 1, wxEXPAND, 0); $self->{sizer_3}->Add($self->{origin_text}, 0, wxEXPAND|wxALIGN_CE +NTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0); $self->{sizer_3}->Add($self->{translated_text}, 0, wxEXPAND|wxALIG +N_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0); $self->{sizer_4}->Add($self->{Credits}, 0, wxALIGN_RIGHT, 0); $self->{sizer_4}->Add($self->{Exit}, 0, wxALIGN_RIGHT, 0); $self->{sizer_3}->Add($self->{sizer_4}, 1, wxEXPAND, 0); $self->{sizer_2}->Add($self->{sizer_3}, 1, wxEXPAND, 0); $self->SetSizer($self->{sizer_2}); $self->{sizer_2}->Fit($self); $self->{sizer_2}->SetSizeHints($self); $self->Layout(); # end wxGlade } sub OnTrans { my ($self, $event) = @_; # wxGlade: MyFrame1::OnTrans <event_handler> if ($self->{origin_text}->GetValue()) { my $tag_1 = getTag($self->{origin}->GetValue()); my $tag_2 = getTag($self->{destination}->GetValue()); my $message = $self->{origin_text}->GetValue(); my $object = Lingua::Translate->new( src => $tag_1, dest => $tag_2 + ); my $trans = $object->translate($message); $self->{translated_text}->SetValue($trans);} else {$event->Skip;} # end wxGlade } sub OnCredit { my ($self, $event) = @_; # wxGlade: MyFrame1::OnCredit <event_handler> my $credit = Wx::MessageDialog->new( $self, "All Credit goes to : \n Google : http://translate.google.com \n CPAN : Lingua::Translate - Lingua::Translate::Google \n wxPerl & wxGlade : perl_sourcer\@yahoo.com \n Still under testing ;) ", "Credits"); $credit->ShowModal; # end wxGlade } sub OnExit { my ($self, $event) = @_; # wxGlade: MyFrame1::OnExit <event_handler> $self->Close(); # end wxGlade } sub OnSwap { my ($self, $event) = @_; # wxGlade: MyFrame1::OnSwap <event_handler> my $first = $self->{origin}->GetValue(); my $second = $self->{destination}->GetValue(); if($first eq "AutoDetect"){ $self->{translated_text}->SetValue("Can't use AutoDetect on desti +nation ! Please choose a language & try again ..");} else { ($first, $second) = ($second, $first); $self->{origin}->SetValue($first); $self->{destination}->SetValue($second); } # end wxGlade } sub getTag { my $value = shift; my %tags = qw/ AutoDetect auto Arabic ar English en French fr Dutc +h nl German de Persian fa Turkish tr Japanese ja Italian it Slovak sk Serbian sr + Thai th Hindi hi Hebrew iw Spanish es Greek el Russian ru Swedish sv Croa +tion hr Polish pl Portuguese pt Filipino tl Irish ga Malay ms Belarusian +be Czech cs Norwegian no Swahili sw Catalan ca Bulgarian bg Korean ko Indones +ian id Chinese zh-CN Vietnamese vi Yiddish yi Afrikaans af Ukrainian +uk/; return my $tag = $tags{$value}; } # end of class MyFrame1 1; 1; package main; unless(caller){ local *Wx::App::OnInit = sub{1}; my $app = Wx::App->new(); Wx::InitAllImageHandlers(); my $frame_2 = MyFrame1->new(); $app->SetTopWindow($frame_2); $frame_2->Show(1); $app->MainLoop(); }

Comment on Google Translator -> wxPerl frontend
Download Code
Re: Google Translator -> wxPerl frontend
by jwkrahn (Monsignor) on Oct 03, 2010 at 18:01 UTC
    my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_ +; $parent = undef unless defined $parent; $id = -1 unless defined $id; $title = "" unless defined $title; $pos = wxDefaultPosition unless defined $pos; $size = wxDefaultSize unless defined $size; $name = "" unless defined $name;

    Perl now has the // operator which would simplify that to:

    my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_ +; $parent //= undef; $id //= -1; $title //= ""; $pos //= wxDefaultPosition; $size //= wxDefaultSize; $name //= "";

    Although I don't see much point in assigning undef to a variable that already contains undef?

      This code is wxGlade generated .. Thanks a lot for pointing the // operator great notice..
        Update:
        fixed the size of the frame by removing Min & Max buttons.. new code:
        $style = wxCAPTION|wxCLOSE_BOX|wxFRAME_NO_TASKBAR|wxCLIP_CHILDREN unless defined $style;
        Since only Lingua::Translate->new( src => $tag_1, dest => $tag_2 ) needs connection... how about:
        my ($message, $trans); my $tag_1 = getTag($self->{origin}->GetValue()); my $tag_2 = getTag($self->{destination}->GetValue()); eval { $object = Lingua::Translate->new( src => $tag_1, dest => $tag_2 ) + or die "Cannot Create an instance"; $message = $self->{origin_text}->GetValue(); $trans = $object->translate($message); }; if ($@){$self->{translated_text}->SetValue("An Error occured : $@ + \n Perhaps your not online."); } else{ $self->{translated_text}->SetValue($trans); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://863191]
Approved by ww
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-07-26 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls