[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Scripts & i18n



 Bash scripts can now easily internationalized with gettext.

 Here's how:

 Indicate the package's name when the scripts starts by adding a...
TEXTDOMAIN=package_name

 Mark any internationalizable string this way:

before: echo  "Hello"
after : echo $"Hello"

 That tells bash (2.0) to look for those strings in the corresponding file
(/usr/share/aa/LC_MESSAGES/package.mo).

 xgettext won't extract i18n messages from scripts. Bash has an included
feature to do so, but I find it unusable. So I'm enclosing a script  (with a
little of Perl) to extract messages from scripts.

 Then you may use the other gettext utilites, as msgmerge and msgfmt.

 If you want to see a working example, download the new `bug' source
archive.

 Bye...! Let's internationalize Debian!
/usr/bin/perl -ne '
if (/\$(".*[^\\]")/)
{
	$_ = $1;
	s/\\/\\\\/g;
	print $_, ";;;", $ARGV, ":" , $. , "\n" ;
} elsif (/\$\"(.*)$/)
	{
		$_ = $1;
		s/\\/\\\\/g;
		$line=$.;
		$msg="\"" . $_ . "\\n";
		$_ = <>;
		chomp;
		s/\\/\\\\/g;
		while(!/[^\\]"/)
		{
			s/"/\\"/g;
			$msg .= $_ . "\\n";
			$_ = <>;
			chomp;
			s/\\/\\\\/g;
		}
		s/^(.*[^\\])".*$/$1/;
		s/"/\\"/g;
		$msg.=$_ . "\"";
		print $msg, ";;;", $ARGV, ":" , $line , "\n" ;
	}
close(ARGV) if (eof);
' $@ | sort |
perl -ne '
BEGIN {
	$saved_msgid="";
	$saved_pos="";
}

chomp;
($msgid,$pos)=split /\;\;\;/;

if ( $msgid eq $saved_msgid )
{
	$saved_pos = $saved_pos . " " . $pos;
} else
{
	if ($saved_msgid ne "")
	{
		print "#: " , $saved_pos , "\n";
		print "msgid ", $saved_msgid , "\n";
		print "msgstr \"\"\n\n";
	}
	$saved_pos = $pos;
	$saved_msgid = $msgid;
	$saved_msgid =~ s/([^\\])\\n/$1\\n"\n"/g;
}

END {
	if ($saved_msgid ne "")
	{
		print "#: ", $saved_pos, "\n";
		print "msgid ", $saved_msgid, "\n";
		print "msgstr \"\"\n\n";
	}
}
'

Reply to: