* Source/GSLocale.m:

- Deprecate GSSetLocale and GSSetLocaleC; they now do nothing
   but print a warning.
 - Introduce GSDefaultLanguageLocale(), which is a
   substitute for GSSetLocale(LC_MESSAGES, nil), which just returns the
   current setting of LC_MESSAGES.
* Source/NSObject.m (+initialize): Remove GSSetLocaleC call,
which was changing the libc locale.
* Source/NSUserDefaults.m: Use GSDefaultLanguageLocale() instead of
GSSetLocale(LC_MESSAGES, nil)


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34841 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Eric Wasylishen 2012-02-28 06:20:53 +00:00
parent f745fa90d7
commit 4db79e6fef
5 changed files with 81 additions and 57 deletions

View file

@ -1,3 +1,16 @@
2012-02-27 Eric Wasylishen <ewasylishen@gmail.com>
* Source/GSLocale.m:
- Deprecate GSSetLocale and GSSetLocaleC; they now do nothing
but print a warning.
- Introduce GSDefaultLanguageLocale(), which is a
substitute for GSSetLocale(LC_MESSAGES, nil), which just returns the
current setting of LC_MESSAGES.
* Source/NSObject.m (+initialize): Remove GSSetLocaleC call,
which was changing the libc locale.
* Source/NSUserDefaults.m: Use GSDefaultLanguageLocale() instead of
GSSetLocale(LC_MESSAGES, nil)
2012-02-27 Eric Wasylishen <ewasylishen@gmail.com> 2012-02-27 Eric Wasylishen <ewasylishen@gmail.com>
* Source/NSArchiver.m (-resetArchiver): Write [self systemVersion] as * Source/NSArchiver.m (-resetArchiver): Write [self systemVersion] as

View file

@ -44,6 +44,11 @@ GS_EXPORT NSString *GSSetLocale(int category, NSString *locale);
GS_EXPORT NSDictionary *GSDomainFromDefaultLocale(void); GS_EXPORT NSDictionary *GSDomainFromDefaultLocale(void);
/**
* Returns the locale string for LC_MESSAGES
*/
GS_EXPORT NSString *GSDefaultLanguageLocale(void);
/** /**
* Returns a language name string for a given locale. * Returns a language name string for a given locale.
* e.g. GSLanguageFromLocale(@"en_CA") returns @"CanadaEnglish" * e.g. GSLanguageFromLocale(@"en_CA") returns @"CanadaEnglish"

View file

@ -29,6 +29,23 @@
#import "Foundation/NSArray.h" #import "Foundation/NSArray.h"
#import "Foundation/NSLock.h" #import "Foundation/NSLock.h"
static NSString *
privateSetLocale(int category, NSString *locale);
const char*
GSSetLocaleC(int category, const char *loc)
{
NSWarnLog(@"GSSetLocaleC is deprecated and has no effect");
return NULL;
}
NSString *
GSSetLocale(int category, NSString *locale)
{
NSWarnLog(@"GSSetLocale is deprecated and has no effect");
return nil;
}
#ifdef HAVE_LOCALE_H #ifdef HAVE_LOCALE_H
#include <locale.h> #include <locale.h>
@ -39,51 +56,24 @@
#import "GSPrivate.h" #import "GSPrivate.h"
/*
* Function called by [NSObject +initialize] to setup locale information
* from environment variables. Must *not* use any ObjC code since it needs
* to run before any ObjC classes are fully initialised so that they can
* make use of locale information.
*/
const char*
GSSetLocaleC(int category, const char *loc)
{
return setlocale(category, loc);
}
#define ToString(value) [NSString stringWithCString: (value) \ #define ToString(value) [NSString stringWithCString: (value) \
encoding: GSPrivateNativeCStringEncoding()] encoding: GSPrivateNativeCStringEncoding()]
/* Set the locale for libc functions from the supplied string or from static NSString *
the environment if not specified. This function should be called privateSetLocale(int category, NSString *locale)
as soon as possible after the start of the program. Passing
@"" will set the locale from the environment variables LC_CTYPE or LANG (or
whatever is specified by setlocale) Passing nil will just return the
current locale. */
NSString *
GSSetLocale(int category, NSString *locale)
{ {
const char *clocale; const char *clocale = NULL;
clocale = NULL;
if (locale != nil) if (locale != nil)
{ {
clocale = [locale cString]; clocale = [locale cString];
} }
clocale = GSSetLocaleC(category, clocale); clocale = setlocale(category, clocale);
if (clocale == NULL || strcmp(clocale, "C") == 0 if (clocale != NULL)
|| strcmp(clocale, "POSIX") == 0)
{ {
clocale = NULL; return ToString(clocale);
} }
return nil;
locale = nil;
if (clocale != 0)
{
locale = ToString(clocale);
}
return locale;
} }
#define GSLanginfo(value) ToString(nl_langinfo (value)) #define GSLanginfo(value) ToString(nl_langinfo (value))
@ -104,6 +94,7 @@ GSDomainFromDefaultLocale(void)
int i; int i;
NSMutableArray *arr; NSMutableArray *arr;
#endif #endif
NSString *backupLocale;
if (saved != nil) if (saved != nil)
return saved; return saved;
@ -115,6 +106,13 @@ GSDomainFromDefaultLocale(void)
*/ */
[gnustep_global_lock lock]; [gnustep_global_lock lock];
/**
* Set the current locale to the system default, and backup
* what it was previously (should have been @"C").
*/
backupLocale = privateSetLocale(LC_ALL, nil);
privateSetLocale(LC_ALL, @"");
#ifdef HAVE_LANGINFO_H #ifdef HAVE_LANGINFO_H
/* Time/Date Information */ /* Time/Date Information */
arr = [NSMutableArray arrayWithCapacity: 7]; arr = [NSMutableArray arrayWithCapacity: 7];
@ -199,7 +197,7 @@ GSDomainFromDefaultLocale(void)
/* FIXME: Get currency format from localeconv */ /* FIXME: Get currency format from localeconv */
#ifdef LC_MESSAGES #ifdef LC_MESSAGES
str1 = GSSetLocale(LC_MESSAGES, nil); str1 = privateSetLocale(LC_MESSAGES, nil);
#else #else
str1 = nil; str1 = nil;
#endif #endif
@ -221,13 +219,20 @@ GSDomainFromDefaultLocale(void)
{ {
saved = [NSObject leak: dict]; saved = [NSObject leak: dict];
} }
/**
* Restore the current locale to what we backed up (again, should
* be restored to @"C")
*/
privateSetLocale(LC_ALL, backupLocale);
[gnustep_global_lock unlock]; [gnustep_global_lock unlock];
return saved; return saved;
} }
#else /* HAVE_LOCALE_H */ #else /* HAVE_LOCALE_H */
NSString * static NSString *
GSSetLocale(int category, NSString *locale) privateSetLocale(int category, NSString *locale)
{ {
return nil; return nil;
} }
@ -310,3 +315,21 @@ GSLanguagesFromLocale(NSString *locale)
} }
return result; return result;
} }
NSString *GSDefaultLanguageLocale()
{
NSString *backup, *locale = nil;
#ifdef HAVE_LOCALE_H
[gnustep_global_lock lock];
backup = privateSetLocale(LC_ALL, nil);
privateSetLocale(LC_ALL, @"");
locale = privateSetLocale(LC_MESSAGES, nil);
privateSetLocale(LC_ALL, backup);
[gnustep_global_lock unlock];
#endif
return locale;
}

