Skip to content

Commit

Permalink
Check arguments of translation strings
Browse files Browse the repository at this point in the history
Make sure the arguments of placeholders agree

Signed-off-by: Robert C. Helling <[email protected]>
  • Loading branch information
atdotde authored and dirkhh committed Oct 29, 2017
1 parent 15028fd commit 43b1a73
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions scripts/checktranslation.pl
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
#!/usr/bin/perl -CS

# Run this script to check if translations messed up format string arguments
#
# Usage scripts/checktranslations.pl translations/*ts

use strict;
use utf8;
use utf8::all;
use XML::TreeBuilder;

foreach my $file_name (@ARGV) {
my $tree = XML::TreeBuilder->new({'NoExpand' => 0, 'ErrorContext' => 0});
$tree->parse_file($file_name, ProtocolEncoding => 'UTF-8');
foreach my $string ($tree->find_by_tag_name('message')) {
my $source = $string->find_by_tag_name('source')->as_text;
my $translation = $string->find_by_tag_name('translation')->as_text;
next unless $translation =~ /\S/;
my @source_args = ($source =~ /\%([^\s\-\(\)])/g);
my @translation_args = ($translation =~ /\%([^\s\-\(\)])/g);
if (scalar(@source_args) != scalar(@translation_args)) {
print "$file_name:\n$source\n->\n$translation\n\n";
my $translation = $string->find_by_tag_name('translation');
if ($translation->find_by_tag_name('numerusform')) {
my @all = $translation->find_by_tag_name('numerusform');
foreach my $transstring(@all) {
&compare($file_name, $source, $transstring->as_text);
}
} else {
&compare($file_name, $source, $translation->as_text);
}
}
}

sub compare {
my ($file_name, $src, $trans) = @_;

# print "Checking $src\n";
return unless $trans =~ /\S/;
my @source_args = sort {$a cmp $b} ($src =~ /\%([^\s\-\(\)\,\.\:])/g);
my @translation_args = sort {$a cmp $b} ($trans =~ /\%([^\s\-\(\)\,\.\:])/g);
# print join("|", @translation_args), " vs ", join("|", @source_args),"\n";
unless (join('', @source_args) eq join('', @translation_args)) {
print "$file_name:\n$src\n->\n$trans\n\n";
}
}

0 comments on commit 43b1a73

Please sign in to comment.