On Friday, September 20, 2002, at 11:41 PM, Jens Baumeister wrote:
> Thanks for the script, the only gripe I have with it is that it doesn't
> respect the current values of $LANG and $LC_ALL
>
> Instead of hard-coding the plist values, one should probably check
> $ENV{LC_ALL} and $ENV{LANG} first and use the default ones as a
> fallback.
If you are getting locale errors running perl outside the terminal in
OSX it means $ENV{LC_ALL} hasn't been set. However you're right zapping
values set by the system isn't very neighbourly so here's the script
again adjusted accordingly:
==SCRIPT STARTS BELOW THIS TEXT==
#!/usr/bin/perl -w
# Locale WTK(water torture killer).pl
# Author Robin McFarlane<[EMAIL PROTECTED]>
#
# based on the shell script written by by Bob Dalgleish
# viewable at http://duke.usask.ca/~dalglb/macosx/Perl_5.6.html
#
# This script will look for missing folders and files in your user folder
# needed to kill locale realted errors, creating them as need be,
respecting
# any %ENV variables which have already been defined
#
# It will also safely add missing locale info to an existing
environment.plist,
# making a back-up of the original called "environment.plist.old" in the
same folder.
#=========== declare includes =============
use strict;
use diagnostics-verbose;
#========== declare variables =============
my ($TempPath,$path,$flag,$dir,$lang,$lc_all);
my (@NewPlist,@OldPlist,@Template,$TempFile,$plist);
my (%DataCheck,%MyEnv,$ChangeFlag);
$path=join ('/', '/Users',$ENV{USER});
$dir ='.MacOSX';
$plist='environment.plist';
@Template=<DATA>;
print "Checking your current environment set up\n";
$ENV{LANG}?$MyEnv{lang}=$ENV{LANG}:$MyEnv{lang}='En_US';
$ENV{LC_ALL}?$MyEnv{lc_all}=$ENV{LC_ALL}:$MyEnv{lc_all}='C';
$TempPath= join ('/',$path,$dir);
if (-e $TempPath){
print "\t",$TempPath," exists\n";
}else{
print "\t",$TempPath," is missing - creating it now\n";
mkdir $TempPath || die "can't make $TempPath,\n OS says $!";
}
$TempPath= join ('/',$TempPath,$plist);
if (-f $TempPath){
print "\t",$TempPath," exists, checking the locale data\n";
open(IN, $TempPath)|| die "can't open $TempPath,\n OS says $!";
@OldPlist=<IN>;
close (IN);
@DataCheck{@OldPlist}=();
for (@Template){
s/\$(\w*)\b/$MyEnv{$1}/g;
unless (exists $DataCheck{$_}){
print "\t$_ missing from $TempPath, adding\n";
$DataCheck{$_}=() ;
$ChangeFlag=1;
}
@NewPlist=keys(%DataCheck);
if ($ChangeFlag){
$flag = rename $TempPath,$TempPath.'.old';
die "can't seem to modify $TempFile, may be permissions
related\n" if $flag;
}
}
} else {
print "\t",$TempPath," is missing - creating it now\n";
@NewPlist=@Template;
$ChangeFlag=1;
}
if ($ChangeFlag){
open (OUT,">$TempPath") || die "can't print to $TempPath,\n OS
says $!";
print OUT @NewPlist;
close (OUT);
print "your locale data has been updated - \nany changes will come
into effect the next time you log into OSX. \n";
}else { print "No data was changed, any problems you might be
experiencing \nare not connected with your environment.plist\n"; }
__DATA__
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM
"file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>LANG</key>
<string>$lang</string>
<key>LC_ALL</key>
<string>$lc_all</string>
</dict>
</plist>
==SCRIPT STARTS ABOVE THIS TEXT==