]> git.openstreetmap.org Git - rails.git/blob - script/locale/update-languages
Add script to update config/languages.yml from translatewiki language list.
[rails.git] / script / locale / update-languages
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Encode;
7 use HTML::TreeBuilder;
8 use LWP::UserAgent;
9 use YAML::Syck;
10
11 $YAML::Syck::Headless = 1;
12 $YAML::Syck::ImplicitUnicode = 1;
13 $YAML::Syck::SortKeys = 1;
14
15 my $languages = LoadFile("config/languages.yml");
16
17 my $ua = LWP::UserAgent->new(env_proxy => 1);
18 my $response = $ua->get("http://translatewiki.net/wiki/User:Siebrand/langs?uselang=en");
19
20 die $response->message unless $response->is_success;
21
22 my $root = HTML::TreeBuilder->new_from_content($response->decoded_content);
23 my $content = $root->look_down("_tag" => "div", "id" => "mw_contentholder");
24 my $list = $content->look_down("_tag" => "ul");
25
26 for my $item ($list->look_down("_tag" => "li"))
27 {
28     my $language = $item->as_trimmed_text;
29
30     if ($language =~ /^([a-z]+(?:-[a-z]+)*) +- +(.*) +- +(.*)$/i)
31     {
32         my $code = $1;
33         my $english_name = decode_utf8($2);
34         my $native_name = decode_utf8($3);
35
36         next if $english_name =~ /\(deprecated:[a-z]+(?:-[a-z]+)*\)$/;
37
38         if (length($code) == 2 || -f "config/locales/${code}.yml")
39         {
40             $languages->{$code}->{english} = $english_name;
41             $languages->{$code}->{native} = $native_name;
42         }
43     }
44     else
45     {
46         die "Can't parse: $language";
47     }
48 }
49
50 DumpFile("config/languages.yml", $languages);
51
52 exit 0;