summaryrefslogtreecommitdiffstats
path: root/po/bin/po2txt
blob: f10ee5be9266d8731883b30f876c37b38114f9dc (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /usr/bin/perl

# convert *.po files to texts.* files suitable for gfxboot
# usage: po2txt lang.po >texts.lang
# Note: en.po ist treated specially!

use Getopt::Long;

sub read_texts;
sub join_msg;

$opt_product = "SUSE Linux";

GetOptions(
  'product=s' => \$opt_product
);


for $lang (@ARGV) {
  $lang = 'en' if $lang eq 'bootloader.pot';
  $lang =~ s/\.po$//;
  read_texts $lang;
}

sub read_texts
{
  local $_;

  my ($lang, @f, $txt, $context, $t, $p, $ids, $file);

  $lang = shift;

  $file = "$lang.po";
  $file = 'bootloader.pot' if $lang eq 'en';

  if($lang eq 'en') {
    $ids = 1;
  }

  open F, $file;
  @f = (<F>);
  close F;

  map { s/<product>/$opt_product/g; } @f;

  $_ = $lang;
  s/.*\///;

  for (@f) {
    if(/^\s*#\.\s*(txt_\S+)/) {
      if($txt) {
        @msgstr = @msgid if $ids || join_msg(\@msgstr) eq "";
        $txts{$txt} = join_msg(\@msgstr);
      }

      $txt = $1;

      undef @msgid;
      undef @msgstr;
      undef $context;
      next;
    }

    next if /^\s*#.*|^\s*$/;
    
    if(/^\s*msgid\s*(\".*\")\s*$/) {
      push @msgid, $1 unless $1 eq '""';
      $context = 1;
      next;
    }

    if(/^\s*msgstr\s*(\".*\")\s*$/) {
      push @msgstr, $1 unless $1 eq '""';
      $context = 2;
      next;
    }

    if(/^\s*(\".*\")\s*$/) {
      if($context == 1) {
        push @msgid, $1;
      }
      elsif($context == 2) {
        push @msgstr, $1;
      }
      else {
        die "format oops in ${lang}.po: $_"
      }
    }
  }

  if($txt) {
    @msgstr = @msgid if $ids || join_msg(\@msgstr) eq "";
    $txts{$txt} = join_msg(\@msgstr);
  }

  @txts = sort keys %txts;

  for (@txts) {
    $txt = $txts{$_};
    $txt =~ s/\\"/"/g;
    $txt =~ s/\\\\/\\/g;
    $txt =~ s/\\n/\n/g;
    print "$txt\x00"
  }

  if($ids) {
    open W, ">text.inc";
    print W "%\n% This file is generated automatically. Editing it is pointless.\n%\n\n";
    print W "/texts [\n";
    $p = pop @txts;
    for (@txts) { print W "  /$_\n" }
    print W "  /$p\n] def\n";
    close W;
  }
  else {
    open F, "text.inc";
    for (<F>) {
      if(/\s+\/(txt_\S+)/) {
        $txt_ref{$1} = undef;
      }
    }
    close F;
    for (@txts) {
      $txt_list{$_}++;
      $txt_multi{$_} = 1 if $txt_list{$_} > 1;
    }
    for (@txts) {
      $txt_unknown{$_} = 1 unless exists $txt_ref{$_};
    }
    for (keys %txt_ref) {
      $txt_miss{$_} = 1 unless exists $txt_list{$_};
    }

    if(defined(%txt_miss) || defined(%txt_unknown) || defined(%txt_multi)) {
      print STDERR "$lang:\n";
      for (sort keys %txt_miss) {
        print STDERR "  missing: $_\n"
      }
      for (sort keys %txt_unknown) {
        print STDERR "  unknown: $_\n"
      }
      for (sort keys %txt_multi) {
        print STDERR "  multi: $_\n"
      }
    }
  }

}


sub join_msg
{
  local $_;
  my ($s, $msg, $m);

  $msg = shift;

  for $s (@{$msg}) {
    $_ = $s;
    s/^\"(.*)\"$/$1/;
    $m .= $_;
  }

  return $m;
}