From bd9e6617827818fd043452c08c606f07b78014a0 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdesdk@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- scripts/extractattr | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100755 scripts/extractattr (limited to 'scripts/extractattr') diff --git a/scripts/extractattr b/scripts/extractattr new file mode 100755 index 00000000..324b1f56 --- /dev/null +++ b/scripts/extractattr @@ -0,0 +1,158 @@ +#! /usr/bin/env perl + +# +# Copyright (c) 2004 Richard Evans +# +# License: LGPL 2.0 +# + +sub usage +{ + warn <<"EOF"; + +extractattr [flags] filenames + +This script extracts element attributes from designer (.ui) and XMLGIU (.rc) files +and writes on standard output (usually redirected to rc.cpp) the equivalent +i18n() calls so that xgettext can parse them. + +--attr=spec : Specify the attribute to be extracted. The specification + consists of the following comma separated arguments: + + Element,attribute[,context] + + The context is optional and overrides the name set by + --context below. Repeat the flag to specify multiple + attributes: + + --attr=Title,data --attr=Description,data,Stencils + +--context=name : Give i18n calls a context name: i18n("name", ...) +--lines : Include source line numbers in comments (deprecated, it is switched on by default now) +--help|? : Display this summary + +EOF + + exit; +} + +########################################################################################### + +use strict; +use warnings; +use Getopt::Long; + +########################################################################################### +# Add options here as necessary - perldoc Getopt::Long for details on GetOptions + +GetOptions ( "attr=s" => \my @opt_attr, + "context=s" => \my $opt_context, + "lines" => \my $opt_lines, + "help|?" => \&usage ); + +unless ( @ARGV ) +{ + warn "No filename specified"; + exit; +} + +unless ( @opt_attr ) +{ + warn "No attributes specified"; + exit; +} + +########################################################################################### +# Program start proper - NB $. is the current line number + +my $code =<<'EOF'; +our $file_name; + +for $file_name ( @ARGV ) +{ + my $fh; + + unless ( open $fh, "<", $file_name ) + { + warn "Failed to open: '$file_name': $!"; + next; + } + + while ( <$fh> ) + { + last if $. == 1 and $_ !~ /^(?:]*?| . + quotemeta($attribute) . qq|="([^"]+)"/ and write_i18n('| . $context . qq|', \$1);\n|; + } + + return "$code\n"; +} + +sub write_i18n +{ + my ($context, $text) = @_; + + our $file_name; + + unless ( $text ) + { + print "// Skipped empty message at $file_name line $.\n"; + return; + } + + $text =~ s/<//g; + $text =~ s/'/\'/g; + $text =~ s/"/\"/g; + $text =~ s/&/&/g; + + # Escape characters exactly like uic does it + # (As extractrc needs it, we follow the same rule to avoid to be different.) + $text =~ s/\\/\\\\/g; # escape \ + $text =~ s/\"/\\\"/g; # escape " + $text =~ s/\r//g; # remove CR (Carriage Return) + $text =~ s/\n/\\n\"\n\"/g; # escape LF (Line Feed). uic also change the code line at a LF, we do not do that. + + $context ||= $opt_context; + + print "//i18n: file $file_name line $.\n"; + print qq|i18n("|; + print qq|$context", "| if $context; + print qq|$text");\n|; +} + -- cgit v1.2.1