X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/4e8f35012a0936d872c1b9a6a8858cf832736eec..9721a9a96e70e8ce0624d06107c35bcff1ecd1f0:/script/locale/diff diff --git a/script/locale/diff b/script/locale/diff old mode 100644 new mode 100755 index 1bb07d818..fdaf4a4fb --- a/script/locale/diff +++ b/script/locale/diff @@ -2,7 +2,6 @@ use strict; use warnings; use YAML::Syck qw(LoadFile); -use Data::Walk; use Test::Differences; use Pod::Usage (); use Getopt::Long (); @@ -31,6 +30,18 @@ translated files when F changes. Print this help message. +=item --diff-keys + +Show the hash keys that differ between the two files, useful merging +new entries from F to a local file. + +=item --untranslated-values + +Show keys whose values are either exactly the same between the two +files, or don't exist in the target file (the latter file specified). + +This helps to find untranslated values. + =back =head1 AUTHOR @@ -44,6 +55,8 @@ Getopt::Long::Parser->new( config => [ qw< bundling no_ignore_case no_require_order pass_through > ], )->getoptions( 'h|help' => \my $help, + 'diff-keys' => \my $diff_keys, + 'undiff-values' => \my $undiff_values, ) or help(); # On --help @@ -57,31 +70,64 @@ my ($from, $to) = @ARGV; my $from_data = LoadFile($from); my $to_data = LoadFile($to); -# Normalize the two to have the same root element -my ($from_key) = keys %$from_data; -$from_data = $from_data->{$from_key}; +my $from_parsed = { iterate($from_data->{basename($from)}) }; +my $to_parsed = { iterate($to_data->{basename($to)}) }; + +# Since this used to be the default, support that... +if ((not $undiff_values and not $diff_keys) or $diff_keys) +{ + print_key_differences(); +} +elsif ($undiff_values) +{ + my @untranslated = untranslated_keys($from_parsed, $to_parsed); -my ($to_key) = keys %$to_data; -$to_data = $to_data->{$to_key}; + print $_, "\n" for @untranslated; +} -# Delete hash values -walkdepth \&delete_hash_values, $_ for $from_data, $to_data; +exit 0; -# Hack around Test::Differences wanting a Test::* module loaded -$INC{"Test.pm"} = 1; -sub Test::ok { print shift } +sub print_key_differences +{ + # Hack around Test::Differences wanting a Test::* module loaded + $INC{"Test.pm"} = 1; + sub Test::ok { print shift } -# Diff the tree -eq_or_diff($from_data, $to_data); + # Diff the tree + eq_or_diff([ sort keys %$from_parsed ], [ sort keys %$to_parsed ]); +} -sub delete_hash_values +sub untranslated_keys { - return unless defined $Data::Walk::type and $Data::Walk::type eq 'HASH'; + my ($from_parsed, $to_parsed) = @_; + sort grep { not exists $to_parsed->{$_} or $from_parsed->{$_} eq $to_parsed->{$_} } keys %$from_parsed; +} - # We totally need Perl 6's $OUTER::_ to make this prettier - my $hash = $_; +sub iterate +{ + my ($hash, @path) = @_; + my @ret; + + while (my ($k, $v) = each %$hash) + { + if (ref $v eq 'HASH') + { + push @ret => iterate($v, @path, $k); + } + else + { + push @ret => join(".",@path, $k), $v; + } + } + + return @ret; +} - @$hash{grep { not ref $hash->{$_} } keys %$hash} = (); +sub basename +{ + my $name = shift; + $name =~ s[\..*?$][]; + $name; } sub help