Optimise use of gettext, and avoid ugly warnings if Locale::gettext is not available.
The test suite was emitting a lot of ugly gettext warnings; setting LC_ALL didn't solve the problem for all locale setups (since ikiwiki remaps it to LANG, and ikiwiki didn't know about the C locale). People also seem generally annoyed by the messages when Locale::Gettext is not installed, and I suspect will be generally happier if it just silently doesn't localize. The optimisation came about when I noticed that the gettext sub was doing rather a lot of work each call just to see if localisation is needed. We can avoid that work by caching, and the best thing to cache is a version of the gettext sub that does exactly the right thing. This was slightly complicated by the locale setting, which might need to override the original locale (or lack thereof) after gettext has been called. So it needs to invalidate the cache in that case. It used to do it via a global variable, which I am happy to have also gotten rid of.master
parent
331cc6cca1
commit
5418385336
34
IkiWiki.pm
34
IkiWiki.pm
|
@ -14,7 +14,7 @@ use open qw{:utf8 :std};
|
||||||
use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
|
use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
|
||||||
%pagestate %wikistate %renderedfiles %oldrenderedfiles
|
%pagestate %wikistate %renderedfiles %oldrenderedfiles
|
||||||
%pagesources %destsources %depends %hooks %forcerebuild
|
%pagesources %destsources %depends %hooks %forcerebuild
|
||||||
$gettext_obj %loaded_plugins};
|
%loaded_plugins};
|
||||||
|
|
||||||
use Exporter q{import};
|
use Exporter q{import};
|
||||||
our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
|
our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
|
||||||
|
@ -459,7 +459,7 @@ sub checkconfig () {
|
||||||
if (defined $config{locale}) {
|
if (defined $config{locale}) {
|
||||||
if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
|
if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
|
||||||
$ENV{LANG}=$config{locale};
|
$ENV{LANG}=$config{locale};
|
||||||
$gettext_obj=undef;
|
define_gettext();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1704,27 +1704,35 @@ sub file_pruned ($$) {
|
||||||
return $file =~ m/$regexp/ && $file ne $base;
|
return $file =~ m/$regexp/ && $file ne $base;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub gettext {
|
sub define_gettext () {
|
||||||
# Only use gettext in the rare cases it's needed.
|
# If translation is needed, redefine the gettext function to do it.
|
||||||
|
# Otherwise, it becomes a quick no-op.
|
||||||
|
no warnings 'redefine';
|
||||||
if ((exists $ENV{LANG} && length $ENV{LANG}) ||
|
if ((exists $ENV{LANG} && length $ENV{LANG}) ||
|
||||||
(exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
|
(exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
|
||||||
(exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
|
(exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
|
||||||
if (! $gettext_obj) {
|
*gettext=sub {
|
||||||
$gettext_obj=eval q{
|
my $gettext_obj=eval q{
|
||||||
use Locale::gettext q{textdomain};
|
use Locale::gettext q{textdomain};
|
||||||
Locale::gettext->domain('ikiwiki')
|
Locale::gettext->domain('ikiwiki')
|
||||||
};
|
};
|
||||||
if ($@) {
|
|
||||||
print STDERR "$@";
|
if ($gettext_obj) {
|
||||||
$gettext_obj=undef;
|
$gettext_obj->get(shift);
|
||||||
return shift;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $gettext_obj->get(shift);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return shift;
|
return shift;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*gettext=sub { return shift };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub gettext {
|
||||||
|
define_gettext();
|
||||||
|
gettext(@_);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub yesno ($) {
|
sub yesno ($) {
|
||||||
|
|
|
@ -18,6 +18,8 @@ ikiwiki (3.15) UNRELEASED; urgency=low
|
||||||
name, to support several cases including mercurial's long user
|
name, to support several cases including mercurial's long user
|
||||||
names on the RecentChanges page, and urls with spaces being handled
|
names on the RecentChanges page, and urls with spaces being handled
|
||||||
by the 404 plugin.
|
by the 404 plugin.
|
||||||
|
* Optimise use of gettext, and avoid ugly warnings if Locale::gettext
|
||||||
|
is not available. Closes: #532285
|
||||||
|
|
||||||
-- Joey Hess <joeyh@debian.org> Tue, 02 Jun 2009 17:03:41 -0400
|
-- Joey Hess <joeyh@debian.org> Tue, 02 Jun 2009 17:03:41 -0400
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ ok(! system("make -s ikiwiki.out"));
|
||||||
ok(! system("make extra_install DESTDIR=`pwd`/t/tmp/install PREFIX=/usr >/dev/null"));
|
ok(! system("make extra_install DESTDIR=`pwd`/t/tmp/install PREFIX=/usr >/dev/null"));
|
||||||
|
|
||||||
foreach my $plugin ("", "listdirectives") {
|
foreach my $plugin ("", "listdirectives") {
|
||||||
ok(! system("LC_ALL=C perl -I. ./ikiwiki.out -rebuild -plugin brokenlinks ".
|
ok(! system("perl -I. ./ikiwiki.out -rebuild -plugin brokenlinks ".
|
||||||
# always enabled because pages link to it conditionally,
|
# always enabled because pages link to it conditionally,
|
||||||
# which brokenlinks cannot handle properly
|
# which brokenlinks cannot handle properly
|
||||||
"-plugin smiley ".
|
"-plugin smiley ".
|
||||||
|
|
|
@ -5,7 +5,7 @@ use Test::More 'no_plan';
|
||||||
|
|
||||||
ok(! system("mkdir t/tmp"));
|
ok(! system("mkdir t/tmp"));
|
||||||
ok(! system("make -s ikiwiki.out"));
|
ok(! system("make -s ikiwiki.out"));
|
||||||
ok(! system("LC_ALL=C perl -I. ./ikiwiki.out -plugin inline -url=http://example.com -cgiurl=http://example.com/ikiwiki.cgi -rss -atom -underlaydir=underlays/basewiki -templatedir=templates t/tinyblog t/tmp/out"));
|
ok(! system("perl -I. ./ikiwiki.out -plugin inline -url=http://example.com -cgiurl=http://example.com/ikiwiki.cgi -rss -atom -underlaydir=underlays/basewiki -templatedir=templates t/tinyblog t/tmp/out"));
|
||||||
# This guid should never, ever change, for any reason whatsoever!
|
# This guid should never, ever change, for any reason whatsoever!
|
||||||
my $guid="http://example.com/post/";
|
my $guid="http://example.com/post/";
|
||||||
ok(length `grep '<guid>$guid</guid>' t/tmp/out/index.rss`);
|
ok(length `grep '<guid>$guid</guid>' t/tmp/out/index.rss`);
|
||||||
|
|
Loading…
Reference in New Issue