5 use File::Slurp qw(slurp);
6 use YAML::Syck qw(Dump Load LoadFile DumpFile);
8 $YAML::Syck::Headless = 1;
9 $YAML::Syck::SortKeys = 1;
12 use HTML::TableParser::Grid;
15 use Data::Dump 'dump';
16 use File::Spec::Functions qw(catfile);
22 merge-from-translatewiki - Get new translations from L<http://translatewiki.net> and selectively merge them with ours
26 merge-from-translatewiki --locales-dir=config/locales
28 # Diff the existing files:
29 config/locales$ for i in $(ls *yml | grep -v en.yml); do perl ../../script/locale/diff --dump-flat $i > $i.0 ;done
31 # Merge and find out what changed:
32 rails_port$ for i in $(svn st config/locales/ | egrep '^M|\\?' | awk '{print $2}' | grep 'yml$'); do rm -v $i; done && svn up config/locales && perl script/locale/merge-from-translatewiki --locales-dir config/locales && svn st config/locales
35 config/locales$ for i in $(ls *yml | grep -v en.yml); do perl ../../script/locale/diff --dump-flat $i > $i.1 ;done && for i in $(ls *yml | grep -v en.yml); do diff -ru $i.*; done
39 Translatewiki's export process L<is
40 broken|http://trac.openstreetmap.org/ticket/2305>. This script imports
41 new messages from it while tiptoeing around known bugs.
49 Print this help message.
53 The locales dir we'll merge stuff into. E.g. C<config/locales>.
59 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
63 # Get the command-line options
64 Getopt::Long::Parser->new(
65 config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
67 'h|help' => \my $help,
68 'locales-dir=s' => \my $locales_dir,
74 help() unless $locales_dir;
80 ### Get Translatewiki data
81 my %translatewiki_languages = translatewiki_languages();
83 # Don't process English from Translatewiki
84 delete $translatewiki_languages{en};
86 #say Dump \%translatewiki_languages;
88 my @translatewiki_languages_codes = keys %translatewiki_languages;
89 my %translatewiki_translations = get_translatewiki_translations(@translatewiki_languages_codes);
91 #say Dump \%translatewiki_translations;
93 ### Get our existing data
95 my @my_yaml_files = glob catfile($locales_dir, '*.yml');
96 for my $my_yaml_file (@my_yaml_files) {
97 my $basename = basename($my_yaml_file);
98 my $tw_lang = lc $basename;
100 say STDERR "Loading my translation $tw_lang ($my_yaml_file)";
101 $my_translations{$tw_lang} = load_and_flatten_yaml(scalar slurp($my_yaml_file));
104 say "loaded my translations";
106 ## Write out merged data
107 for my $translatewiki_lang (@translatewiki_languages_codes) {
108 my $rails_lang = $translatewiki_lang; $rails_lang =~ s/(?<=-)(\w+)/\U$1\E/;
109 my $out_file = catfile($locales_dir, $rails_lang . '.yml');
111 unless (-f $out_file) {
112 # No translation like this exists
113 say STDERR "$rails_lang has no existing translation. Importing as-is from Translatewiki to $out_file";
114 my $expanded = expand_hash($translatewiki_translations{$translatewiki_lang});
115 my $out = +{ $rails_lang => $expanded };
116 spit_out($out_file, $out);
117 } elsif (ref $my_translations{$translatewiki_lang} eq 'HASH') {
118 say STDERR "$rails_lang has existing translations. Merging";
121 my %tw = %{ $translatewiki_translations{$translatewiki_lang} };
122 my %me = %{ $my_translations{$translatewiki_lang} };
123 # Use %tw to start with
128 # These keys shouldn't be removed
130 layouts.help_wiki_url
132 notifier.gpx_notification.failure.import_failures_url
133 notifier.signup_confirm_plain.the_wiki_url
134 notifier.signup_confirm_plain.wiki_signup_url
135 trace.edit.visibility_help_url
136 trace.trace_form.help_url
137 trace.trace_form.visibility_help_url
140 for my $key (@url_keys) {
141 if ( exists $me{$key} and not exists $new{$key} ) {
142 $new{$key} = $me{$key};
146 my $expanded = expand_hash( \%new );
147 my $out = +{ $rails_lang => $expanded };
148 spit_out($out_file, $out);
150 die "Internal error on $translatewiki_lang";
156 my ($file, $data) = @_;
157 my $yaml_out = Dump $data;
159 open my $fh, ">", $file;
160 say $fh "# Imported at " . (scalar localtime) . " from Translatewiki.net";
174 map { if (ref $_ eq 'ARRAY') { map { utf8::decode($_) } @$_ } else { utf8::decode($_) } } values %$hash;
179 my ($hash, @path) = @_;
182 while (my ($k, $v) = each %$hash)
184 if (ref $v eq 'HASH')
186 push @ret => iterate($v, @path, $k);
190 push @ret => join(".",@path, $k), $v;
199 my ($flat_hash) = @_;
201 while (my ($k, $v) = each %$flat_hash) {
202 insert_string_deep(\%new_hash, $k, $v);
208 sub insert_string_deep {
209 my ($h, $ks, $v) = @_;
210 my $p = \$h; $p = \$$p->{$_} for split /\./, $ks;
215 # Get language from Translatewiki
218 sub get_translatewiki_translations
222 my $cache_file = "/tmp/merge-from-translatewiki.storable";
223 if (-f $cache_file) {
224 my $cache = retrieve($cache_file);
228 my %translatewiki_languages;
229 say "All languages are: @languages";
230 for my $lang (@languages) {
231 say STDERR "Getting language $lang from Translatewiki";
232 my $yaml = get_language_from_translatewiki($lang);
234 my $flat_data = load_and_flatten_yaml($yaml);
236 $translatewiki_languages{$lang} = $flat_data;
239 store \%translatewiki_languages, $cache_file;
241 return %translatewiki_languages;
244 sub get_language_from_translatewiki
247 my $mech = WWW::Mechanize->new;
249 $mech->get("http://translatewiki.net/w/i.php?title=Special%3ATranslate&task=export-to-file&group=out-osm&language=$lang");
251 die "Couldn't get lang $lang lang from Translatewiki" unless $mech->success;
253 return $mech->content;
260 sub translatewiki_languages
262 my $mech = WWW::Mechanize->new;
264 $mech->get('http://translatewiki.net/wiki/Translating:OpenStreetMap/stats/trunk');
266 die "Couldn't get translatewiki table" unless $mech->success;
268 my $content = $mech->content;
269 my ($sortable) = $content =~ m[(<table class="sortable.*</table>)]s;
271 my @table = parse_language_table($sortable);
274 map { $_->{code} => $_->{language} } @table;
277 sub parse_language_table
281 my $parser = HTML::TableParser::Grid->new($table);
283 for my $n (0 .. $parser->num_rows - 1) {
285 @row{qw(code language done fuzzy)} = $parser->row($n);
301 $name =~ s[\..*?$][];
306 sub load_and_flatten_yaml
310 my $data = Load($yaml);
312 # Remove the root $lang => key
313 my @keys = keys %$data;
314 die "YAML data had more than 1 root key" if @keys != 1;
315 $data = $data->{$keys[0]};
318 my $flat_data = { iterate($data) };
320 mark_utf8($flat_data);
333 Pod::Usage::pod2usage(
334 -verbose => $arg{ verbose },
335 -exitval => $arg{ exitval } || 0,