From: Frank Lichtenheld Date: Fri, 16 Nov 2007 17:04:40 +0000 (+0100) Subject: Switch to Locale::Maketext X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=1b69eafb0f33afa134e3a0c0c794c72cbaf37294;p=deb%2Fpackages.git Switch to Locale::Maketext For now the template stuff works ok. The gettext stuff from within the Perl code only returns the English text and create_index_pages is probably completly broken. --- diff --git a/lib/Packages/Dispatcher.pm b/lib/Packages/Dispatcher.pm index 86be87c..b2030aa 100755 --- a/lib/Packages/Dispatcher.pm +++ b/lib/Packages/Dispatcher.pm @@ -101,10 +101,6 @@ sub do_dispatch { debug( "LANGUAGES=@all_langs header=". ($input->http("Accept-Language")||''). " http_lang=$http_lang", 1 ) if DEBUG; - bindtextdomain ( 'pdo', $LOCALES ); - bindtextdomain ( 'templates', $LOCALES ); - bindtextdomain ( 'langs', $LOCALES ); - textdomain( 'pdo' ); # backwards compatibility stuff debug( "SCRIPT_URL=$ENV{SCRIPT_URL} SCRIPT_URI=$ENV{SCRIPT_URI}" ) if DEBUG; @@ -277,17 +273,9 @@ sub do_dispatch { my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts ); Packages::CGI::init_url( $input, \%params, \%opts ); - my $locale = get_locale($opts{lang}); - my $charset = get_charset($opts{lang}); - setlocale ( LC_ALL, $locale ) - or do { debug( "couldn't set locale $locale, using default" ) if DEBUG; - setlocale( LC_ALL, get_locale() ) - or do { - debug( "couldn't set default locale either" ) if DEBUG; - setlocale( LC_ALL, "C" ); - }; - }; - debug( "locale=$locale charset=$charset", 1 ) if DEBUG; + my $charset = "UTF-8"; + my $cat = Packages::I18N::Locale->get_handle( $opts{lang} ) + or die "get_handle failed"; $opts{h_suites} = { map { $_ => 1 } @suites }; $opts{h_sections} = { map { $_ => 1 } @sections }; @@ -312,6 +300,7 @@ sub do_dispatch { my $template = new Packages::Template( $TEMPLATEDIR, $opts{format}, { lang => $opts{lang}, charset => $charset, + cat => $cat, debug => ( DEBUG ? $opts{debug} : 0 ) }, ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) ); diff --git a/lib/Packages/I18N/Locale.pm b/lib/Packages/I18N/Locale.pm index fd98758..94fd899 100644 --- a/lib/Packages/I18N/Locale.pm +++ b/lib/Packages/I18N/Locale.pm @@ -3,58 +3,27 @@ package Packages::I18N::Locale; use strict; use warnings; -use Exporter; -use Locale::gettext; - -our @ISA = qw( Exporter ); -# the reason we have both _g and _ is simply that there -# seem to be some situations where Perl doesn't handle _ -# correctly. If in doubt use _g -our @EXPORT = qw( get_locale get_charset _g N_ ); - -my %lang2loc = ( en => "en_US", - cs => "cs_CZ", - da => "da_DK", - ja => "ja_JP", - sv => "sv_SE", - uk => "uk_UA", - default => "en_US", - ); - -# this can probably be removed now that all locales are available in UTF-8 -my %lang2charset = ( - default => 'UTF-8', - ); - -sub get_locale { - my $lang = shift; - my $locale = $lang; - - return "$lang2loc{default}.".get_charset() unless $lang; - - if ( length($lang) == 2 ) { - $locale = $lang2loc{$lang} || ( "${lang}_" . uc $lang ); - } elsif ( $lang !~ /^[a-z][a-z]_[A-Z][A-Z]$/ ) { - warn "get_locale: couldn't determine locale\n"; - return; - } - $locale .= ".".get_charset($lang); - return $locale; -} - -sub get_charset { - my $lang = shift; - - return $lang2charset{default} unless $lang; - return $lang2charset{$lang} || $lang2charset{default}; -} - -sub tt_gettext { - my ($str, @args) = @_; - return dgettext( 'templates', $str ) unless @args; - return sprintf(dgettext( 'templates', $str ), @args); +use base 'Locale::Maketext'; +use Locale::Maketext::Lexicon { + '*' => [Gettext => '/home/djpig/debian/www.d.o/packages/po/pdo.*.po', + Gettext => '/home/djpig/debian/www.d.o/packages/po/templates.*.po', + Gettext => '/home/djpig/debian/www.d.o/packages/po/langs.*.po'], + _auto => 1, + _style => 'gettext', +}; + +use base 'Exporter'; + +our @EXPORT = qw( _g N_ ); + +sub _g { return $_[0]; } +sub N_ { return $_[0]; } + +sub g { + my ($self, $format, @args) = @_; + my $result = $self->maketext($format, @args); + return sprintf($result, @args) if $result =~ /%[su]/; + return $result; } -sub _g { return gettext( $_[0] ) } -sub N_ { return $_[0] } 1; diff --git a/lib/Packages/Template.pm b/lib/Packages/Template.pm index 60cabd6..6ae96b4 100644 --- a/lib/Packages/Template.pm +++ b/lib/Packages/Template.pm @@ -36,7 +36,9 @@ sub new { }; $vars->{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) }; $vars->{make_url} = sub { return &Packages::CGI::make_url(@_) }; - $vars->{g} = sub { return &Packages::I18N::Locale::tt_gettext(@_) }; + if ($vars->{cat}) { + $vars->{g} = sub { return Packages::I18N::Locale::g($vars->{cat}, @_) }; + } $vars->{extract_host} = sub { my $uri = URI->new($_[0]); my $host = $uri->host; $host .= ':'.$uri->port if $uri->port != $uri->default_port;