]> git.openstreetmap.org Git - rails.git/blobdiff - script/locale/merge-from-translatewiki
Replace quova with support for local Maxmind GeoIP lookups
[rails.git] / script / locale / merge-from-translatewiki
old mode 100644 (file)
new mode 100755 (executable)
index 6f0120a..706ec45
@@ -30,6 +30,9 @@ merge-from-translatewiki - Get new translations from L<http://translatewiki.net>
     config/locales$ for i in $(ls *yml | grep -v en.yml); do perl ../../script/locale/diff --dump-flat $i > $i.0 ;done
 
     # Merge and find out what changed:
+    rails_port$ perl script/locale/merge-from-translatewiki --locales-dir config/locales
+
+    # Or, more complexy:
     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
  
     # Diff:
@@ -51,12 +54,17 @@ Print this help message.
 
 =item --locales-dir
 
-The locales dir we'll merge stuff into. E.g. C<config/locales>.
+The locales dir we'll merge stuff into. F<config/locales> by default.
 
 =item --only-new
 
 Only import translations that don't exists for us yet.
 
+=item --cache
+
+Write a L<Storable> cache for things downloaded from Translatewiki and
+use it if it exists.
+
 =back
 
 =head1 AUTHOR
@@ -70,14 +78,15 @@ Getopt::Long::Parser->new(
     config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
 )->getoptions(
     'h|help' => \my $help,
-    'locales-dir=s' => \my $locales_dir,
+    'locales-dir=s' => \(my $locales_dir = 'config/locales'),
     'only-new' => \my $only_new,
+    'cache' => \my $cache,
 ) or help();
 
 # On --help
 help() if $help;
 
-help() unless $locales_dir;
+help() unless $locales_dir and -d $locales_dir;
 
 ###
 ### Main
@@ -193,6 +202,16 @@ for my $translatewiki_lang (sort @translatewiki_languages_codes) {
             }
         }
 
+        # There are a bunch of keys on Translatewiki that are
+        # equivalent to English for some reason. Probably because they
+        # were there at import time. Nuke them.
+        while (my ($new_k, $new_v) = each %new) {
+            if (exists $en{ $new_k } and $en{ $new_k } eq $new_v) {
+                #say "Purging dupe in $rails_lang: $new_k=$new_v";
+                delete $new{ $new_k };
+            }
+        }
+
         my $expanded = expand_hash( \%new );
         my $out = +{ $rails_lang => $expanded };
         spit_out($out_file, $out);
@@ -207,7 +226,6 @@ sub spit_out
     my $yaml_out = Dump $data;
     
     open my $fh, ">", $file;
-    say $fh "# Imported at " . (scalar localtime) . " from Translatewiki.net";
     print $fh $yaml_out;
     close $fh;
 }
@@ -300,9 +318,11 @@ sub get_translatewiki_translations
     my @languages = @_;
 
     my $cache_file = "/tmp/merge-from-translatewiki.storable";
-    if (-f $cache_file) {
-        my $cache = retrieve($cache_file);
-        return %$cache;
+    if ($cache) {
+        if (-f $cache_file) {
+            my $c = retrieve($cache_file);
+            return %$c;
+        }
     }
 
     my %translatewiki_languages;
@@ -319,7 +339,9 @@ sub get_translatewiki_translations
         $translatewiki_languages{$lang} = $flat_data;
     }
 
-    store \%translatewiki_languages, $cache_file;
+    if ($cache) {
+        store \%translatewiki_languages, $cache_file;
+    }
 
     return %translatewiki_languages;
 }
@@ -329,7 +351,7 @@ sub get_language_from_translatewiki
     my ($lang) = @_;
     my $mech = WWW::Mechanize->new;
 
-    $mech->get("http://translatewiki.net/w/i.php?title=Special%3ATranslate&task=export-to-file&group=out-osm&language=$lang");
+    $mech->get("http://translatewiki.net/w/i.php?title=Special%3ATranslate&task=export-to-file&group=out-osm-site&language=$lang");
 
     die "Couldn't get lang $lang lang from Translatewiki" unless $mech->success;
 
@@ -344,7 +366,7 @@ sub translatewiki_languages
 {
     my $mech = WWW::Mechanize->new;
 
-    $mech->get('http://translatewiki.net/wiki/Translating:OpenStreetMap/stats/trunk');
+    $mech->get('http://translatewiki.net/wiki/Translating:OpenStreetMap/stats/trunk/site');
 
     die "Couldn't get translatewiki table" unless $mech->success;