From b8b780f25f2cb15def44c3579776c6df15cd6e90 Mon Sep 17 00:00:00 2001 From: Frank Lichtenheld Date: Sat, 13 Oct 2007 23:58:55 +0200 Subject: [PATCH] Add L10N support for Template Toolkit output These go to a new templates domain, to not have to mix xgettext and ttxgettext input for the same .pot file. --- bin/create_index_pages | 2 ++ bin/ttxgettext | 42 ++++++++++++++++++++++++------------- cron.d/500update_mo | 14 ++++++++----- lib/Packages/Dispatcher.pm | 6 ++++-- lib/Packages/I18N/Locale.pm | 5 +++++ 5 files changed, 48 insertions(+), 21 deletions(-) diff --git a/bin/create_index_pages b/bin/create_index_pages index 72498e9..9beac92 100755 --- a/bin/create_index_pages +++ b/bin/create_index_pages @@ -28,6 +28,7 @@ delete $ENV{'LC_ALL'}; delete $ENV{'LC_MESSAGES'}; bindtextdomain ( 'pdo', $LOCALES ); bindtextdomain ( 'sections', $LOCALES ); +bindtextdomain ( 'templates', $LOCALES ); textdomain( 'pdo' ); my $wwwdir = "$TOPDIR/www"; @@ -53,6 +54,7 @@ my (%pages, %tt_vars); $tt_vars{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) }; $tt_vars{make_url} = sub { return &Packages::CGI::make_url(@_) }; +$tt_vars{g} = sub { return &Packages::I18N::Locale::tt_gettext(@_) }; # needed to work around the limitations of the the FILTER syntax $tt_vars{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') }; $tt_vars{uri_escape} = sub { return URI::Escape::uri_escape(@_) }; diff --git a/bin/ttxgettext b/bin/ttxgettext index 7464f60..5e3aced 100755 --- a/bin/ttxgettext +++ b/bin/ttxgettext @@ -1,5 +1,14 @@ #! /usr/bin/perl -w +# copied from webwml/english/po/wmlxgettext.pl and +# adapted for Template Toolkit +# Copyright © 2002-2003 Denis Barbier +# Copyright © 2007 Frank Lichtenheld +# +# 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. use strict; use Getopt::Std; @@ -41,18 +50,23 @@ sub processFile { $repl = $1; } $file =~ s{^$prefix}{$repl}o unless $prefix eq '__'; - # Remove comments if they contain or - $text =~ s/^[ \t]*#.*<\/?gettext\b//mg; +# # Remove comments if they contain or +# $text =~ s/^[ \t]*#.*<\/?gettext\b//mg; $lineno = 1; - while ($text =~ m{\G(.*?)(]*>)(.*?)}gs) { # " -- to fix vim syntax hilighting :) + while ($text =~ m{\G(.*?) # begin at end of previous match, $1=prefix + (\bg\(\s* # "g(" , $2=whole match + (["']) # " quoting, $3=quotes + (.*?)\3 # string, $4=string + )}gxs) { $msgid = escape($4); $lineno += countNewline ($1.$2); $nextlineno = countNewline ($4);; - my $dom = ($3) ? $3 : 'templates'; - if ($domain ne $dom) { - $lineno += $nextlineno; - next; # wrong domain - } + my $dom = $domain; +# my $dom = ($3) ? $3 : 'templates'; +# if ($domain ne $dom) { +# $lineno += $nextlineno; +# next; # wrong domain +# } $comment = ''; if ($1 =~ m/(((^|\n)[ \t]*#.*)+)\n?[^\n]*$/) { $comment = $1; @@ -60,12 +74,12 @@ sub processFile { $comment =~ s/\n[ \t]*#\s*/\n/g; } push (@msgids, $msgid); - if (defined ($messages->{$msgid})) { - print STDERR "wmlxgettext: Warning: msgid multiple defined:\n\t". - $msgid."\n"; - } else { - $messages->{$msgid} = []; - } +# if (defined ($messages->{$msgid})) { +# print STDERR "ttxgettext: Warning: msgid multiple defined:\n\t". +# $msgid."\n"; +# } else { +# $messages->{$msgid} = []; +# } push (@{$messages->{$msgid}}, $comment, $file, $lineno); $lineno += $nextlineno; } diff --git a/cron.d/500update_mo b/cron.d/500update_mo index ec37c4f..168a2fe 100755 --- a/cron.d/500update_mo +++ b/cron.d/500update_mo @@ -2,17 +2,21 @@ . `dirname $0`/../config.sh -gettextfiles=$(find cgi-bin lib -not -name 'LanguageNames.pm' -a -not -name 'Sections.pm' -a \( -name '*.pl' -o -name '*.pm' \)); +cd "$topdir" +gettextfiles=$(find cgi-bin lib -not -name 'LanguageNames.pm' -a -not -name 'Sections.pm' -a \( -name '*.pl' -o -name '*.pm' \)) +templatefiles=$(find templates -name '*.tmpl' -o -name '*.inc') gettextfiles="$gettextfiles bin/create_index_pages" -podomains="pdo sections langs" +podomains="pdo templates sections langs" # Update pot # # Common options for all calls -xgettext_opts="--language=Perl --keyword=_ --keyword=N_ --keyword=_g --foreign-user --add-comments" +xgettext_opts="--language=Perl --keyword=N_ --keyword=_g --foreign-user --add-comments" echo gettextfiles=$gettextfiles +echo templatefiles=$templatefiles xgettext $xgettext_opts -d pdo -o ${podir}/pdo.pot ${gettextfiles} +$topdir/bin/ttxgettext templates ${templatefiles} >${podir}/templates.pot xgettext $xgettext_opts -d sections -o ${podir}/sections.pot ${libdir}/Packages/Sections.pm xgettext $xgettext_opts -d langs -o ${podir}/langs.pot ${libdir}/Packages/I18N/LanguageNames.pm @@ -40,8 +44,8 @@ for lang in ${polangs} do for domain in ${podomains} do - mv ${domain}.${lang}.po ${domain}.${lang}.po.tmp - msgmerge -q -F -o ${domain}.${lang}.po ${domain}.${lang}.po.tmp ${domain}.pot + cp ${domain}.${lang}.po ${domain}.${lang}.po.tmp + msgmerge --previous --quiet --sort-by-file -o ${domain}.${lang}.po ${domain}.${lang}.po.tmp ${domain}.pot rm ${domain}.${lang}.po.tmp # normalize paths in .po files perl -p -i -e "s,^#:\s*\Q${topdir}\E,#: .,go" ${domain}.${lang}.po diff --git a/lib/Packages/Dispatcher.pm b/lib/Packages/Dispatcher.pm index 221bfbe..3ae8004 100755 --- a/lib/Packages/Dispatcher.pm +++ b/lib/Packages/Dispatcher.pm @@ -102,8 +102,9 @@ sub do_dispatch { \@all_langs ) || 'en'; debug( "LANGUAGES=@all_langs header=". ($input->http("Accept-Language")||''). - " http_lang=$http_lang", 2 ) if DEBUG; + " http_lang=$http_lang", 1 ) if DEBUG; bindtextdomain ( 'pdo', $LOCALES ); + bindtextdomain ( 'templates', $LOCALES ); textdomain( 'pdo' ); # backwards compatibility stuff @@ -287,7 +288,7 @@ sub do_dispatch { setlocale( LC_ALL, "C" ); }; }; - debug( "locale=$locale charset=$charset", 2 ) if DEBUG; + debug( "locale=$locale charset=$charset", 1 ) if DEBUG; $opts{h_suites} = { map { $_ => 1 } @suites }; $opts{h_sections} = { map { $_ => 1 } @sections }; @@ -338,6 +339,7 @@ sub do_dispatch { $host .= ':'.$uri->port if $uri->port != $uri->default_port; return $host; }; + $page_content{g} = sub { return &Packages::I18N::Locale::tt_gettext(@_) }; # needed to work around the limitations of the the FILTER syntax $page_content{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') }; $page_content{uri_escape} = sub { return URI::Escape::uri_escape(@_) }; diff --git a/lib/Packages/I18N/Locale.pm b/lib/Packages/I18N/Locale.pm index 623bfc7..fd98758 100644 --- a/lib/Packages/I18N/Locale.pm +++ b/lib/Packages/I18N/Locale.pm @@ -49,6 +49,11 @@ sub get_charset { return $lang2charset{$lang} || $lang2charset{default}; } +sub tt_gettext { + my ($str, @args) = @_; + return dgettext( 'templates', $str ) unless @args; + return sprintf(dgettext( 'templates', $str ), @args); +} sub _g { return gettext( $_[0] ) } sub N_ { return $_[0] } -- 2.39.2