Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: UTF8 fun and games again

by ultranerds (Pilgrim)
on Oct 17, 2011 at 18:13 UTC ( #931987=note: print w/ replies, xml ) Need Help??


in reply to Re: UTF8 fun and games again
in thread UTF8 fun and games again

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


Comment on Re^2: UTF8 fun and games again
Download Code
Re^3: UTF8 fun and games again
by moritz (Cardinal) on Oct 17, 2011 at 18:20 UTC
      Hi,

      I can't post that code, as its all over the place (part of the API for the database in the script I'm using). I know that code works fine normally, as I've used UTF8 charachters loads). The problem has to be something to do with this code.
      If I do:
      my $string = Do_Translate("en","fr",'are you'); use Data::Dumper; print Dumper($string,'Ítes-vous');


      I see:

      $VAR1 = 'Ítes-vous'; $VAR2 = "\x{ad25}s-vous";


      Argh its driving me up the wall!

      BTW, here is the actual JSON data thats getting passed back:

      'translations' => [ { 'translatedText' => "\x{c3 +}\x{aa}tes-vous" } ]
        Ok, for anyone interested - its cos the JSON module needed to explicitly use the utf8 functions JSON->new->utf8 , and then also decode(). I've changed it to this, and it works perfectly now <G>
        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 $ua = LWP::UserAgent->new(); my $body = $ua->get($url); my $json = JSON->new->utf8; my $value = $json->decode( $body->decoded_content ); print "GOT: $value->{data}->{translations}[0]->{translatedText} \n +"; return utf8($value->{data}->{translations}[0]->{translatedText})-> +latin1; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2014-10-23 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (129 votes), past polls