package INSTALL::locale; use INSTALL::common; use INSTALL::ini; use INSTALL::global; use INSTALL::query; use INSTALL::parameter; use strict; # # locales map # # ex. ["literal string", lcid, codepage, default locale, [ locale strings, ... ],] # my @supported_locales; # List of supported locale structs. my %locale_hash; # Mapping between all locale strings and their associated locale struct # (e.g. "en_US" => ["English - US", "0409", "1252", ["en_US", "POSIX", "C"],0]) my @available_locales; # List of those @supported_locales which are available on this OS. # Guaranteed to be in the same order as @supported_locales. my $pGlobals = \%INSTALL::global::variables; my $pParameters = \%INSTALL::parameter::variables; BEGIN { @supported_locales = ( ["English - US", "0409", "1252", ["en_US", "POSIX", "C"]], ["English - British", "0809", "1252", ["en_GB", "en_UK"]], ["Japanese Shift-JIS", "0411", "932", ["ja", "japanese"]], ["German", "0407", "1252", ["de_DE", "de" ]], ["Dutch", "0413", "1252", ["nl_NL", "nl" ]], ["Spanish", "040a", "1252", ["es_ES", "es"]], ["French", "040c", "1252", ["fr_FR", "fr"]], ["Swedish", "041d", "1252", ["sv_SV", "sv"]], ); # Append index. my $i = 0; foreach my $pLocales (@supported_locales) { push @$pLocales,$i++; } # Create hash mapping between all locale strings and their associated locale struct. foreach my $pLocales (@supported_locales) { foreach my $locale_string (@{$pLocales->[3]}) { $locale_hash{$locale_string} = $pLocales; } } ## Detect which of the system locales are supported. #my @system_locales = split(/\n/,`locale -a`); #my %detected_locales; #foreach my $locale_string (@system_locales) { # if (exists $locale_hash{$locale_string}) { # $detected_locales{$locale_hash{$locale_string}} = undef; # } #} # On HP-UX like systems, the locale keys has different formats like # iso88591,utf,roman etc. # Presently our code is not responsible for looking which format of the # locale string to be supported except to decide whether to support it # or NOT. We leave this phase for futuer development. # Detect which of the system locales are supported. my @system_locales = split(/\n/,`locale -a`); my %detected_locales; foreach my $locale_string (@system_locales) { my $valid_locale = $locale_string; if ($valid_locale =~ m@(\S+)\.(.*)@) { $valid_locale =~ s@(\S+)\.(.*)@$1@; $locale_string = $valid_locale; } if (exists $locale_hash{$locale_string}) { $detected_locales{$locale_hash{$locale_string}} = undef; } } # We want the available locales listed in the same order as they are # in supported locales. @available_locales = (); foreach my $pLocale (@supported_locales) { if (exists $detected_locales{$pLocale}) { push @available_locales,$pLocale; } } } #------------------------------------------------------------------------------ # converts a locale string into an LCID value. Returns the converted # LCID value which defaults to 0409(en_US) on an error #------------------------------------------------------------------------------ sub locale2lcid { ($#_ == 0) || die "Invalid number of arguments."; my ($locale) = @_; if (exists $locale_hash{$locale}) { return($locale_hash{$locale}->[1]); } else { error("Unable to locate locale '$locale'."); return($locale_hash{en_US}->[1]); } } #------------------------------------------------------------------------------ # converts a locale string into a CODEPAGE value. Returns the converted # CODEPAGE value which defaults to 1252(en_US) on an error #------------------------------------------------------------------------------ sub locale2codepage { ($#_ == 0) || die "Invalid number of arguments."; my ($locale) = @_; if (exists $locale_hash{$locale}) { return($locale_hash{$locale}->[2]); } else { error("Unable to locate locale '$locale'."); return($locale_hash{en_US}->[2]); } } #------------------------------------------------------------------------------ # creates an ini file which lists the available locales for the system #------------------------------------------------------------------------------ sub build_locale_cache { ($#_ == 0) || die "Invalid number of arguments."; my ($cache_file) = @_; my $pIni = INSTALL::ini::open($cache_file); if ($pIni) { foreach my $pLocale (@available_locales) { my $pComment = INSTALL::ini::insertComments($pIni); $pComment->[0] = "locale cache created on ".scalar(localtime); my $pSection = INSTALL::ini::insertSection($pIni,$pLocale->[0]); if ($pSection) { $pSection->{lcid} = $pLocale->[1]; $pSection->{codepage} = $pLocale->[2]; } else { return(error("Unable to create section in $cache_file")); } } } else { return(error("Unable to open $cache_file for writing.")); } return(INSTALL::ini::save($pIni,$cache_file)); } #------------------------------------------------------------------------------ # returns an array of english strings representing the os supported locales. #------------------------------------------------------------------------------ sub listlocales { return(map { $_->[0] } @available_locales); } #------------------------------------------------------------------------------ # returns an array of english strings representing the os supported locales. #------------------------------------------------------------------------------ sub configure { # Choose default OS language, if such a beast can be detected. my $locale_string; if (exists $ENV{LC_ALL}) { $locale_string = $ENV{LC_ALL}; } elsif (exists $ENV{LC_LANG}) { $locale_string = $ENV{LC_LANG}; } elsif (exists $ENV{LANGUAGE}) { $locale_string = $ENV{LANGUAGE}; } elsif (exists $ENV{LANG}) { $locale_string = $ENV{LANG}; } my $default = 1; if (defined $locale_string && (exists $locale_hash{$locale_string})) { $default = $locale_hash{$locale_string}->[4] + 1; } my $pLocale; if ((not exists $pParameters->{locale}) || (not exists $locale_hash{$pParameters->{locale}})) { my $locale_index = INSTALL::query::user_menu ("Sun Chili!Soft ASP - Language Selection", "Sun Chili!Soft ASP supports various languages. Select the ". "language you want to use from the list below.", "Which language would you like to use", "terminal_mark=?", listlocales, "default=$default" ); # Indexes are 0 based. $locale_index--; $pLocale = $available_locales[$locale_index]; } else { $pLocale = $locale_hash{$pParameters->{locale}}; } my ($lcid,$codepage) = (@{$pLocale}[1 .. 2]); ($pGlobals->{lcid},$pGlobals->{codepage}) = ($lcid,$codepage); # Store the lcid and codepage global to the installation, for use later. my $pIni=INSTALL::ini::open($pGlobals->{chili_ini}); if ($pIni) { my $pSection = INSTALL::ini::insertSection($pIni,$pGlobals->{asphome}); if ($pSection) { $pSection->{lcid} = $lcid; $pSection->{codepage} = $codepage; INSTALL::ini::save($pIni,$pGlobals->{chili_ini}); } my $locale_cache = "$pGlobals->{asphome}/INSTALL/.locale-cache"; build_locale_cache($locale_cache); } return($lcid,$codepage); } 1;