=head1 NAME
-tmpl_process3.pl - Experimental version of tmpl_process.pl
+tmpl_process3.pl - Alternative version of tmpl_process.pl
using gettext-compatible translation files
=cut
use Locale::PO;
use File::Temp qw( :POSIX );
use TmplTokenizer;
-use VerboseWarnings qw( error_normal warn_normal );
+use VerboseWarnings qw( :warn :die );
###############################################################################
print $output find_translation($t);
} elsif ($kind eq TmplTokenType::TEXT_PARAMETRIZED) {
my $fmt = find_translation($s->form);
- print $output TmplTokenizer::parametrize($fmt, [ map {
+ print $output TmplTokenizer::parametrize($fmt, 1, $s, sub {
+ $_ = $_[0];
my($kind, $t, $attr) = ($_->type, $_->string, $_->attributes);
$kind == TmplTokenType::TAG && %$attr?
- text_replace_tag($t, $attr): $t } $s->parameters ], [ $s->anchors ]);
+ text_replace_tag($t, $attr): $t });
} elsif ($kind eq TmplTokenType::TAG && %$attr) {
print $output text_replace_tag($t, $attr);
} elsif (defined $t) {
--help Display this help and exit
The -o option is ignored for the "create" and "update" actions.
+Try `perldoc $0' for perhaps more information.
EOF
exit($exitcode);
}
###############################################################################
+
sub usage_error (;$) {
for my $msg (split(/\n/, $_[0])) {
print STDERR "$msg\n";
# guess the charsets. HTML::Templates defaults to iso-8859-1
if (defined $href) {
+ die "$str_file: PO file is corrupted, or not a PO file\n"
+ unless defined $href->{'""'};
$charset_out = TmplTokenizer::charset_canon $2
if $href->{'""'}->msgstr =~ /\bcharset=(["']?)([^;\s"'\\]+)\1/;
for my $msgid (keys %$href) {
}
my $xgettext = './xgettext.pl'; # actual text extractor script
+my $st;
if ($action eq 'create') {
# updates the list. As the list is empty, every entry will be added
- die "$str_file: Output file already exists" if -f $str_file;
+ if (!-s $str_file) {
+ warn "Removing empty file $str_file\n";
+ unlink $str_file || die "$str_file: $!\n";
+ }
+ die "$str_file: Output file already exists\n" if -f $str_file;
my($tmph, $tmpfile) = tmpnam();
# Generate the temporary file that acts as <MODULE>/POTFILES.in
for my $input (@in_files) {
}
close $tmph;
# Generate the specified po file ($str_file)
- my $st = system ($xgettext, '-s', '-f', $tmpfile, '-o', $str_file);
+ $st = system ($xgettext, '-s', '-f', $tmpfile, '-o', $str_file);
warn_normal "Text extraction failed: $xgettext: $!\n", undef if $st != 0;
- unlink $tmpfile || warn_normal "$tmpfile: unlink failed: $!\n", undef;
+# unlink $tmpfile || warn_normal "$tmpfile: unlink failed: $!\n", undef;
} elsif ($action eq 'update') {
my($tmph1, $tmpfile1) = tmpnam();
}
close $tmph1;
# Generate the temporary file that acts as <MODULE>/<LANG>.pot
- my $st = system($xgettext, '-s', '-f', $tmpfile1, '-o', $tmpfile2,
+ $st = system($xgettext, '-s', '-f', $tmpfile1, '-o', $tmpfile2,
'--po-mode',
(defined $charset_in? ('-I', $charset_in): ()),
(defined $charset_out? ('-O', $charset_out): ()));
# Merge the temporary "pot file" with the specified po file ($str_file)
# FIXME: msgmerge(1) is a Unix dependency
# FIXME: need to check the return value
- system('msgmerge', '-U', '-s', $str_file, $tmpfile2);
+ $st = system('msgmerge', '-U', '-s', $str_file, $tmpfile2);
} else {
- warn_normal "Text extraction failed: $xgettext: $!\n", undef;
- warn_normal "Will not run msgmerge\n", undef;
+ error_normal "Text extraction failed: $xgettext: $!\n", undef;
+ error_additional "Will not run msgmerge\n", undef;
}
- unlink $tmpfile1 || warn_normal "$tmpfile1: unlink failed: $!\n", undef;
- unlink $tmpfile2 || warn_normal "$tmpfile2: unlink failed: $!\n", undef;
+# unlink $tmpfile1 || warn_normal "$tmpfile1: unlink failed: $!\n", undef;
+# unlink $tmpfile2 || warn_normal "$tmpfile2: unlink failed: $!\n", undef;
} elsif ($action eq 'install') {
if(!defined($out_dir)) {
} else {
usage_error('Unknown action specified.');
}
+
+if ($st == 0) {
+ printf "The %s seems to be successful.\n", $action;
+} else {
+ printf "%s FAILED.\n", "\u$action";
+}
exit 0;
###############################################################################
=head1 DESCRIPTION
-This is an experimental version of the tmpl_process.pl script,
-using standard gettext-style PO files. Note that the behaviour
-of this script should still be considered unstable.
+This is an alternative version of the tmpl_process.pl script,
+using standard gettext-style PO files. While there still might
+be changes made to the way it extracts strings, at this moment
+it should be stable enough for general use; it is already being
+used for the Chinese and Polish translations.
Currently, the create, update, and install actions have all been
reimplemented and seem to work.
+=head2 Features
+
+=over
+
+=item -
+
+Translation files in standard Uniforum PO format.
+All standard tools including all gettext tools,
+plus PO file editors like kbabel(1) etc.
+can be used.
+
+=item -
+
+Minor changes in whitespace in source templates
+do not generally require new strings to be re-translated.
+
+=item -
+
+Able to handle <TMPL_VAR> variables in the templates;
+<TMPL_VAR> variables are usually extracted in proper context.
+
+=item -
+
+Able to handle text input and radio button INPUT elements
+in the templates; these INPUT elements are also usually
+extracted in proper context.
+
+=item -
+
+Automatic comments in the generated PO files to provide
+even more context (line numbers, and the names and types
+of the variables).
+
+=item -
+
+Using the PO format also means translators can add their
+own comments in the translation files, if necessary.
+
+=item -
+
+Create, update, and install actions are all based on the
+same scanner module. This ensures that when installing the
+translation file, nothing that should not be translated is
+accidentally translated.
+
+=back
+
+=head1 NOTES
+
The create action calls xgettext.pl to do the actual work;
the update action calls xgettext.pl and msgmerge(1) to do the
actual work.
-The script can detect <TMPL_VAR> directives embedded inside what
-appears to be a full sentence (this actual work being done by
-TmplTokenizer(3)); these larger patterns appear in the translation
-file as c-format strings with %s.
-
-Whitespace in extracted strings are folded to single blanks, in
-order to prevent new strings from appearing when minor changes in
-the original templates occur, and to prevent overly difficult to
-read strings in the PO file.
-
=head1 BUGS
xgettext.pl must be present in the current directory; the
been written in TmplTokenizer and more is likely to be needed
(e.g., to get rid of the "Strange line" warning for #~).
+This script may not work in Windows.
+
There are probably some other bugs too, since this has not been
tested very much.
=head1 SEE ALSO
xgettext.pl,
+TmplTokenizer.pm,
msgmerge(1),
Locale::PO(3),
translator_doc.txt