[Catalyst] I18N with quotation marks

Larry Leszczynski larryl at emailplus.org
Wed Jul 1 18:51:08 GMT 2009


Hi Ton -

> However, if the translated value contains quotations (such as: s'il
> vous plait), then it could break the HTML:
>
> <select value='[% c.loc("Please select one") %]'>
>
> or the javascript:
>
> alert('[% c.loc("Please select one") %]');

We create some custom scalar ops in a subclass of Catalyst::View::TT
(code below) that let you do:

  <select value="[% c.loc("Please select one").escape_dq %]">

  alert('[% c.loc("Please select one").escape_sq %]');

HTH,
Larry

--------------------------------------------

package Platform::View::TT;

# This is "MyApp/View/TT.pm":

use strict;
use warnings;

use base 'My::Catalyst::View::TT';

1;

--------------------------------------------

package My::Catalyst::View::TT;

use strict;
use warnings;

use base 'Catalyst::View::TT';

$Template::Stash::SCALAR_OPS->{escape_q} = sub {
    my $s = shift;
    $s =~ s/\\/\\\\/g;
    $s =~ s/"/\\"/g;
    $s =~ s/'/\\'/g;
    return $s;
};

$Template::Stash::SCALAR_OPS->{escape_dq} = sub {
    my $s = shift;
    $s =~ s/\\/\\\\/g;
    $s =~ s/"/\\"/g;
    return $s;
};

$Template::Stash::SCALAR_OPS->{escape_sq} = sub {
    my $s = shift;
    $s =~ s/\\/\\\\/g;
    $s =~ s/'/\\'/g;
    return $s;
};

1;

--------------------------------------------



More information about the Catalyst mailing list