Jifty
view release on metacpan or search on metacpan
lib/Jifty/I18N.pm view on Meta::CPAN
local $ENV{REQUEST_METHOD} = Jifty->web->request->method
if Jifty->web->request;
local $ENV{HTTP_ACCEPT_LANGUAGE} = Jifty->web->request->header("Accept-Language") || ""
if Jifty->web->request;
$$DynamicLH = $self->get_handle($lang ? $lang : ()) if $DynamicLH;
}
=head2 get_current_language
Get the current language for this request, formatted as a Locale::Maketext
subclass string (i.e., C<zh_tw> instead of C<zh-TW>).
=cut
sub get_current_language {
return unless $DynamicLH;
my ($lang) = ref($$DynamicLH) =~ m/::(\w+)$/;
return $lang;
}
=head2 refresh
Used by L<Jifty::Handler> in DevelMode to reload F<.po> files whenever they
are modified on disk.
=cut
my $LAST_MODIFED = '';
sub refresh {
if ( Jifty->config->framework('L10N')->{'Disable'} && !$loaded) {
# skip loading po, but still do the translation for maketext
require Locale::Maketext::Lexicon;
my $lh = __PACKAGE__->get_handle;
my $orig = Jifty::I18N::en->can('maketext');
no warnings 'redefine';
*Jifty::I18N::en::maketext = Locale::Maketext::Lexicon->_style_gettext($orig);
__PACKAGE__->install_global_loc(\$lh);
++$loaded;
return;
}
my $modified = join(
',',
# sort map { $_ => -M $_ } map { glob("$_/*.po") } ( Jifty->config->framework('L10N')->{'PoDir'}, Jifty->config->framework('L10N')->{'DefaultPoDir'}
sort map { $_ => -M $_ } map { glob($_) } _get_file_patterns()
);
if ($modified ne $LAST_MODIFED) {
Jifty::I18N->new;
$LAST_MODIFED = $modified;
}
}
=head2 promote_encoding STRING [CONTENT-TYPE]
Return STRING promoted to our best-guess of an appropriate
encoding. STRING should B<not> have the UTF-8 flag set when passed in.
Optionally, you can pass a MIME content-type string as a second
argument. If it contains a charset= parameter, we will use that
encoding. Failing that, we use Encode::Guess to guess between UTF-8
and iso-latin-1. If that fails, and the string validates as UTF-8, we
assume that. Finally, we fall back on returning the string as is.
=cut
# XXX TODO This possibly needs to be more clever and/or configurable
sub promote_encoding {
my $class = shift;
my $string = shift;
my $content_type = shift;
my $charset;
# Don't bother parsing the Content-Type header unless it mentions "charset".
# This is to avoid the "Unquoted / not allowed in Content-Type" warnings when
# the Base64-encoded MIME boundary string contains "/".
if ($content_type and $content_type =~ /charset/i) {
$content_type = Email::MIME::ContentType::parse_content_type($content_type);
$charset = $content_type->{attributes}->{charset};
}
# XXX TODO Is this the right thing? Maybe we should just return
# the string as-is.
Encode::_utf8_off($string);
if($charset) {
$string = Encode::decode($charset, $string);
} else {
my $encoding = Encode::Guess->guess($string);
if(!ref($encoding)) {
local $@;
eval {
# Try utf8
$string = Encode::decode_utf8($string, 1);
};
if($@) {
warn "Unknown encoding -- none specified, couldn't guess, not valid UTF-8";
}
} else {
$string = $encoding->decode($string) if $encoding;
}
}
return $string;
}
=head2 maybe_decode_utf8 STRING
Attempt to decode STRING as UTF-8. If STRING is not valid UTF-8, or
already contains wide characters, return it undecoded.
N.B: In an ideal world, we wouldn't need this function, since we would
know whether any given piece of input is UTF-8. However, the world is
not ideal.
=cut
sub maybe_decode_utf8 {
( run in 1.649 second using v1.01-cache-2.11-cpan-d7f47b0818f )