5 use YAML::Syck qw(Dump LoadFile);
12 locale-diff - Compare two YAML files and print how their datastructures differ
16 # --keys is the default
18 diff --keys en.yml is.yml
20 # --untranslated-values compares prints keys whose values don't differ
21 diff --untranslated-values en.yml is.yml
23 # --untranslated-values-all compares prints keys whose values
24 # don't differ. Ignoring the blacklist which prunes things
25 # unlikley to be translated
26 diff --untranslated-values-all en.yml is.yml
28 # Check that interpolated variables ({{var}} and [[var]]) are the same
29 diff --validate-variables en.yml is.yml
33 This utility prints the differences between two YAML files using
34 L<Test::Differences>. The purpose of it is to diff the files is
35 F<config/locales> to find out what keys need to be added to the
36 translated files when F<en.yml> changes.
44 Print this help message.
48 Show the hash keys that differ between the two files, useful merging
49 new entries from F<en.yml> to a local file.
51 =item --untranslated-values
53 Show keys that B<exist in both the compared files> and whose values
54 are exactly the same. Use C<--keys> to a list of values that hasn't
57 The values are pruned according to global and language specific
58 blacklists found in the C<__DATA__> section of this script.
60 This helps to find untranslated values.
62 =item --untranslated-values-all
64 Like C<--untranslated-values> but ignores blacklists.
66 =item --validate-variables
68 Check that interpolated Ruby i18n variables (C<{{foo}}> and
69 C<[[foo]]>) are equivalent in the two provided files.
73 Dump a flat version of the translation hash in YAML format,
74 i.e. "foo.bar" instead of "{foo}->{bar}".
80 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
84 # Get the command-line options
85 Getopt::Long::Parser->new(
86 config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
88 'h|help' => \my $help,
90 'dump-flat' => \my $dump_flat,
91 'untranslated-values' => \my $untranslated_values,
92 'untranslated-values-all' => \my $untranslated_values_all,
93 'validate-variables' => \my $validate_variables,
94 'reconstruct' => \my $reconstruct,
97 # --keys is the default
98 $keys = 1 if not $untranslated_values_all and not $untranslated_values and not $validate_variables and not $dump_flat;
103 # If we're not given two .yml files
104 help() if (@ARGV != 2 or (!-f $ARGV[0] or !-f $ARGV[1])) and not $dump_flat || $reconstruct;
106 my ($from, $to) = @ARGV;
108 my $from_data = LoadFile($from);
109 my $from_parsed = { iterate($from_data->{basename($from)}) };
113 mark_utf8($from_parsed);
115 print Dump $from_parsed;
121 mark_utf8($from_parsed);
124 while (my ($k, $v) = each %$from_parsed) {
125 insert_string_deep(\%out, $k, $v);
128 print Dump { basename($from) => \%out };
133 my $to_data = LoadFile($to);
134 my $to_parsed = { iterate($to_data->{basename($to)}) };
138 print_key_differences($from_parsed, $to_parsed);
140 elsif ($untranslated_values or $untranslated_values_all)
142 my @untranslated = untranslated_keys($from_parsed, $to_parsed);
144 # Prune according to blacklist
145 if ($untranslated_values) {
146 @untranslated = prune_untranslated_with_blacklist(basename($to), @untranslated);
149 say for @untranslated;
150 } elsif ($validate_variables)
152 print_validate_variables($from_parsed, $to_parsed);
157 sub print_key_differences
161 # Hack around Test::Differences wanting a Test::* module loaded
163 sub Test::ok { print shift }
166 eq_or_diff([ sort keys %$f ], [ sort keys %$t ]);
169 sub untranslated_keys
171 my ($from_parsed, $to_parsed) = @_;
172 sort grep { exists $to_parsed->{$_} and $from_parsed->{$_} eq $to_parsed->{$_} } keys %$from_parsed;
175 sub prune_untranslated_with_blacklist
177 my ($language, @keys) = @_;
181 my $end_yaml = LoadFile(*DATA);
182 my $untranslated_values = $end_yaml->{untranslated_values};
183 my $default = $untranslated_values->{default};
184 my $this_language = $untranslated_values->{$language} || {};
186 my %bw_list = (%$default, %$this_language);
188 while (my ($key, $blacklisted) = each %bw_list)
190 # FIXME: Does syck actually support true/false booleans in yaml?
191 delete $keys{$key} if $blacklisted eq 'true'
197 sub print_validate_variables
201 while (my ($key, $val) = each %$f)
203 next if exists $f->{$key} and not exists $t->{$key};
205 my @from_var = parse_variables_from_string($f->{$key});
206 my @to_var = parse_variables_from_string($t->{$key});
208 unless (@from_var ~~ @to_var) {
209 say "$key in $from has (@from_var) and $to has (@to_var)";
215 sub parse_variables_from_string
219 # This probably matches most of the variables
220 my $var = qr/ [a-z0-9_]+? /xs;
222 if (my @var = $string =~ m/ \{\{ ($var) \}\} | \[\[ ($var) \]\] /gsx) {
223 return sort grep { defined } @var;
231 my ($hash, @path) = @_;
234 while (my ($k, $v) = each %$hash)
236 if (ref $v eq 'HASH')
238 push @ret => iterate($v, @path, $k);
242 push @ret => join(".",@path, $k), $v;
249 # $s = 'foo.bar.baz.spam.eggs.ham'; $h = \%h; $h = $h->{$_} = {} for split /\./, $s; \%h
250 # ==> {foo => {bar => {baz => {spam => {eggs => {ham => {}}}}}}}
251 sub insert_string_deep {
252 my ($h, $ks, $v) = @_;
253 my $p = \$h; $p = \$$p->{$_} for split /\./, $ks;
257 # sub insert_string_deep
259 # my ($hash, $key, $value) = @_;
261 # my @key = split /\./, $key;
270 # if (ref $h->{$k}) {
273 # $h = $h->{$k} = {};
282 $name =~ s[\..*?$][];
292 map { if (ref $_ eq 'ARRAY') { map { utf8::decode($_) } @$_ } else { utf8::decode($_) } } values %$hash;
299 Pod::Usage::pod2usage(
300 -verbose => $arg{ verbose },
301 -exitval => $arg{ exitval } || 0,
308 # Default/Per language blacklist/whitelist for the
309 # --untranslated-values switch. "true" as a value indicates that the
310 # key is to be blacklisted, and "false" that it's to be
311 # whitelisted. "false" is only required to whitelist a key
312 # blacklisted by default on a per-language basis.
316 layouts.intro_3_bytemark: true
317 layouts.intro_3_ucl: true
318 layouts.project_name.h1: true
319 layouts.project_name.title: true
320 site.index.license.project_url: true
321 browse.relation_member.entry: true
324 changeset.changeset.id: true
327 activerecord.attributes.message.sender: true
328 activerecord.attributes.trace.name: true
329 activerecord.models.changeset: true
330 activerecord.models.relation: true
331 browse.changeset.changeset: true
332 browse.changeset.changesetxml: true
333 browse.changeset.osmchangexml: true
334 browse.changeset.title: true
335 browse.common_details.version: true
336 browse.containing_relation.relation: true
337 browse.relation.relation: true
338 browse.relation.relation_title: true
339 browse.start_rjs.details: true
340 browse.start_rjs.object_list.details: true
341 browse.tag_details.tags: true
342 changeset.changesets.id: true
343 export.start.export_button: true
344 export.start.format: true
345 export.start.output: true
346 export.start.zoom: true
347 export.start_rjs.export: true
350 site.edit.anon_edits: true
351 site.index.license.license_name: true
352 site.index.permalink: true
353 site.key.table.entry.park: true
354 site.search.submit_text: true
355 trace.edit.tags: true
357 trace.trace_form.tags: true
358 trace.trace_optionals.tags: true
359 trace.view.tags: true
360 user.account.public editing.enabled link: true
364 site.edit.anon_edits: true
366 # Creative Commons Attribution-Share Alike 2.0
367 site.index.license.license_name: true
369 # http://creativecommons.org/licenses/by-sa/2.0/
370 site.index.license.license_url: true
373 printable_name.with_id: true
376 printable_name.with_name: true
379 geocoder.search_osm_namefinder.prefix: true
381 # {{suffix}}, {{parentname}}
382 geocoder.search_osm_namefinder.suffix_suburb: true