~ubuntu-core-dev/localechooser/ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/perl
# Builds templates with list of countries per continent (region)
# and a template to select the continent.
use strict;
use warnings;

# Parameters.
my $iso3166tab=shift;
my $regionmap=shift;
my $outfile=shift;

# These are the regions to use. The list should be sorted alphabetically.
# There is no call to gettext for Antarctica as that is included in ISO-3166.
my @known_regions = (
	gettext("Africa"),
	"Antarctica",
	gettext("Asia"),
	gettext("Atlantic Ocean"),
	gettext("Caribbean"),
	gettext("Central America"),
	gettext("Europe"),
	gettext("Indian Ocean"),
	gettext("North America"),
	gettext("Oceania"),
	gettext("South America"),
	gettext("other"),
);

my @regions;
my %is_region;
for (@known_regions) { $is_region{$_} = 1; }

my %code2country;
my %country2code;
my %codes_listed;

open (L, $iso3166tab) || die "$iso3166tab: $!";
while (<L>) {
	chomp;
	my ($code, $country) = split(' ', $_, 2);

	# Escape commas
	$country =~ s/,/\\,/g;
	$code2country{$code}=$country;
	$country2code{$country}=$code;
	$codes_listed{$code}=0;
}

my %regions;
open (R, $regionmap) || die "$regionmap: $!";
while (<R>) {
	chomp;
	my ($code, $region) = split(' ', $_, 2);
	if (exists $code2country{$code}) {
		if ($is_region{$region}) {
			push @{$regions{$region}}, $code2country{$code};
			$codes_listed{$code}++;
		} else {
			print STDERR "E: country code '$code': region '$region' is not defined\n";
			exit 1
		}
	} elsif ($code ne 'unlisted') {
		print STDERR "I: skipping country code '$code': in $regionmap but not in $iso3166tab\n";
	}
}
close R;

# Add any unlisted countries to "other".
foreach my $code (keys %codes_listed) {
	if ($codes_listed{$code} == 0) {
		push @{$regions{"other"}}, $code2country{$code};
		print STDERR "W: unknown region for country $code: not listed in $regionmap\n";
	}
}

open(TOUT, "> $outfile") || die "Unable to write $outfile";
print TOUT "\n";

foreach my $region (@known_regions) {
	if (exists $regions{$region}) {
		push @regions, $region;

		my @countries;
		my @codes;
		foreach my $country (sort @{$regions{$region}}) {
			push @countries, "$country";
			push @codes, "$country2code{$country}";
		}

		my $tregion = $region;
		$tregion =~ s/ /_/g;
		print TOUT "Template: localechooser/countrylist/$tregion\n";
		print TOUT "Type: select\n";
		print TOUT "Choices-C: ", join(", ", @codes), "\n";
		print TOUT "#flag:partial\n";
		print TOUT "__Choices: ", join(", ", @countries), "\n";
		print TOUT "Description: \${TXT1}\n";
		print TOUT " \${TXT2}\n";
		print TOUT " .\n";
		print TOUT " \${TXT3}\n";
		print TOUT "\n";
	} else {
		print STDERR "I: skipping region $region: no associated countries in $regionmap\n";
		delete $regions{$region};
	}
}

print TOUT "Template: localechooser/continentlist\n";
print TOUT "Type: select\n";
print TOUT "#flag:partial\n";
print TOUT "__Choices: ",  join(", ", @regions), "\n";
print TOUT "Description: \${TXT1}\n";
print TOUT " \${TXT2}\n";
print TOUT " .\n";
print TOUT " \${TXT3}\n";

close(TOUT) || warn;

# Dummy.
sub gettext {
	return shift;
}