/gist:27d6fca91cb24be5e396 Secret
Created
September 22, 2013 07:07
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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