summaryrefslogtreecommitdiff
path: root/util/lang.pl
blob: 12232e06ea3ac524c18a8eb6b176bfb2d6987db3 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/perl

use strict;
use warnings;

use Cwd 'abs_path';
our $ROOT;
BEGIN { ($ROOT = abs_path $0) =~ s{/util/lang\.pl$}{}; }

use lib $ROOT.'/lib';
use LangFile;

my $langtxt = "$ROOT/data/lang.txt";


sub usage {
  print <<__;
$0 stats
  Prints some stats.

$0 add <lang> [<file>]
  Adds new (empty) translation lines for language <lang> to <file> (defaults to
  the global lang.txt) for keys that don't have a TL line yet.

$0 only <lang>[,..] <outfile>
  Makes a copy of lang.txt to <outfile> and removes all translations except the
  ones of langauge <lang> (comma-seperated list of tags)

$0 merge <lang> <file>
  Merges <file> into lang.txt, copying over all the translations of <lang> in
  <file> while ignoring any other changes. Keys in <file> not present in
  lang.txt are silently ignored. Keys in lang.txt but not in <file> remain
  unaffected. Make sure each key in lang.txt already has a line for <lang>,
  otherwise do an 'add' first.

$0 reorder <lang1>,<lang2>,..
  Re-orders the translation lines in lang.txt using the specified order.

$0 cleanup
  Higher-level cleanup/canonicalization. Does a bunch of things:
  - Adds empty translation lines for languages present in the first key but not in other keys.
  - Removes translation lines for languages not present in the first key.
  - Reorders all translations to be in the same order as the first key.
  - Removes translations that are equivalent to the English text.
  TL;DR: Uses the first key in lang.txt as a template for all other keys.

$0 stage <lang>
  Puts all changes of <lang> into the git index, and leaves everything else untouched.
__
  exit;
}


sub stats {
  my $r = LangFile->new(read => $langtxt);
  my $keys = 0;
  my %lang;
  while(my $l = $r->read()) {
    $keys++ if $l->[0] eq 'key';
    if($l->[0] eq 'tl') {
      $lang{$l->[1]} ||= [0,0];
      $lang{$l->[1]}[0]++;
      $lang{$l->[1]}[1]++ if $l->[2];
    }
  }
  print  "lang  lines        sync         unsync\n";
  printf "%3s   %4d (%3d%%)  %4d (%3d%%)  %4d\n", $_,
    $lang{$_}[0], $lang{$_}[0]/$keys*100, $lang{$_}[1], $lang{$_}[1]/$keys*100, $keys-$lang{$_}[1]
    for keys %lang;
  printf "Total keys: %d\n", $keys;
}


sub add {
  my($lang, $file) = @_;
  $file ||= $langtxt;
  my $r = LangFile->new(read => $file);
  my $w = LangFile->new(write => "$file~");
  my $k = 0;
  while((my $l = $r->read())) {
    if($k && $l->[0] ne 'tl') {
      $k = 0;
      $w->write('tl', $lang, 0, '');
    }
    $k = 1 if $l->[0] eq 'key';
    $k = 0 if $l->[0] eq 'tl' && $l->[1] eq $lang;
    $w->write(@$l);
  }
  $r->close;
  $w->close;
  rename "$file~", $file or die $!;
}


sub only {
  my($lang, $out) = @_;
  my @lang = split /,/, $lang;
  my $r = LangFile->new(read => $langtxt);
  my $w = LangFile->new(write => $out);
  while((my $l = $r->read())) {
    $w->write(@$l) unless $l->[0] eq 'tl' && !grep $_ eq $l->[1], @lang;
  }
  $r->close;
  $w->close;
}


sub merge {
  my($lang, $file) = @_;

  # read all translations in $lang in $file
  my $trans = LangFile->new(read => $file);
  my($key, %trans);
  while((my $l = $trans->read)) {
    $key = $l->[1] if $l->[0] eq 'key';
    $trans{$key} = [ $l->[2], $l->[3] ] if $l->[0] eq 'tl' && $l->[1] eq $lang;
  }
  $trans->close;

  # now update lang.txt
  my $r = LangFile->new(read => $langtxt);
  my $w = LangFile->new(write => "$langtxt~");
  while((my $l = $r->read)) {
    $key = $l->[1] if $l->[0] eq 'key';
    ($l->[2], $l->[3]) = @{$trans{$key}} if $l->[0] eq 'tl' && $l->[1] eq $lang && $trans{$key};
    $w->write(@$l);
  }
  $r->close;
  $w->close;
  rename "$langtxt~", $langtxt or die $!;
}


sub reorder {
  my @lang = split /,/, shift;
  my $r = LangFile->new(read => $langtxt);
  my $w = LangFile->new(write => "$langtxt~");
  my($key, %tl);
  while((my $l = $r->read)) {
    if($key && $l->[0] ne 'tl') {
      $tl{$_} && $w->write(@{delete $tl{$_}}) for(@lang);
      $w->write(@{$tl{$_}}) for sort keys %tl;
      $key = undef;
      %tl = ();
    }
    $key = $l->[1] if $l->[0] eq 'key';
    $tl{$l->[1]} = $l if $l->[0] eq 'tl';
    $w->write(@$l) unless $l->[0] eq 'tl';
  }
  $r->close;
  $w->close;
  rename "$langtxt~", $langtxt or die $!;
}


sub cleanup {
  my $r = LangFile->new(read => $langtxt);
  my $w = LangFile->new(write => "$langtxt~");
  my $key;      # current key, undef if not in a TL part
  my %tl;       # translations of the current key
  my @template; # languages and order of the first key
  my $havetemp; # set when @template is complete
  while((my $l = $r->read)) {
    if($key && $l->[0] ne 'tl') {
      $havetemp = 1;
      for(@template) {
        if(!$tl{$_}) {
          $w->write('tl', $_, 0, '');
        } elsif($_ ne 'en' && $tl{$_}[3] eq $tl{en}[3]) {
          $w->write('tl', $_, 1, '');
        } else {
          $w->write(@{$tl{$_}});
        }
      }
      $key = undef;
      %tl = ();
    }
    $key = $l->[1] if $l->[0] eq 'key';
    $w->write(@$l) unless $l->[0] eq 'tl';
    if($l->[0] eq 'tl') {
      $tl{$l->[1]} = $l;
      push @template, $l->[1] if !$havetemp;
    }
  }
  $r->close;
  $w->close;
  rename "$langtxt~", $langtxt or die $!;
}


sub stage {
  my $lang = shift;
  chdir "$ROOT/data";
  rename 'lang.txt', '.lang.txt.tmp' or die $!;
  `git checkout lang.txt`;
  add $lang;
  merge $lang, '.lang.txt.tmp';
  `git add lang.txt`;
  rename '.lang.txt.tmp', 'lang.txt';
}


usage if !@ARGV;
my $act = shift;
stats if $act eq 'stats';
add @ARGV if $act eq 'add';
only @ARGV if $act eq 'only';
merge @ARGV if $act eq 'merge';
reorder @ARGV if $act eq 'reorder';
cleanup @ARGV if $act eq 'cleanup';
stage @ARGV if $act eq 'stage';