]> git.openstreetmap.org Git - rails.git/blobdiff - script/locale/diff
Add --untranslated-values to spot things that haven't been translated
[rails.git] / script / locale / diff
index 1bb07d8189905ce00290a294376a32df67fa7b50..fdaf4a4fbea7bb1c1986915c7d6b5b9dada95264 100755 (executable)
@@ -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<en.yml> changes.
 
 Print this help message.
 
+=item --diff-keys
+
+Show the hash keys that differ between the two files, useful merging
+new entries from F<en.yml> 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