summaryrefslogtreecommitdiff
path: root/t/03_translations.t
blob: 679d6156fef3f280100082917cb1126849def71c (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
#!perl

## Run some sanity checks on the translations

use 5.006;
use strict;
use warnings;
use Data::Dumper;
BEGIN {
    use vars qw/$t %complete_langs/;
    %complete_langs = (
        'en' => 'English',
        'fr' => 'French',
        );
}
use Test::More;

if (!$ENV{RELEASE_TESTING}) {
    plan (skip_all =>  'Test skipped unless environment variable RELEASE_TESTING is set');
}
else {
    plan tests => 3 + (5 * ((scalar keys %complete_langs)-1));
}

my $file = 'check_postgres.pl';
my ($fh, $slurp);
if (!open $fh, '<', $file) {
    if (!open $fh, '<', "../$file") {
        die "Could not find $file!\n";
    }
}
{
    local $/;
    $slurp = <$fh>;
}
close $fh or warn qq{Could not close "$file": $!\n};

my ($lang,%msg,%call);
my ($start,$linecount) = (0,0);
for my $line (split /\n/ => $slurp) {
    $linecount++;
    if (!$start) {
        if ($line =~ /^our \%msg/) {
            $start = 1;
        }
        next;
    }

    while ($line =~ /msgn?\('([\w\-]+)'(.*?)\)/g) {
        my ($msg,$args,$orig) = ($1,$2,$2);
        $args =~ s/substr\(.+?,.+?,/substr\(foo bar/g;
        my $numargs = $args =~ y/,//d;
        push @{$call{$msg}}, { line => $linecount, numargs => $numargs, actual => $orig };
    }

    if ($line =~ /^'(\w+)' => \{/) {
        $lang = $1;
        $msg{$lang} = {};
        next;
    }

    if ($line =~ /^(\s*)'([\w\-]+)'\s+=> qq?\{(.+?)}[,.]/) {
        my ($space,$msg,$value) = (length $1 ? 1 : 0, $2, $3);
        $msg{$lang}{$msg} = [$space,$value];
        next;
    }
}

$t=q{All msg() function calls are mapped to an 'en' string};
my $ok = 1;
for my $call (sort keys %call) {
    if (!exists $msg{'en'}{$call}) {
        my $lines = join ',' => map { $_->{line} } @{$call{$call}};
        fail qq{Could not find message for "$call" (lines: $lines)};
        $ok = 0;
    }
}
$ok and pass $t;

$t=q{All msg() function calls are called with correct number of arguments};
$ok = 1;
for my $call (sort keys %call) {
    next if !exists $msg{'en'}{$call};
    my $msg = $msg{'en'}{$call}->[1];
    for my $l (@{$call{$call}}) {
        my $line = $l->{line};
        my $numargs = $l->{numargs};
        for my $x (1..$numargs) {
            if ($msg !~ /\$$x/) {
                fail sprintf q{Message '%s' called with %d %s as line %d, but no %s argument found in msg '%s'},
                    $call, $numargs, 1==$numargs ? 'argument' : 'arguments', $line, '$'.$x, $msg;
                $ok = 0;
            }
        }

        if (!$numargs and $msg =~ /\$\d/) {
            fail qq{Message '$call' called with no args at line $line, but requires some};
            $ok = 0;
        }
    }
}
$ok and pass $t;

my %ok2notuse = map { $_ => 1 }
    qw/time-week time-weeks time-month time-months time-year time-years
qtime-count-msg qtime-count-none qtime-for-msg qtime-msg qtime-none
txntime-count-msg txntime-count-none txntime-for-msg txntime-msg txntime-none
txnidle-count-msg txnidle-count-none txnidle-for-msg txnidle-msg txnidle-none
index language schema table user
/;

my %ok2nottrans;
for my $msg (qw/timesync-diff time-minute time-minutes maxtime version version-ok/) {
    $ok2nottrans{'fr'}{$msg} = 1;
}

$t=q{All 'en' message strings are used somewhere in the code};
$ok = 1;
for my $msg (sort keys %{$msg{'en'}}) {
    if (!exists $call{$msg}) {
        ## Known exceptions
        next if exists $ok2notuse{$msg};
        fail qq{Message '$msg' does not appear to be used in the code};
        $ok = 0;
    }
}
$ok and pass $t;

for my $l (sort keys %complete_langs) {
    my $language = $complete_langs{$l};
    next if $language eq 'English';

    $ok = 1;
    $t=qq{Language $language contains all valid message strings};
    for my $msg (sort keys %{$msg{'en'}}) {
        if (! exists $msg{$l}{$msg}) {
            fail qq{Message '$msg' does not appear in the $language translations};
            $ok = 0;
        }
    }
    $ok and pass $t;

    $ok = 1;
    $t=qq{Language $language contains no extra message strings};
    for my $msg (sort keys %{$msg{$l}}) {
        if (! exists $msg{'en'}{$msg}) {
            fail qq{Message '$msg' does not appear in the 'en' messages!};
            $ok = 0;
        }
    }
    $ok and pass $t;

    $ok = 1;
    $t=qq{Language $language messages have same number of args as 'en'};
    for my $msg (sort keys %{$msg{'en'}}) {
        next if ! exists $msg{$l}{$msg};
        my $val = $msg{'en'}{$msg}->[1];
        my $lval = $msg{$l}{$msg}->[1];
        my $x = 1;
        {
            last if $val !~ /\$$x/;
            if ($lval !~ /\$$x/) {
                fail qq{Message '$msg' is missing \$$x argument for language $language};
                $ok = 0;
            }
            $x++;
            redo;
        }
    }
    $ok and pass $t;

    $ok = 1;
    $t=qq{Language $language messages appears to not be translated, but is not marked as such};
    for my $msg (sort keys %{$msg{'en'}}) {
        next if ! exists $msg{$l}{$msg};
        next if exists $ok2nottrans{$l}{$msg};
        my $val = $msg{'en'}{$msg}->[1];
        my $lval = $msg{$l}{$msg}->[1];
        my $indent = $msg{$l}{$msg}->[0];
        next if $language eq 'French' and ($msg eq 'PID' or $msg eq 'port' or $msg eq 'pgbouncer-pool'
            or $msg eq 'index' or $msg eq 'table' or $msg eq 'transactions' or $msg eq 'mode');
        if ($val eq $lval and $indent) {
            fail qq{Message '$msg' in language $language appears to not be translated, but it not marked as such};
            $ok = 0;
        }
    }
    $ok and pass $t;

    $ok = 1;
    $t=qq{Language $language messages are marked as translated correctly};
    for my $msg (sort keys %{$msg{'en'}}) {
        next if ! exists $msg{$l}{$msg};
        my $val = $msg{'en'}{$msg}->[1];
        my $lval = $msg{$l}{$msg}->[1];
        my $indent = $msg{$l}{$msg}->[0];
        if ($val ne $lval and !$indent) {
            fail qq{Message '$msg' in language $language appears to not be translated, but it not marked as such};
            $ok = 0;
        }
    }
    $ok and pass $t;
}
exit;