Skip to content

Instantly share code, notes, and snippets.

@takihito
Created September 22, 2013 07:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
package Project::Ja2Ko;
use strict;
use warnings;
use utf8;
use parent 'Exporter';
BEGIN {
my %constants = (
TRANSLATION_LIST => {
"こんにちは" => "안녕하세요"
},
);
require constant;
import constant \%constants;
our @EXPORT = keys %constants;
}
1;
package Plack::Middleware::Ja2Ko;
use strict;
use warnings;
use parent qw(Plack::Middleware);
use Plack::Util ();
use Scalar::Util ();
use Encode qw(encode decode decode_utf8 encode_utf8);
use Project::Ja2Ko;
our $VERSION = '0.01';
my $module;
sub call {
my ( $self, $env ) = @_;
my $res = $self->app->($env);
$self->response_cb(
$res,
sub {
my $res = shift;
return unless $res->[2];
return if (ref $res->[2] eq 'Plack::Util::IOWithPath');
my $encode_txt = $res->[2]->[0];
$encode_txt = Encode::decode_utf8( $encode_txt )
unless Encode::is_utf8( $encode_txt );
for my $ja ( keys TRANSLATION_LIST ) {
my $ko = TRANSLATION_LIST->{$ja};
if ( $encode_txt ) {
$encode_txt =~ s!>$ja<!>$ko<!g;
}
}
$encode_txt = Encode::encode_utf8 ( $encode_txt );
$res->[2] = [$encode_txt];
}
);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment