| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Author: Petter Reinholdtsen <[email protected]> |
| 4 |
# Date: 2002-02-23 |
| 5 |
# |
| 6 |
# Test if the locale given as argument is a valid locale. If it |
| 7 |
# is not, print on stdout the string to add to /etc/locale.gen to make |
| 8 |
# locale-gen generate the locale (if it exists at all). |
| 9 |
|
| 10 |
use POSIX qw(setlocale LC_ALL); |
| 11 |
|
| 12 |
my $debug = 0; |
| 13 |
|
| 14 |
my $defaultcharset = $ENV{"DEFAULTCHARSET"} || "ISO-8859-1"; |
| 15 |
|
| 16 |
my $supportedlist = "/usr/share/i18n/SUPPORTED"; |
| 17 |
|
| 18 |
unless (defined $ARGV[0]) { |
| 19 |
usage(); |
| 20 |
exit 1; |
| 21 |
} |
| 22 |
|
| 23 |
my $LANG = $ARGV[0]; |
| 24 |
|
| 25 |
my $loc = setlocale(LC_ALL, $LANG); |
| 26 |
if ( ! $loc) { |
| 27 |
print STDERR "locale '$LANG' not available\n"; |
| 28 |
|
| 29 |
my ($locale) = $LANG =~ m/^([^.@]+)/; |
| 30 |
my ($charset) = $LANG =~ m/^[^.]+\.([^@]+)/; |
| 31 |
my ($modifier) = $LANG =~ m/(@.+)$/; |
| 32 |
|
| 33 |
$modifier = "" unless defined $modifier; |
| 34 |
|
| 35 |
# Hm, if charset is missing, how to we pick the correct one to |
| 36 |
# use? Fetching the value from /usr/share/i18n/SUPPORTED should |
| 37 |
# work on Debian. |
| 38 |
my $codeset = ""; |
| 39 |
if (defined $charset) { |
| 40 |
$codeset = '.' . $charset; |
| 41 |
} else { |
| 42 |
$charset = get_default_charset("$locale$modifier"); |
| 43 |
} |
| 44 |
|
| 45 |
# print "L: $locale C: $charset M: $modifier\n"; |
| 46 |
print "$locale$codeset$modifier $charset\n"; |
| 47 |
|
| 48 |
exit 1; |
| 49 |
} else { |
| 50 |
print STDERR "locale '$LANG' valid and available\n"; |
| 51 |
exit 0; |
| 52 |
} |
| 53 |
|
| 54 |
sub usage { |
| 55 |
print "Usage: $0 <locale>\n" |
| 56 |
} |
| 57 |
|
| 58 |
sub get_default_charset { |
| 59 |
my ($locale) = @_; |
| 60 |
my ($l, $c); |
| 61 |
open(SUPPORTED, "< $supportedlist") || die "Unable to open $supportedlist"; |
| 62 |
while (<SUPPORTED>) { |
| 63 |
chomp; |
| 64 |
($l, $c) = split(/\s+/); |
| 65 |
print "Checking '$l' '$c' != '$locale'\n" if $debug; |
| 66 |
last if ($l eq $locale); |
| 67 |
} |
| 68 |
close(SUPPORTED); |
| 69 |
|
| 70 |
if ($l eq $locale) { |
| 71 |
return $c; |
| 72 |
} else { |
| 73 |
return $defaultcharset; |
| 74 |
} |
| 75 |
} |