element-web/scripts/check-i18n.pl

166 lines
4.2 KiB
Perl
Raw Normal View History

2017-05-27 15:47:15 +00:00
#!/usr/bin/perl
use strict;
use warnings;
use Cwd 'abs_path';
# script which checks how out of sync the i18ns are drifting
# example i18n format:
# "%(oneUser)sleft": "%(oneUser)sleft",
$|=1;
$0 =~ /^(.*\/)/;
my $i18ndir = abs_path($1."/../src/i18n/strings");
2017-05-27 16:13:17 +00:00
my $srcdir = abs_path($1."/../src");
2017-05-27 15:47:15 +00:00
my $en = read_i18n($i18ndir."/en_EN.json");
2017-05-27 16:13:17 +00:00
my $src_strings = read_src_strings($srcdir);
my $src = {};
2017-05-27 16:13:17 +00:00
print "Checking strings in src\n";
2017-05-27 16:27:58 +00:00
foreach my $tuple (@$src_strings) {
my ($s, $file) = (@$tuple);
$src->{$s} = $file;
2017-05-27 16:13:17 +00:00
if (!$en->{$s}) {
if ($en->{$s . '.'}) {
2017-05-27 16:27:58 +00:00
printf ("%50s %24s\t%s\n", $file, "en_EN has fullstop!", $s);
2017-05-27 16:13:17 +00:00
}
else {
$s =~ /^(.*)\.?$/;
if ($en->{$1}) {
2017-05-27 16:27:58 +00:00
printf ("%50s %24s\t%s\n", $file, "en_EN lacks fullstop!", $s);
2017-05-27 16:13:17 +00:00
}
else {
2017-05-27 16:27:58 +00:00
printf ("%50s %24s\t%s\n", $file, "Translation missing!", $s);
2017-05-27 16:13:17 +00:00
}
}
}
}
print "\nChecking en_EN\n";
my $count = 0;
my $remaining_src = {};
foreach (keys %$src) { $remaining_src->{$_}++ };
foreach my $k (sort keys %$en) {
2017-05-27 17:38:37 +00:00
# crappy heuristic to ignore country codes for now...
next if ($k =~ /^(..|..-..)$/);
if (!$src->{$k}) {
if ($src->{$k. '.'}) {
printf ("%50s %24s\t%s\n", $src->{$k. '.'}, "src has fullstop!", $k);
}
else {
$k =~ /^(.*)\.?$/;
if ($src->{$1}) {
printf ("%50s %24s\t%s\n", $src->{$1}, "src lacks fullstop!", $k);
}
else {
printf ("%50s %24s\t%s\n", '???', "Not present in src?", $k);
}
}
}
else {
$count++;
delete $remaining_src->{$k};
}
}
printf ("$count/" . (scalar keys %$src) . " strings found in src are present in en_EN\n");
foreach (keys %$remaining_src) {
printf "remaining: $_\n";
}
2017-05-27 15:47:15 +00:00
opendir(DIR, $i18ndir) || die $!;
my @files = readdir(DIR);
closedir(DIR);
2017-05-27 17:41:43 +00:00
foreach my $lang (grep { -f "$i18ndir/$_" && !/(basefile|en_EN)\.json/ } @files) {
2017-05-27 15:47:15 +00:00
print "\nChecking $lang\n";
my $map = read_i18n($i18ndir."/".$lang);
my $count = 0;
my $remaining_en = {};
foreach (keys %$en) { $remaining_en->{$_}++ };
2017-05-27 15:47:15 +00:00
foreach my $k (sort keys %$map) {
if ($en->{$k}) {
if ($map->{$k} eq $k) {
printf ("%10s %24s\t%s\n", $lang, "Untranslated string?", $k);
2017-05-27 15:47:15 +00:00
}
$count++;
delete $remaining_en->{$k};
2017-05-27 15:47:15 +00:00
}
else {
if ($en->{$k . "."}) {
printf ("%10s %24s\t%s\n", $lang, "en_EN has fullstop!", $k);
2017-05-27 15:47:15 +00:00
next;
}
$k =~ /^(.*)\.?$/;
if ($en->{$1}) {
printf ("%10s %24s\t%s\n", $lang, "en_EN lacks fullstop!", $k);
2017-05-27 15:47:15 +00:00
next;
}
printf ("%10s %24s\t%s\n", $lang, "Not present in en_EN", $k);
2017-05-27 15:47:15 +00:00
}
}
if (scalar keys %$remaining_en < 100) {
foreach (keys %$remaining_en) {
printf ("%10s %24s\t%s\n", $lang, "Not yet translated", $_);
}
}
2017-05-27 15:47:15 +00:00
printf ("$count/" . (scalar keys %$en) . " strings translated\n");
}
sub read_i18n {
my $path = shift;
my $map = {};
open(FILE, "<", $path) || die $!;
while(<FILE>) {
if ($_ =~ m/^(\s+)"(.*?)"(: *)"(.*?)"(,?)$/) {
my ($indent, $src, $colon, $dst, $comma) = ($1, $2, $3, $4, $5);
2017-05-27 16:36:02 +00:00
$src =~ s/\\"/"/g;
$dst =~ s/\\"/"/g;
2017-05-27 15:47:15 +00:00
$map->{$src} = $dst;
}
}
close(FILE);
return $map;
2017-05-27 16:13:17 +00:00
}
sub read_src_strings {
my $path = shift;
use File::Find;
use File::Slurp;
my $strings = [];
my @files;
find( sub { push @files, $File::Find::name if (-f $_ && /\.jsx?$/) }, $path );
foreach my $file (@files) {
my $src = read_file($file);
$src =~ s/'\s*\+\s*'//g;
$src =~ s/"\s*\+\s*"//g;
2017-05-27 16:27:58 +00:00
$file =~ s/^.*\/src/src/;
2017-05-27 16:13:17 +00:00
while ($src =~ /_t\(\s*'(.*?[^\\])'/sg) {
my $s = $1;
$s =~ s/\\'/'/g;
2017-05-27 16:27:58 +00:00
push @$strings, [$s, $file];
2017-05-27 16:13:17 +00:00
}
while ($src =~ /_t\(\s*"(.*?[^\\])"/sg) {
2017-05-27 16:27:58 +00:00
push @$strings, [$1, $file];
2017-05-27 16:13:17 +00:00
}
}
return $strings;
2017-05-27 15:47:15 +00:00
}