View file

@ -1098,15 +1098,6 @@ static id gs_weak_load(id obj)
# endif # endif
#endif #endif
#ifdef HAVE_LOCALE_H
/* Set up locale from environment.
* This function should not use any ObjC code since important
* classes are not yet initialized.
*/
GSSetLocaleC(LC_ALL, "");
#endif
/* Create the global lock. /* Create the global lock.
* NB. Ths is one of the first things we do ... setting up a new lock * NB. Ths is one of the first things we do ... setting up a new lock
* must not call any other Objective-C classes and must not involve * must not call any other Objective-C classes and must not involve

View file

@ -353,19 +353,16 @@ systemLanguages()
} }
// If LANGUAGES did not yield any languages, try LC_MESSAGES // If LANGUAGES did not yield any languages, try LC_MESSAGES
#ifdef HAVE_LOCALE_H
#ifdef LC_MESSAGES
if ([names count] == 0) if ([names count] == 0)
{ {
NSString *locale = GSSetLocale(LC_MESSAGES, nil); NSString *locale = GSDefaultLanguageLocale();
if (locale != nil) if (locale != nil)
{ {
[names addObjectsFromArray: GSLanguagesFromLocale(locale)]; [names addObjectsFromArray: GSLanguagesFromLocale(locale)];
} }
} }
#endif
#endif
return names; return names;
} }
@ -994,13 +991,8 @@ newLanguages(NSArray *oldNames)
* precisely because it is the currently set locale in the * precisely because it is the currently set locale in the
* C library. * C library.
*/ */
NSString *locale = nil; NSString *locale = GSDefaultLanguageLocale();
#ifdef HAVE_LOCALE_H
#ifdef LC_MESSAGES
locale = GSSetLocale(LC_MESSAGES, nil);
#endif
#endif
if (locale != nil) if (locale != nil)
{ {
NSString *i18n = GSLanguageFromLocale(locale); NSString *i18n = GSLanguageFromLocale(locale);