Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

UTF8 fun and games again

by ultranerds (Pilgrim)
on Oct 17, 2011 at 18:01 UTC ( #931984=perlquestion: print w/ replies, xml ) Need Help??
ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to get the Google Translate API function working, using:

sub Do_Translate { my ($from,$to,$string) = @_; my $url = "https://www.googleapis.com/language/translate/v2?key=$k +ey&q=$string&source=$from&target=$to"; # print "getting URL: $url \n"; #my $page = get($url); my $ua = LWP::UserAgent->new(); my $body = $ua->get($url); my $json = from_json($body->decoded_content); use Data::Dumper; print Dumper($json); print "GOT: $json->{data}->{translations}[0]->{translatedText} \n" +; return utf8($json->{data}->{translations}[0]->{translatedText} +)->latin1; }
This prints out fine in PuttY (with UTF8 set as the encoding type);
GOT: Ítes-vous $VAR1 = 'Ítes-vous';
...yet when I update this value in the database, it converts it to:

êtes-vous

If I manually update to the string, it updates the field fine:

$DB->table('Links')->update( { Title => "Ítes-vous" } , { ID => 32902 } ) || die $GT::SQL::error;

Anyone got any ideas/suggestions as to what I can try?

TIA!

Andy

Comment on UTF8 fun and games again
Select or Download Code
Re: UTF8 fun and games again
by moritz (Cardinal) on Oct 17, 2011 at 18:10 UTC

    The problem seems to be in some of the code you don't show, the code that talks to the database. It's a bit hard to debug that.

    Please read and understand this post, it will help you with your issue.

      Hi,

      Thanks for the reply. Here is the full test script:

      #!/usr/bin/perl use strict; use lib './'; use Links qw/$IN $DB $CFG $USER/; use CGI::Carp qw(fatalsToBrowser); use JSON; use Unicode::String qw(latin1 utf8); use LWP::UserAgent; print "Content-Type: text/html \n\n"; my $key = 'xxxx'; my $string = Do_Translate("en","fr",'are you'); $DB->table('Links')->update( { Title => "Ítes-vous" } , { ID => 32 +902 } ) || die $GT::SQL::error; # works $DB->table('Links')->update( { Title => $string } , { ID => 32902 +} ) || die $GT::SQL::error; # doesnt work sub Do_Translate { my ($from,$to,$string) = @_; my $url = "https://www.googleapis.com/language/translate/v2?key=$k +ey&q=$string&source=$from&target=$to"; my $ua = LWP::UserAgent->new(); my $body = $ua->get($url); my $json = from_json($body->decoded_content); use Data::Dumper; print Dumper($json); print "GOT: $json->{data}->{translations}[0]->{translatedText} \n" +; return utf8($json->{data}->{translations}[0]->{translatedText})->l +atin1; }


      Just gonna take a look at that link too

      Cheers

      Andy

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://931984]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-08-22 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (145 votes), past polls