Search code examples
perlencodingutf-8mojolicious

Only one page need to be shown at specific encoding


I have mojolicous app which works in utf-8 encoding. All my files (templates,code, anything) in utf-8.

But for third-party service I need to show one of my page in win-1251 encoding.

I add new format in startup :

$self->types->type(wbm => 'text/html; charset=win-1251'); 

I add template/show/mypage.wbm.ep

<%= $txt %>

And in my controller I set the format:

$self->render( 'show/mypage', format => 'wbm', txt => 'Превед, win1251');

And when i visit 127.0.0.1:3000/show/mypage I see right type text/html; charset=win-1251 and wrong characters like

УУАУЅУЂУЅУЄ, win1251

I tried do encoding in template like:

<%=  Encode::encode('windows-1251',$txt) %>

And tried do same in controller, but page still shown incorrect.

Help me please.

UPD:

Here is test projects https://github.com/korjavin/MojoWinPage with full code.


Solution

  • It was solved by

    1. render partial
    2. encode it
    3. send as raw data
    my $page = $self->render( 'show/mypage', format => 'wbm', txt => $txt, partial> => 1);
    my $win=Mojo::Util::encode ('cp-1251',$page);
    $self->render(data=>$win);
    

    Thanks to Sebastian Riedel (https://groups.google.com/forum/#!topic/mojolicious/bX_LLQIsop8)