# $Id: UTF8Hack.pl,v 1.2 2003/02/26 16:23:31 akosut Exp $ # by Alexei Kosut # # Adding utf8_hack="1" to a template tag will check its output for # UTF-8 text. If the output is not valid UTF-8, it is assumed to be # ISO-8859-1, and is translated to UTF-8 appropriately. use MT; use MT::Template::Context; use Text::Iconv; use strict; use warnings; use bytes; # "Borrowed" from the LiveJournal source code sub is_utf8 ($) { return $_[0] =~ m/^([\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf])*$/; } MT::Template::Context->add_global_filter(utf8_hack => sub { my $s = shift; if (!is_utf8($s)) { $s = Text::Iconv->new("iso-8859-1", "utf-8")->convert($s); } return $s; });