]> git.openstreetmap.org Git - rails.git/blob - script/locale/diff
Detect test environment correctly
[rails.git] / script / locale / diff
1 #!/usr/bin/env perl
2 use feature ':5.10';
3 use strict;
4 use warnings;
5 use YAML::Syck qw(Dump LoadFile);
6 use Test::Differences;
7 use Pod::Usage ();
8 use Getopt::Long ();
9
10 =head1 NAME
11
12 locale-diff - Compare two YAML files and print how their datastructures differ
13
14 =head1 SYNOPSIS
15
16     # --keys is the default
17     diff en.yml is.yml
18     diff --keys en.yml is.yml
19
20     # --untranslated-values compares prints keys whose values don't differ
21     diff --untranslated-values en.yml is.yml
22
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
27
28     # Check that interpolated variables ({{var}} and [[var]]) are the same
29     diff --validate-variables en.yml is.yml
30
31 =head1 DESCRIPTION
32
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.
37
38 =head1 OPTIONS
39
40 =over
41
42 =item -h, --help
43
44 Print this help message.
45
46 =item --keys
47
48 Show the hash keys that differ between the two files, useful merging
49 new entries from F<en.yml> to a local file.
50
51 =item --untranslated-values
52
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
55 been merged.
56
57 The values are pruned according to global and language specific
58 blacklists found in the C<__DATA__> section of this script.
59
60 This helps to find untranslated values.
61
62 =item --untranslated-values-all
63
64 Like C<--untranslated-values> but ignores blacklists.
65
66 =item --validate-variables
67
68 Check that interpolated Ruby i18n variables (C<{{foo}}> and
69 C<[[foo]]>) are equivalent in the two provided files.
70
71 =item --dump-flat
72
73 Dump a flat version of the translation hash in YAML format,
74 i.e. "foo.bar" instead of "{foo}->{bar}".
75
76 =back
77
78 =head1 AUTHOR
79
80 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
81
82 =cut
83
84 # Get the command-line options
85 Getopt::Long::Parser->new(
86     config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
87 )->getoptions(
88     'h|help' => \my $help,
89     'keys' => \my $keys,
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,
95 ) or help();
96
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;
99
100 # On --help
101 help() if $help;
102
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;
105
106 my ($from, $to) = @ARGV;
107
108 my $from_data = LoadFile($from);
109 my $from_parsed = { iterate($from_data->{basename($from)}) };
110
111 if ($dump_flat)
112 {
113     mark_utf8($from_parsed);
114
115     print Dump $from_parsed;
116
117     exit 0;
118 }
119
120 if ($reconstruct) {
121     mark_utf8($from_parsed);
122
123     my %out;
124     while (my ($k, $v) = each %$from_parsed) {
125         insert_string_deep(\%out, $k, $v);
126     }
127
128     print Dump { basename($from) => \%out };
129
130     exit 0;
131 }
132
133 my $to_data   = LoadFile($to);
134 my $to_parsed = { iterate($to_data->{basename($to)}) };
135
136 if ($keys)
137 {
138     print_key_differences($from_parsed, $to_parsed);
139 }
140 elsif ($untranslated_values or $untranslated_values_all)
141 {
142     my @untranslated = untranslated_keys($from_parsed, $to_parsed);
143
144     # Prune according to blacklist
145     if ($untranslated_values) {
146         @untranslated = prune_untranslated_with_blacklist(basename($to), @untranslated);
147     }
148
149     say for @untranslated;
150 } elsif ($validate_variables)
151 {
152     print_validate_variables($from_parsed, $to_parsed);
153 }
154
155 exit 0;
156
157 sub print_key_differences
158 {
159     my ($f, $t) = @_;
160
161     # Hack around Test::Differences wanting a Test::* module loaded
162     $INC{"Test.pm"} = 1;
163     sub Test::ok { print shift }
164
165     # Diff the tree
166     eq_or_diff([ sort keys %$f ], [ sort keys %$t ]);
167 }
168
169 sub untranslated_keys
170 {
171     my ($from_parsed, $to_parsed) = @_;
172     sort grep { exists $to_parsed->{$_} and $from_parsed->{$_} eq $to_parsed->{$_} } keys %$from_parsed;
173 }
174
175 sub prune_untranslated_with_blacklist
176 {
177     my ($language, @keys) = @_;
178     my %keys;
179     @keys{@keys} = ();
180
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} || {};
185
186     my %bw_list = (%$default, %$this_language);
187     
188     while (my ($key, $blacklisted) = each %bw_list)
189     {
190         # FIXME: Does syck actually support true/false booleans in yaml?
191         delete $keys{$key} if $blacklisted eq 'true'
192     }
193
194     sort keys %keys;
195 }
196
197 sub print_validate_variables
198 {
199     my ($f, $t) = @_;
200
201     while (my ($key, $val) = each %$f)
202     {
203         next if exists $f->{$key} and not exists $t->{$key};
204
205         my @from_var = parse_variables_from_string($f->{$key});
206         my @to_var   = parse_variables_from_string($t->{$key});
207
208         unless (@from_var ~~ @to_var) {
209             say "$key in $from has (@from_var) and $to has (@to_var)";
210         }
211
212     }
213 }
214
215 sub parse_variables_from_string
216 {
217     my ($string) = @_;
218
219     # This probably matches most of the variables
220     my $var = qr/ [a-z0-9_]+? /xs;
221
222     if (my @var = $string =~ m/ \{\{ ($var) \}\} | \[\[ ($var) \]\] /gsx) {
223         return sort grep { defined } @var;
224     } else {
225         return;
226     }
227 }
228
229 sub iterate
230 {
231     my ($hash, @path) = @_;
232     my @ret;
233         
234     while (my ($k, $v) = each %$hash)
235     {
236         if (ref $v eq 'HASH')
237         {
238              push @ret => iterate($v, @path, $k);
239         }
240         else
241         {
242             push @ret => join(".",@path, $k), $v;
243         }
244     }
245
246     return @ret;
247 }
248
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;
254     $$p = $v;
255 }
256
257 # sub insert_string_deep
258 # {
259 #     my ($hash, $key, $value) = @_;
260 #
261 #     my @key = split /\./, $key;
262 #     my $h = $hash;
263 #
264 #     my $i = 0;
265 #     for my $k (@key) {
266 #         $i ++;
267 #         if ($i == @key) {
268 #             $h->{$k} = $value;
269 #         } else {
270 #             if (ref $h->{$k}) {
271 #                 $h = $h->{$k};
272 #             } else {
273 #                 $h = $h->{$k} = {};
274 #             }
275 #         }
276 #     }
277 # }
278
279 sub basename
280 {
281     my $name = shift;
282     $name =~ s[\..*?$][];
283     $name =~ s[.*/][];
284     $name;
285 }
286
287 sub mark_utf8
288 {
289     my ($hash) = @_;
290
291     # Mark as UTF-8
292     map { if (ref $_ eq 'ARRAY') { map { utf8::decode($_) } @$_ } else {  utf8::decode($_) } } values %$hash;
293 }
294
295 sub help
296 {
297     my %arg = @_;
298
299     Pod::Usage::pod2usage(
300         -verbose => $arg{ verbose },
301         -exitval => $arg{ exitval } || 0,
302     );
303 }
304
305 __DATA__
306 untranslated_values:
307
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.
313
314   default:
315     html.dir: true
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
322
323     # #{{id}}
324     changeset.changeset.id: true
325
326   de:
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
348     layouts.export: true
349     layouts.shop: 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
356     trace.trace.in: 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
361
362   is:
363     # ({{link}})
364     site.edit.anon_edits: true
365
366     # Creative Commons Attribution-Share Alike 2.0
367     site.index.license.license_name: true
368
369     # http://creativecommons.org/licenses/by-sa/2.0/
370     site.index.license.license_url: true
371
372     # {{id}}
373     printable_name.with_id: true
374     
375     # {{name}} ({{id}})
376     printable_name.with_name: true
377
378     # {{type}} 
379     geocoder.search_osm_namefinder.prefix: true
380
381     # {{suffix}}, {{parentname}}
382     geocoder.search_osm_namefinder.suffix_suburb: true