Search code examples
encodingutf-8mojolicious

Mojolicious template displaying invalid UTF-8


I'm using Mojolicious (not Lite) together with CPAN::Redis.

I'm storing some data which is Japanese encoded in this way:

use Redis;
my $redis = Redis->new;
$redis->set("mykey",$val); 
# $val contains a string which was read from a file. 
# The value looks like: テスト

Later in the code I read that value from redis:

my $val = $redis->get("mykey");
print Dumper($val); #the value prints correctly in terminal
$self->stash(
    myvalue => $val
);
$self->render(
    template => "/pages/test"
);

And the template:

<!DOCTYPE html>
<html>
  <head>
      <title>Test</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
  <div><%= $myvalue %></div>
  ...

But it display the value like: ãã¹ã.

Changing the charset manually in the browser makes no difference (it is not displayed as expected).

Why if its displayed correctly in the terminal, it is not displayed correctly in the template?

Notes:

  • I used base64 encode/decode and it didn't change (I'm sure its not Redis).
  • I have Japanese fonts and settings installed correctly (I have been working with Japanese encodings for many years but first time I use Mojolicious templates for this task).
  • All files are saved in UTF-8 (no other encoding is being used).
  • If I write something in Japanese inside the template (hard coded) it displays correctly.

Solution

  • I hate to answer my own questions.. but I found the solution:

    use Encode qw(decode_utf8);
    ...
    $self->stash(
        myvalue => decode_utf8($val)
    );
    

    Simple as that. Not sure why its displayed correctly on the terminal... Probably "Dumper" is converting it?