5 use YAML::Syck qw(LoadFile);
9 use File::Basename qw(fileparse);
13 locale-diff - Compare two YAML files and print how their datastructures differ
17 # --keys is the default
19 diff --keys en.yml is.yml
21 # --untranslated-values compares prints keys whose values don't differ
22 diff --untranslated-values en.yml is.yml
24 # --untranslated-values-all compares prints keys whose values
25 # don't differ. Ignoring the blacklist which prunes things
26 # unlikley to be translated
27 diff --untranslated-values-all en.yml is.yml
29 # Check that interpolated variables ({{var}} and [[var]]) are the same
30 diff --validate-variables en.yml is.yml
34 This utility prints the differences between two YAML files using
35 L<Test::Differences>. The purpose of it is to diff the files is
36 F<config/locales> to find out what keys need to be added to the
37 translated files when F<en.yml> changes.
45 Print this help message.
49 Show the hash keys that differ between the two files, useful merging
50 new entries from F<en.yml> to a local file.
52 =item --untranslated-values
54 Show keys that B<exist in both the compared files> and whose values
55 are exactly the same. Use C<--keys> to a list of values that hasn't
58 The values are pruned according to global and language specific
59 blacklists found in the C<__DATA__> section of this script.
61 This helps to find untranslated values.
63 =item --untranslated-values-all
65 Like C<--untranslated-values> but ignores blacklists.
67 =item --validate-variables
69 Check that interpolated Ruby i18n variables (C<{{foo}}> and
70 C<[[foo]]>) are equivalent in the two provided files.
76 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@f-prot.com>
80 # Get the command-line options
81 Getopt::Long::Parser->new(
82 config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
84 'h|help' => \my $help,
86 'untranslated-values' => \my $untranslated_values,
87 'untranslated-values-all' => \my $untranslated_values_all,
88 'validate-variables' => \my $validate_variables,
91 # --keys is the default
92 $keys = 1 if not $untranslated_values_all and not $untranslated_values and not $validate_variables;
97 # If we're not given two .yml files
98 help() if @ARGV != 2 or (!-f $ARGV[0] or !-f $ARGV[1]);
100 my ($from, $to) = @ARGV;
102 my $from_data = LoadFile($from);
103 my $to_data = LoadFile($to);
105 my $from_parsed = { iterate($from_data->{fileparse($from, qr/\.[^.]*/)}) };
106 my $to_parsed = { iterate($to_data->{fileparse($to, qr/\.[^.]*/)}) };
110 print_key_differences($from_parsed, $to_parsed);
112 elsif ($untranslated_values or $untranslated_values_all)
114 my @untranslated = untranslated_keys($from_parsed, $to_parsed);
116 # Prune according to blacklist
117 if ($untranslated_values) {
118 @untranslated = prune_untranslated_with_blacklist(scalar(fileparse($to, qr/\.[^.]*/)), @untranslated);
121 say for @untranslated;
122 } elsif ($validate_variables)
124 print_validate_variables($from_parsed, $to_parsed);
129 sub print_key_differences
133 # Hack around Test::Differences wanting a Test::* module loaded
135 sub Test::ok { print shift }
138 eq_or_diff([ sort keys %$f ], [ sort keys %$t ]);
141 sub untranslated_keys
143 my ($from_parsed, $to_parsed) = @_;
144 sort grep { exists $to_parsed->{$_} and $from_parsed->{$_} eq $to_parsed->{$_} } keys %$from_parsed;
147 sub prune_untranslated_with_blacklist
149 my ($language, @keys) = @_;
153 my $end_yaml = LoadFile(*DATA);
154 my $untranslated_values = $end_yaml->{untranslated_values};
155 my $default = $untranslated_values->{default};
156 my $this_language = $untranslated_values->{$language} || {};
158 my %bw_list = (%$default, %$this_language);
160 while (my ($key, $blacklisted) = each %bw_list)
162 # FIXME: Does syck actually support true/false booleans in yaml?
163 delete $keys{$key} if $blacklisted eq 'true'
169 sub print_validate_variables
173 while (my ($key, $val) = each %$f)
175 next if exists $f->{$key} and not exists $t->{$key};
177 my @from_var = parse_variables_from_string($f->{$key});
178 my @to_var = parse_variables_from_string($t->{$key});
180 unless (@from_var ~~ @to_var) {
181 say "$key in $from has (@from_var) and $to has (@to_var)";
187 sub parse_variables_from_string
191 # This probably matches most of the variables
192 my $var = qr/ [a-z0-9_]+? /xs;
194 if (my @var = $string =~ m/ \{\{ ($var) \}\} | \[\[ ($var) \]\] /gsx) {
195 return sort grep { defined } @var;
203 my ($hash, @path) = @_;
206 while (my ($k, $v) = each %$hash)
208 if (ref $v eq 'HASH')
210 push @ret => iterate($v, @path, $k);
214 push @ret => join(".",@path, $k), $v;
225 Pod::Usage::pod2usage(
226 -verbose => $arg{ verbose },
227 -exitval => $arg{ exitval } || 0,
234 # Default/Per language blacklist/whitelist for the
235 # --untranslated-values switch. "true" as a value indicates that the
236 # key is to be blacklisted, and "false" that it's to be
237 # whitelisted. "false" is only required to whitelist a key
238 # blacklisted by default on a per-language basis.
242 layouts.intro_3_bytemark: true
243 layouts.intro_3_ucl: true
244 layouts.project_name.h1: true
245 layouts.project_name.title: true
246 site.index.license.project_url: true
247 browse.relation_member.entry: true
250 changeset.changeset.id: true
253 activerecord.attributes.message.sender: true
254 activerecord.attributes.trace.name: true
255 activerecord.models.changeset: true
256 activerecord.models.relation: true
257 browse.changeset.changeset: true
258 browse.changeset.changesetxml: true
259 browse.changeset.osmchangexml: true
260 browse.changeset.title: true
261 browse.common_details.version: true
262 browse.containing_relation.relation: true
263 browse.relation.relation: true
264 browse.relation.relation_title: true
265 browse.start_rjs.details: true
266 browse.start_rjs.object_list.details: true
267 browse.tag_details.tags: true
268 changeset.changesets.id: true
269 export.start.export_button: true
270 export.start.format: true
271 export.start.output: true
272 export.start.zoom: true
273 export.start_rjs.export: true
276 site.edit.anon_edits: true
277 site.index.license.license_name: true
278 site.index.permalink: true
279 site.key.table.entry.park: true
280 site.search.submit_text: true
281 trace.edit.tags: true
283 trace.trace_form.tags: true
284 trace.trace_optionals.tags: true
285 trace.view.tags: true
286 user.account.public editing.enabled link: true
290 site.edit.anon_edits: true
292 # Creative Commons Attribution-Share Alike 2.0
293 site.index.license.license_name: true
295 # http://creativecommons.org/licenses/by-sa/2.0/
296 site.index.license.license_url: true
299 printable_name.with_id: true
302 printable_name.with_name: true
305 geocoder.search_osm_namefinder.prefix: true
307 # {{suffix}}, {{parentname}}
308 geocoder.search_osm_namefinder.suffix_suburb: true