InfoBoxes/Wind: use WindSettingsPanel
[xcsoar.git] / tools / lk2po.pl
blob9c9d0a4926ce1e5d282ae361128184454323b848
1 #!/usr/bin/perl
3 # Import translations from LK8000.
6 use strict;
7 require Locale::PO;
9 use vars qw($lk_language);
11 # XXX hard-coded path, sorry!
12 $lk_language = '/usr/src/git/LK8000/Common/Data/Language';
14 # hard-coded list of LK8000 languages
15 my %languages = (
16 GER => 'de',
17 FRA => 'fr',
18 GRE => 'el',
19 CZE => 'cs',
20 ESP => 'es',
21 HRV => 'hr',
22 HUN => 'hu',
23 ITA => 'it',
24 NED => 'nl',
25 POL => 'pl',
26 PTB => 'pt_BR',
27 POR => 'pt',
28 RUS => 'ru',
29 SRB => 'sr',
30 SWE => 'sv',
31 VIE => 'vn',
34 sub fold_english($) {
35 my $value = shift;
36 return lc $value;
39 sub load_msg($$) {
40 my $encoding = shift;
41 my $path = shift;
42 print STDERR "loading $path\n";
43 my @messages;
44 local *FILE;
45 open FILE, "<:encoding($encoding)", $path or die $!;
46 while (<FILE>) {
47 $messages[$1] = $2
48 if /^_\@M(\d+)_\s+"(.*)"/;
50 close FILE;
51 return @messages;
54 mkdir 'output/po';
56 # load the English message file
57 my @english = load_msg('UTF-8', "$lk_language/ENG_MSG.TXT");
59 # now iterate over all languages of LK8000
60 foreach my $lng (keys %languages) {
61 my $iso = $languages{$lng};
63 # read the LK8000 translation file
64 my @translated = load_msg('UTF-8',
65 "$lk_language/Translations/${lng}_MSG.TXT");
66 my %table;
67 for (my $i = 0; $i <= $#translated; $i++) {
68 my $english = $english[$i];
69 next unless defined $english;
70 my $translated = $translated[$i];
71 next unless defined $translated;
72 next unless length $translated;
73 next if $english eq $translated;
75 $english = fold_english($english);
77 # store in table
78 $table{$english} = $translated;
80 # see if we can merge it with the prefix number stripped
81 if ($english =~ /^(\d+)\s+(.*)$/s) {
82 my ($e_number, $english2) = ($1, $2);
83 if ($translated =~ /^(\d+)\s+(.*)$/s) {
84 my ($t_number, $translated2) = ($1, $2);
85 $table{$english2} = $translated2 if $e_number == $t_number;
89 # now strip trailing whitespace
90 if ($english =~ /^(.*?)\s+$/s) {
91 my $english2 = $1;
92 my $translated2 = $translated;
93 $translated2 =~ s,\s+$,,;
94 $table{$english2} = $translated2;
98 my $in = Locale::PO->load_file_asarray("po/${iso}.po");
99 $in = Locale::PO->load_file_asarray("po/xcsoar.pot")
100 unless defined $in;
102 my @out;
103 foreach my $x (@$in) {
104 # see if the translation is useful and needed
105 next if $x->obsolete;
106 my $msgid = eval $x->msgid;
107 next unless length $msgid;
108 my $msgstr = eval $x->msgstr;
109 next if length $msgstr and not $x->fuzzy;
110 next unless exists $table{fold_english $msgid};
112 my $z = $table{fold_english $msgid};
114 # escape special characters
115 $z =~ s/\n/\\n/g;
116 $z =~ s/\r/\\r/g;
118 push @out, new Locale::PO(-msgid => $msgid,
119 -msgstr => $z);
122 # anything new?
123 next unless @out;
125 # declare the UTF-8 charset
126 push @out, new Locale::PO(-msgid => '',
127 -msgstr => "Content-Type: text/plain; charset=UTF-8\\nContent-Transfer-Encoding: 8bit\\n");
129 # write temporary file with new translations
130 local *FILE;
131 open FILE, ">:utf8", "output/po/lk8000-${iso}.po" or die $!;
132 foreach my $x (@out) {
133 print FILE $x->dump;
135 close FILE or die $!;
137 # generate PO file if it doesn't exist already
138 unless (-f "po/${iso}.po") {
139 system("msginit --locale=$iso --no-translator --input=po/xcsoar.pot --output=po/${iso}.po") == 0
140 or die;
143 # merge the two
144 system("msgcat --use-first po/${iso}.po output/po/lk8000-${iso}.po --output-file=po/${iso}.po") == 0
145 or die;