Skip to content

Commit

Permalink
Item12180: Fix Languages tab. Fix tests of class names to check isa()…
Browse files Browse the repository at this point in the history
… instead. Experimental code to cache UI for feedback (disabled for now). Use simpler HTML when helpless.

git-svn-id: http://svn.foswiki.org/trunk@16333 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
TimotheLitt authored and TimotheLitt committed Jan 5, 2013
1 parent 6276f47 commit 5c0c86a
Show file tree
Hide file tree
Showing 14 changed files with 578 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ sub generateForm {
}
my $valueString;
my $type = $query->param("TYPEOF:$key") || 'UNKNOWN';
if ( $type eq 'PASSWORD' ) {
$type =~ /^(\w+)$/ or die "Invalid type $type\n";
$type = Foswiki::Configure::Type::load( $type, $1 );
if ( $type->isa('Foswiki::Configure::Types::PASSWORD') ) {
$valueString = '•' x 15;
}
elsif ( $type eq 'BOOLEAN' ) {
elsif ( $type->isa('Foswiki::Configure::Types::BOOLEAN') ) {
$valueString = $query->param($key) ? 1 : 0;
}
else {
Expand Down Expand Up @@ -173,10 +175,12 @@ sub processForm {
}
my $valueString;
my $type = $query->param("TYPEOF:$key") || 'UNKNOWN';
if ( $type eq 'PASSWORD' ) {
$type =~ /^(\w+)$/ or die "Invalid type $type\n";
$type = Foswiki::Configure::Type::load( $type, $1 );
if ( $type->isa('Foswiki::Configure::Types::PASSWORD') ) {
$valueString = '•' x 15;
}
elsif ( $type eq 'BOOLEAN' ) {
elsif ( $type->isa('Foswiki::Configure::Types::BOOLEAN') ) {
$valueString = $query->param($key) ? 1 : 0;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ sub generateForm {
next if ( $key =~ /^\{ConfigureGUI\}/ );
my $valueString;
my $type = $query->param("TYPEOF:$key") || 'UNKNOWN';
if ( $type eq 'PASSWORD' ) {
$type =~ /^(\w+)$/ or die "Invalid type $type\n";
$type = Foswiki::Configure::Type::load( $type, $1 );
if ( $type->isa('Foswiki::Configure::Types::PASSWORD') ) {
$valueString = '•' x 15;
}
elsif ( $type eq 'BOOLEAN' ) {
elsif ( $type->isa('Foswiki::Configure::Types::BOOLEAN') ) {
$valueString = $query->param($key) ? 1 : 0;
}
else {
Expand Down
161 changes: 161 additions & 0 deletions core/lib/Foswiki/Configure/Checkers/LANGUAGE.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# See bottom of file for license and copyright information
package Foswiki::Configure::Checkers::LANGUAGE;

use strict;
use warnings;

require Foswiki::Configure::Checker;
our @ISA = ('Foswiki::Configure::Checker');

=begin TML
---++ ObjectMethod check($valobj) -> $checkmsg
This is a generic (item-independent) checker for LANGUAGE items.
These have keys of the form {Languages}{ language }{Enabled}.
Functions:
check - verify (non) existence of compiled string files; correct issues.
provideFeedback - flush languages cache (since implies a change)
create/destroy compiled string files.
=cut

sub check {
my $this = shift;
my ($valobj) = @_;

my $e = '';

return $e unless ( $Foswiki::cfg{UserInterfaceInternationalisation} );

my ( $enabled, $keys, $lang, $dir, $compress ) = $this->_config($valobj);

return $e unless ($enabled);

return $this->ERROR("Missing language file $dir/$lang.po")
unless ( -r "$dir/$lang.po" );

if ($compress) {
my $ok = -r "$dir/$lang.mo" && -M "$dir/$lang.po" >= -M "$dir/$lang.mo";

unless ($ok) {
eval "require Locale::Msgfmt;";
if ($@) {
return $e
. $this->ERROR(
"Locale::Msgfmt can not be loaded, unable to compile strings."
);
}
my $umask = umask( oct(777) - $Foswiki::cfg{RCS}{filePermission} );
eval {
Locale::Msgfmt::msgfmt(
{
in => "$dir/$lang.po",
out => "$dir/$lang.mo",
verbose => 0
, # verbose is not documented, but prints results to STDERR
}
);
};
if ($@) {
$e .= $this->ERROR(
"Unable to compress strings: compilation failed.");
}
else {
$e .= $this->NOTE("Successfully compressed strings");
}
umask($umask);
}
}
else {
if ( -f "$dir/$lang.mo" ) {
if ( unlink("$dir/$lang.mo") ) {
$e .= $this->NOTE("Removed compressed strings");
}
else {
$e .= $this->ERROR(
"Unable to remove compressed strings in $dir/$lang.mo: $!");
}
}
}

return $e;
}

sub _config {
my ( $this, $valobj ) = @_;

my $keys = ref($valobj) ? $valobj->getKeys() : $valobj
or die "No keys for value";

my $enabled = $this->getItemCurrentValue($keys);

my $dir = $this->getCfg('{LocalesDir}');
my $compress = $Foswiki::cfg{LanguageFileCompression};

my $lang = $keys;
die "Invalid item key $keys for LANGUAGE\n"
unless ( $lang =~ s/^\{Languages\}\{'?([\w-]+)'?\}\{Enabled\}$/$1/ );

return ( $enabled, $keys, $lang, $dir, $compress );
}

sub provideFeedback {
my $this = shift;
my ( $valobj, $button, $label ) = @_;

$this->{FeedbackProvided} = 1;

# Normally, we call check first, but not if called by check.

my $e = $button ? $this->check($valobj) : '';

delete $this->{FeedbackProvided};

if ( $Foswiki::cfg{UserInterfaceInternationalisation} ) {
my ( $enabled, $keys, $lang, $dir, $compress ) =
$this->_config($valobj);

if ( -f "$dir/languages.cache" ) {
if ( unlink("$dir/languages.cache") ) {
$e .= $this->NOTE("Flushed languages cache");
}
else {
$e .= $this->ERROR("Failed to remove $dir/languages.cache: $!");
}
}
}

return wantarray ? ( $e, 0 ) : $e;
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2013 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Additional copyrights apply to some or all of the code in this
file as follows:
Copyright (C) 2000-2006 TWiki Contributors. All Rights Reserved.
TWiki Contributors are listed in the AUTHORS file in the root
of this distribution. NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.
67 changes: 67 additions & 0 deletions core/lib/Foswiki/Configure/Checkers/LanguageFileCompression.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# See bottom of file for license and copyright information
package Foswiki::Configure::Checkers::LanguageFileCompression;

use strict;
use warnings;

use Foswiki::Configure::Checker ();
our @ISA = ('Foswiki::Configure::Checker');

sub check {
my $this = shift;

return '';
}

# When compression state changes, run feedback for all enabled languages
# to create or remove compressed string files.
#
# Disabled languages will be handled if they are ever enabled - based on the
# value of LanguageFileCompression at that time.

sub provideFeedback {
my $this = shift;
my ( $valobj, $button, $label ) = @_;

my $e = '';

if ( $Foswiki::cfg{UserInterfaceInternationalisation} && wantarray ) {
my $keys = ref($valobj) ? $valobj->getKeys() : $valobj
or die "No keys for value";

my $enabled = $this->getItemCurrentValue($keys);

my @keys;
foreach my $key ( keys %{ $Foswiki::cfg{Languages} } ) {
my $ekey = $key;
$ekey = "'$key'" if ( $ekey =~ /\W/ );
$ekey = "{Languages}{$ekey}{Enabled}";
push @keys, $ekey
if ( $this->getItemCurrentValue($ekey) );
}

return ( $e, [@keys] );
}

return wantarray ? ( $e, 0 ) : $e;
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.
Original file line number Diff line number Diff line change
Expand Up @@ -66,74 +66,31 @@ sub check {
$n .= $this->checkPerlModules( 0, \@perl56 );
}

# If Internalization is enabled, compile the .po files into .mo files
# for all enabled languages.
#
if ( $Foswiki::cfg{UserInterfaceInternationalisation} ) {
eval "require Locale::Msgfmt";
if ($@) {
$n .= $this->WARN(
"Cannot compile language files - error loading 'Locale::Msgfmt'\n"
);
}
else {
my $compMsgs = '';
my $svUmask =
umask( oct(777) - $Foswiki::cfg{RCS}{filePermission} );

foreach my $lang ( keys %{ $Foswiki::cfg{Languages} } ) {
if ( $Foswiki::cfg{Languages}{$lang}{Enabled}
&& -e "$Foswiki::cfg{LocalesDir}/$lang.po" )
{
next
if (
-e "$Foswiki::cfg{LocalesDir}/$lang.mo"
&& ( -M "$Foswiki::cfg{LocalesDir}/$lang.po" >=
-M "$Foswiki::cfg{LocalesDir}/$lang.mo" )
);
if (
!$Foswiki::cfg{LanguageFileCompression}
&& -e "$Foswiki::cfg{LocalesDir}/$lang.mo"
&& ( -M "$Foswiki::cfg{LocalesDir}/$lang.po" <
-M "$Foswiki::cfg{LocalesDir}/$lang.mo" )
)
{
$n .= $this->WARN(
"Stale language file $Foswiki::cfg{LocalesDir}/$lang.mo should be removed - Language file compression is disabled"
);
}
next unless $Foswiki::cfg{LanguageFileCompression};

$compMsgs .= "Compiling $lang.po into $lang.mo <br/>\n";
eval {
Locale::Msgfmt::msgfmt(
{
in => "$Foswiki::cfg{LocalesDir}/$lang.po",
out => "$Foswiki::cfg{LocalesDir}/$lang.mo",
verbose => 0
, # verbose is not documented, but prints results to STDERR
}
);
};
if ($@) {
$n .= $this->ERROR(
"Compile of locale $lang.po failed - further compiles skipped"
);
$compMsgs .= $this->NOTE($@);
last;
}
}
}
umask($svUmask); # Restore modified umask
$n .= $this->NOTE(
"<b>Compiling modified Language files</b> found in $Foswiki::cfg{LocalesDir}<br/>\n$compMsgs"
) if $compMsgs;
}
}

return $n;
}

sub provideFeedback {
my $this = shift;
my ( $valobj, $button, $label ) = @_;

$this->{FeedbackProvided} = 1;

# Normally, we call check first, but not if called by check.

my $e = $button ? $this->check($valobj) : '';

delete $this->{FeedbackProvided};

return wantarray
? (
$e,
$Foswiki::cfg{UserInterfaceInternationalisation}
? [qw/{LanguageFileCompression}/]
: 0
)
: $e;
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Expand Down
Loading

0 comments on commit 5c0c86a

Please sign in to comment.