SubwayTooter-Android-App/checkMissingTranslation.pl

161 lines
3.8 KiB
Perl
Raw Normal View History

2018-08-31 15:51:40 +02:00
#!/usr/bin/perl --
use XML::Parser;
use strict;
use warnings;
use File::Find;
use XML::Simple;
use Data::Dump qw(dump);
2020-12-07 14:37:22 +01:00
use utf8;
2020-12-07 14:37:22 +01:00
binmode $_ for \*STDOUT,\*STDERR;
2019-09-29 19:40:12 +02:00
sub cmd($){
print "+ ",$_[0],"\n";
my $rv=system $_[0];
if ($? == -1) {
die "failed to execute: $!\n";
}elsif ($? & 127) {
die "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
}else {
my $rv = $? >> 8;
$rv and die "child exited with value $rv\n";
}
}
# ワーキングツリーに変更がないことを確認
open(my $fh,"-|","git status --porcelain --branch")
or die "can't check git status. $!";
my @untrackedFiles;
while(<$fh>){
2020-06-24 20:50:07 +02:00
s/[\x0d\x0a]+//;
2019-09-29 19:40:12 +02:00
if(/^\?\?\s*(\S+)/){
my $path =$1;
next if $path =~ /\.idea|_Emoji/;
push @untrackedFiles,$path
}elsif( /^##\s*(\S+?)(?:\.\.|$)/ ){
my $branch=$1;
print "# branch=$branch\n";
2020-06-24 20:50:07 +02:00
$branch eq 'trunk'
or warn "!!!! current branch is not trunk !!!!\n";
2019-09-29 19:40:12 +02:00
# }else{
# warn "working tree is not clean.\n";
# cmd "git status";
# exit 1;
}
}
close($fh)
or die "can't check git status. $!";
@untrackedFiles and die "forgot git add?\n",map{ "- $_\n"} @untrackedFiles;
my $xml = XML::Simple->new;
2018-08-31 15:51:40 +02:00
2020-06-24 20:50:07 +02:00
my $default_name = "_default";
2018-08-31 15:54:12 +02:00
2018-08-31 15:51:40 +02:00
my @files;
find(sub{
return if not -f $_;
($_ eq "strings.xml") and push @files,$File::Find::name;
},"app/src/main/res/");
@files or die "missing string files.\n";
my %langs;
for my $file(@files){
my $lang;
if( $file =~ m|values-([^/]+)| ){
$lang = $1;
}else{
2020-06-24 20:50:07 +02:00
$lang=$default_name;
2018-08-31 15:51:40 +02:00
}
my $data = $xml->XMLin($file);
2018-09-09 02:11:31 +02:00
if( not $data->{string} or ($data->{string}{content} and not ref $data->{string}{content} )){
2019-12-10 22:41:16 +01:00
warn "?? please make at least 2 string entries in $file\n";
next;
2018-09-09 02:11:31 +02:00
}
2018-08-31 15:51:40 +02:00
my %names;
while(my($name,$o)=each %{$data->{string}}){
2018-09-27 17:34:23 +02:00
if(not $o->{content}){
2018-09-27 17:26:01 +02:00
warn "$lang : $name : missing content in ",dump($o),"\n";
2018-09-27 17:34:23 +02:00
}else{
$names{$name}= $o->{content};
2018-09-27 17:26:01 +02:00
}
}
2018-08-31 15:51:40 +02:00
$langs{ $lang } = \%names;
}
2018-09-01 03:11:29 +02:00
my $hasError = 0;
2020-06-24 20:50:07 +02:00
my $master = $langs{ $default_name };
2018-08-31 15:51:40 +02:00
$master or die "missing master languages.\n";
my %params;
while(my($name,$value)=each %$master){
2018-09-01 03:11:29 +02:00
my @params = $value =~ /(%\d+\$[\d\.]*[sdxf])/g;
$params{$name} = join ',', sort @params;
}
2018-08-31 15:51:40 +02:00
my %missing;
my %allNames;
2018-08-31 15:54:12 +02:00
for my $lang ( sort keys %langs ){
my $names = $langs{$lang};
2018-08-31 15:51:40 +02:00
while(my($name,$value)=each %$names){
2018-09-27 17:26:01 +02:00
2018-08-31 15:51:40 +02:00
$allNames{$name}=1;
if(not $master->{$name} ){
$missing{$name} =1;
}
2018-09-01 03:11:29 +02:00
my @params = $value =~ /(%\d+\$[\d\.]*[sdxf])/g;
my $params = join ',', sort @params;
my $master_params = $params{$name} // '';
if( $params ne $master_params){
2018-09-01 03:11:29 +02:00
$hasError =1;
print "!! ($lang)$name : parameter mismatch. master=$master_params, found=$params\n";
}
2018-09-01 03:11:29 +02:00
# 残りの部分に%が登場したらエラー
my $sv = $value;
2020-12-07 14:37:22 +01:00
$sv =~ s/(%\d+\$[\d\.]*[sdxf])//g;
# Unit:%. を除外したい
$sv =~ s/%[\s.。]//g;
if( $sv =~ /%/ ){
2018-09-01 03:11:29 +02:00
$hasError =1;
print "!! ($lang)$name : broken param: $sv // $value\n";
}
# エスケープされていないシングルクォートがあればエラー
if( $value =~ m/(?<!\\)['"]/ ){
print "!! ($lang)$name : containg single or double quote without escape.\n";
}
2018-08-31 15:51:40 +02:00
}
my $nameCount = 0+ keys %$names;
print "($lang)string resource count=$nameCount\n";
}
my @missing = sort keys %missing;
@missing and die "missing string resources in master language: ",join(', ',@missing),"\n";
my $nameCount = 0+ keys %allNames;
print "(total)string resource count=$nameCount\n";
2018-09-01 03:11:29 +02:00
$hasError and die "please fix error(s).\n";
# Weblateの未マージのブランチがあるか調べる
system qq(git fetch weblate -q);
2020-08-02 06:56:38 +02:00
my @list = `git branch -r --no-merged`;
for(@list){
2020-06-24 20:50:07 +02:00
s/[\x0d\x0a]+//;
print "# Unmerged branch: $_\n";
}
2018-08-31 15:51:40 +02:00
exit 0;