* 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:
ericwa 2012-02-28 06:20:53 +00:00
parent 1f20f66738
commit e8bc5d9a5f
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>
* 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);
/**
* Returns the locale string for LC_MESSAGES
*/
GS_EXPORT NSString *GSDefaultLanguageLocale(void);
/**
* Returns a language name string for a given locale.
* e.g. GSLanguageFromLocale(@"en_CA") returns @"CanadaEnglish"

View file

@ -29,6 +29,23 @@
#import "Foundation/NSArray.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
#include <locale.h>
@ -39,51 +56,24 @@
#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) \
encoding: GSPrivateNativeCStringEncoding()]
/* Set the locale for libc functions from the supplied string or from
the environment if not specified. This function should be called
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)
static NSString *
privateSetLocale(int category, NSString *locale)
{
const char *clocale;
clocale = NULL;
const char *clocale = NULL;
if (locale != nil)
{
clocale = [locale cString];
}
clocale = GSSetLocaleC(category, clocale);
clocale = setlocale(category, clocale);
if (clocale == NULL || strcmp(clocale, "C") == 0
|| strcmp(clocale, "POSIX") == 0)
if (clocale != NULL)
{
clocale = NULL;
return ToString(clocale);
}
locale = nil;
if (clocale != 0)
{
locale = ToString(clocale);
}
return locale;
return nil;
}
#define GSLanginfo(value) ToString(nl_langinfo (value))
@ -104,6 +94,7 @@ GSDomainFromDefaultLocale(void)
int i;
NSMutableArray *arr;
#endif
NSString *backupLocale;
if (saved != nil)
return saved;
@ -115,6 +106,13 @@ GSDomainFromDefaultLocale(void)
*/
[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
/* Time/Date Information */
arr = [NSMutableArray arrayWithCapacity: 7];
@ -199,7 +197,7 @@ GSDomainFromDefaultLocale(void)
/* FIXME: Get currency format from localeconv */
#ifdef LC_MESSAGES
str1 = GSSetLocale(LC_MESSAGES, nil);
str1 = privateSetLocale(LC_MESSAGES, nil);
#else
str1 = nil;
#endif
@ -221,13 +219,20 @@ GSDomainFromDefaultLocale(void)
{
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];
return saved;
}
#else /* HAVE_LOCALE_H */
NSString *
GSSetLocale(int category, NSString *locale)
static NSString *
privateSetLocale(int category, NSString *locale)
{
return nil;
}
@ -310,3 +315,21 @@ GSLanguagesFromLocale(NSString *locale)
}
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
#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.
* 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

View file

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