1 #!@XML_I18N_TOOLS_PERL@ -w
2 # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4 -*-
5 # The XML Translation Extractor
7 # Copyright (C) 2000 Free Software Foundation.
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; either version 2 of the
12 # License, or (at your option) any later version.
14 # This script is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this library; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # Authors: Kenneth Christiansen <kenneth@gnu.org>
24 # Darin Adler <darin@eazel.com>
27 ## Release information
28 my $PROGRAM = "xml-i18n-extract";
29 my $PACKAGE = "xml-i18n-tools";
32 ## Script options - Enable by setting value to 1
33 my $ENABLE_INI = "1"; ## desktop and alike files
34 my $ENABLE_KEYS = "1"; ## mimetype descriptions
35 my $ENABLE_GLADE = "1"; ## glade files
36 my $ENABLE_XML = "1"; ## generic xml files
43 ## Scalars used by the option stuff
47 my $VERSION_ARG = "0";
54 my $gettext_type = "";
63 "type=s" => \$TYPE_ARG,
64 "local|l" => \$LOCAL_ARG,
65 "help|h|?" => \$HELP_ARG,
66 "version|v" => \$VERSION_ARG,
67 "update" => \$UPDATE_ARG,
68 "quiet|q" => \$QUIET_ARG,
75 ## This section will check for the different options.
77 sub split_on_argument {
85 } elsif ($LOCAL_ARG) {
89 } elsif ($UPDATE_ARG) {
106 $OUTFILE = "$FILE.h";
110 $OUTFILE = fileparse($FILE, ());
112 system("mkdir tmp/");
114 $OUTFILE = "./tmp/$OUTFILE.h"
118 if ($TYPE_ARG =~ /^gettext\/(.*)/) {
123 ## Sub for printing release information
125 print "${PROGRAM} (${PACKAGE}) $VERSION\n";
126 print "Copyright (C) 2000 Free Software Foundation, Inc.\n";
127 print "Written by Kenneth Christiansen, 2000.\n\n";
128 print "This is free software; see the source for copying conditions. There is NO\n";
129 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
133 ## Sub for printing usage information
135 print "Usage: ${PROGRAM} [FILENAME] [OPTIONS] ...\n";
136 print "Generates a header file from an xml source file.\n\nGrabs all strings ";
137 print "between <_translatable_node> and it's end tag,\nwhere tag are all allowed ";
138 print "xml tags. Read the docs for more info.\n\n";
139 print " -v, --version shows the version\n";
140 print " -h, --help shows this help page\n";
141 print " -q, --quiet quiet mode\n";
142 print "\nReport bugs to <kenneth\@gnu.org>.\n";
146 ## Sub for printing error messages
148 # print "xml-i18n-extract: invalid option @ARGV\n";
149 print "Try `${PROGRAM} --help' for more information.\n";
154 print "Generating C format header file for translation.\n";
162 open OUT, ">$OUTFILE";
166 print "Wrote $OUTFILE\n" unless $QUIET_ARG;
174 local $/; #slurp mode
175 open (IN, "<$FILE") || die "can't open $FILE: $!";
189 ### For generic translatable desktop files ###
191 if ($gettext_type eq "ini"){
193 while ($input =~ /^_.*=(.*)$/mg) {
204 ### For generic translatable mime/keys files ###
206 if ($gettext_type eq "keys"){
207 while ($input =~ /^\s*_\w+=(.*)$/mg) {
218 ### For generic translatable XML files ###
220 if ($gettext_type eq "xml"){
222 while ($input =~ /[\t\n\s]_\w+=\"([^\"]+)\"/sg) {
226 while ($input =~ /<_(\w+)>([^<]+)<\/_\1>/sg) {
238 ### For translatable Glade XML files ###
240 if ($gettext_type eq "glade"){
242 my $translate = "label|title|text|format|copyright|comments|
243 preview_text|tooltip";
245 while ($input =~ /<($translate)>([^<]+)<\/($translate)>/sg) {
247 # Glade has some bugs, especially it uses translations tags to contain little
248 # non-translatable content. We work around this, by not including these
249 # strings that only includes something like: label4, and window1
250 if ($2 !~ /^(window|label)[0-9]+$/) {
255 while ($input =~ /<items>(..[^<]*)<\/items>/sg) {
256 my @items = split (/\n/, $1);
257 for (my $n = 0; $n < @items; $n++) {
258 $messages{$items[$n]} = [];
269 foreach my $message (sort keys %messages) {
271 my ($tag) = @{ $messages{$message} };
273 # Replace XML entities for some special characters with
274 # the appropriate gettext syntax for those characters.
275 $message =~ s/"/\\"/mg; # "
276 $message =~ s/</</mg;
277 $message =~ s/>/>/mg;
278 $message =~ s/&/&/mg;
280 print OUT "/* xgettext:no-c-format */\n" if $message =~ /%/;
281 print OUT "/* $tag */\n" if $tag;
283 my @lines = split (/\n/, $message);
285 for (my $n = 0; $n < @lines; $n++) {
288 print OUT "char *s = N_(\"";
293 print OUT $lines[$n];
295 if ($n < @lines - 1) {