aboutsummaryrefslogtreecommitdiffstats
path: root/english/po/wmlxgettext.pl
blob: 318cfc3add92a38aab4a587906caf72be8a8ad8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /usr/bin/perl -w

use strict;
use Getopt::Std;

use vars qw($opt_p);

my $messages = {};
my @msgids = ();

my $domain = shift @ARGV;

sub escape {
        my $text = shift;
        $text =~ s/\\/\\\\/g;
        $text =~ s/"/\\"/g;
        $text =~ s/\n/\\n/g;
        $text =~ s/\t/\\t/g;
        $text =~ s/%%/%/g;
        return $text;
}

sub countNewline {
        my $text = shift;
        return ($text =~ s/\n//g);
}

sub processFile {
        my $file = shift;
        my $prefix = shift;
        my ($text, $lineno, $comment, $nextlineno, $msgid);
        my (@messages) = ();
        my (%msgids) = ();
        my $repl = '';

        open(IN, "< $file") || die "Unable to open $file\n";
        local ($/) = undef;
        $text = <IN>;
        close(IN);

        if ($prefix =~ s/=(.*)//) {
                $repl = $1;
        }
        $file =~ s{^$prefix}{$repl}o unless $prefix eq '__';
        #  Remove comments if they contain <gettext> or </gettext>
        $text =~ s/^[ \t]*#.*<\/?gettext\b//mg;
        $lineno = 1;
        while ($text =~ m{\G(.*?)(<gettext\b(?:\s+domain="([^"]+)")?[^>]*>)(.*?)</gettext>}gs) { # " -- to fix vim syntax hilighting :)
                $msgid = escape($4);
                $lineno += countNewline ($1.$2);
                $nextlineno = countNewline ($4);;
                my $dom = ($3) ? $3 : 'templates';
                if ($domain ne $dom) {
                   $lineno += $nextlineno;
                   next;  # wrong domain
                }
                $comment = '';
                if ($1 =~ m/(((^|\n)[ \t]*#.*)+)\n?[^\n]*$/) {
                        $comment = $1;
                        $comment =~ s/^\s+#\s*//;
                        $comment =~ s/\n[ \t]*#\s*/\n/g;
                }
                push (@msgids, $msgid);
                if (defined ($messages->{$msgid})) {
                        print STDERR "wmlxgettext: Warning: msgid multiple defined:\n\t".
                                $msgid."\n";
                } else {
                        $messages->{$msgid} = [];
                }
                push (@{$messages->{$msgid}}, $comment, $file, $lineno);
                $lineno += $nextlineno;
        }
}

$opt_p = '__';
getopt('p');

foreach (@ARGV) {
        processFile($_, $opt_p);
}

print "msgid \"\"\nmsgstr \"\"\n".
        "\"Content-Type: text/plain; charset=UTF-8\\n\"\n".
        "\"Content-Transfer-Encoding: 8bit\\n\"\n\n";

foreach my $msgid (@msgids) {
        next unless $messages->{$msgid};
        while (@{$messages->{$msgid}}) {
                $_ = shift(@{$messages->{$msgid}});
                s/\n/\n#. /g;
                print "#. ".$_."\n" if $_;
                print "#: ".shift(@{$messages->{$msgid}}).":".
                            shift(@{$messages->{$msgid}})."\n";
        }
        print "msgid \"$msgid\"\nmsgstr \"\"\n\n";
        undef $messages->{$msgid};
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy