[pacman-dev] [PATCH] makepkg-template: Add gettext support

Florian Pritz bluewind at xinu.at
Sun May 12 10:56:20 EDT 2013


This also slighty changes the word order in the description for
--newest.

Signed-off-by: Florian Pritz <bluewind at xinu.at>
---
 scripts/makepkg-template.pl.in | 74 +++++++++++++++++++++++++++---------------
 1 file changed, 48 insertions(+), 26 deletions(-)

diff --git a/scripts/makepkg-template.pl.in b/scripts/makepkg-template.pl.in
index 1ba007b..10035a6 100755
--- a/scripts/makepkg-template.pl.in
+++ b/scripts/makepkg-template.pl.in
@@ -5,7 +5,8 @@ use v5.10.1;
 use Cwd qw(abs_path);
 use File::Spec;
 use Getopt::Long;
-use Pod::Usage;
+use Module::Load;
+use Module::Load::Conditional qw(can_load);
 
 my %opts = (
 	input => '@BUILDSCRIPT@',
@@ -15,9 +16,27 @@ my %opts = (
 my $template_name_charset = qr/[[:alnum:]+_. at -]/;
 my $template_marker = qr/# template/;
 
+# runtime loading to avoid dependency on cpan since this is the only non-core module
+my $have_gettext = can_load(modules => {'Locale::gettext' => undef});
+if ($have_gettext) {
+	load 'Locale::gettext', qw(gettext);
+	Locale::gettext::bindtextdomain("pacman-scripts", '@localedir@');
+	Locale::gettext::textdomain("pacman-scripts");
+}
+
+sub _ {
+	my ($string) = @_;
+
+	if ($have_gettext) {
+		return gettext($string);
+	} else {
+		return $string;
+	}
+}
+
 sub burp {
 	my ($file_name, @lines) = @_;
-	open (my $fh, ">", $file_name) || die "can't create $file_name $!" ;
+	open (my $fh, ">", $file_name) || die sprintf(_("can't create '%s': %s"), $file_name, $!);
 	print $fh @lines;
 	close $fh;
 }
@@ -34,20 +53,22 @@ sub parse_template_line {
 
 	foreach my $element (@elements) {
 		my ($key, $val) = ($element =~ /^([a-z0-9]+)=(.*)$/);
-		die "invalid key/value pair $filename:$linenumber: $line"
-		    unless $key and $val;
+		unless ($key and $val) {
+			die _("invalid key/value pair\n%s:%s: %s"),
+				"$filename:$linenumber: $line";
+		}
 		$values{$key} = $val;
 	}
 
 	# end doesn't take arguments
 	if ($values{command} ne "end") {
 		if (!$values{name}) {
-			die "invalid template line: can't find template name\n",
+			die _("invalid template line: can't find template name\n"),
 				"$filename:$linenumber: $line";
 		}
 
 		unless ($values{name} =~ /^$template_name_charset+$/) {
-			die "invalid chars used in name '$values{name}'. allowed: [:alnum:]+_. at -\n",
+			die sprintf(_("invalid chars used in name '%s'. allowed: [:alnum:]+_. at -\n"), $values{name}),
 				"$filename:$linenumber: $line";
 		}
 	}
@@ -72,7 +93,7 @@ sub load_template {
 	my ($version) = (abs_path($path) =~ /-([0-9.]+)[.]template$/);
 
 	if (!$version) {
-		die "Couldn't detect version for template '$values->{name}'";
+		die sprintf(_("Couldn't detect version for template '%s'"), $values->{name});
 	}
 
 	my $parsed = process_file($path);
@@ -91,7 +112,7 @@ sub process_file {
 	my $nesting_level = 0;
 	my $linenumber = 0;
 
-	open (my $fh, "<", $filename) or die "failed to open '$filename': $!";
+	open (my $fh, "<", $filename) or die sprintf(_("failed to open '%s': %s"), $filename, $!);
 	my @lines = <$fh>;
 	close $fh;
 
@@ -113,7 +134,7 @@ sub process_file {
 				}
 
 				default {
-					die "Unknown template marker '$values->{command}'\n",
+					die sprintf(_("Unknown template marker '%s'\n"), $values->{command}),
 						"$filename:$linenumber: $line";
 				}
 			}
@@ -134,15 +155,30 @@ sub process_file {
 	return $ret;
 }
 
+sub usage {
+	my ($exitstatus) = @_;
+	print  "makepkg-template [options]\n";
+	print  "\n";
+	print  _("Options:\n");
+	printf(_("  --input, -p <file>    Build script to read (default: %s)\n"), '@BUILDSCRIPT@');
+	print  _("  --output, -o <file>   file to output to (default: input file)\n");
+	print  _("  --newest, -n          update templates to newest version\n");
+	print  _("                        (default: use version specified in the template markers)\n");
+	print  _("  --template-dir <dir>  directory to search for templates\n");
+	printf(_("                        (default: %s)\n"), '@TEMPLATE_DIR@');
+	print  "\n";
+	exit($exitstatus);
+}
+
 Getopt::Long::Configure ("bundling");
 GetOptions(
-	"help" => sub {pod2usage(-exitval => 0, -verbose => 1); },
-	"h" => sub {pod2usage(-exitval => 0, -verbose => 0); },
+	"help" => sub {usage(0); },
+	"h" => sub {usage(0); },
 	"input|p=s" => \$opts{input},
 	"output|o=s" => \$opts{output},
 	"newest|n" => \$opts{newest},
 	"template-dir=s" => \$opts{template_dir},
-) or pod2usage(1);
+) or usage(1);
 
 $opts{output} = $opts{input} unless $opts{output};
 
@@ -151,18 +187,4 @@ $opts{output} = "/dev/stdout" if $opts{output} eq "-";
 
 burp($opts{output}, process_file($opts{input}));
 
-__END__
-=head1 SYNOPSIS
-
-makepkg-template [options]
-
- Options:
-   --input, -p <file>     Build script to read (default: @BUILDSCRIPT@)
-   --output, -o <file>    file to output to (default: input file)
-   --newest, -n           update templates to newest version
-                          (default: use specified version in the template markers)
-   --template-dir <dir>   directory to search for templates
-                          (default: @TEMPLATE_DIR@)
-
-=cut
 # vim: set noet:
-- 
1.8.2.3


More information about the pacman-dev mailing list