OSX: Get the locale for account trees from the OS instead of setlocale().

It's possible for OSX to create locales that while legal aren't
supported by setlocale, and we have account trees for some of these.
Retrieving the locale from NSLocale ignores the fixup done in
gnucash-bin to ensure that a reasonable and supported locale is used.
This commit is contained in:
John Ralls
2016-03-20 15:11:06 -07:00
parent dca13d6248
commit 9854876c70

View File

@@ -31,6 +31,10 @@
#include <sys/types.h>
#include <unistd.h>
#ifdef PLATFORM_OSX
#include <Foundation/Foundation.h>
#endif
#include "gnc-account-merge.h"
#include "dialog-new-user.h"
#include "dialog-options.h"
@@ -182,6 +186,36 @@ set_final_balance (GHashTable *hash, Account *account, gnc_numeric in_balance)
g_hash_table_insert (hash, account, balance);
}
#ifdef PLATFORM_OSX
/* Repeat retrieving the locale from defaults in case it was overridden in
* gnucash-bin because it wasn't a supported POSIX locale.
*/
static char*
mac_locale()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLocale* locale = [NSLocale currentLocale];
NSString* locale_str;
char *retval = NULL;
@try
{
locale_str =[[[locale objectForKey: NSLocaleLanguageCode]
stringByAppendingString: @"_"]
stringByAppendingString:
[locale objectForKey: NSLocaleCountryCode]];
}
@catch (NSException *err)
{
locale_str = @"_";
}
/* If we didn't get a valid current locale, the string will be just "_" */
if ([locale_str isEqualToString: @"_"])
locale_str = @"en_US";
retval = g_strdup([locale_str UTF8String]);
[pool drain];
return retval;
}
#endif
static gchar*
gnc_get_ea_locale_dir(const char *top_dir)
{
@@ -191,10 +225,7 @@ gnc_get_ea_locale_dir(const char *top_dir)
struct stat buf;
int i;
#ifdef HAVE_LC_MESSAGES
locale = g_strdup(setlocale(LC_MESSAGES, NULL));
#else
# ifdef G_OS_WIN32
#ifdef PLATFORM_WIN32
/* On win32, setlocale() doesn't say anything useful. Use
glib's function instead. */
locale = g_win32_getlocale();
@@ -203,14 +234,10 @@ gnc_get_ea_locale_dir(const char *top_dir)
PWARN ("Couldn't retrieve locale. Falling back to default one.");
locale = g_strdup ("C");
}
#elif defined PLATFORM_OSX
locale = mac_locale();
# else
/*
* Mac OS X 10.1 and earlier, not only doesn't have LC_MESSAGES
* setlocale can sometimes return NULL instead of "C"
*/
locale = g_strdup(setlocale(LC_ALL, NULL) ?
setlocale(LC_ALL, NULL) : "C");
# endif
locale = g_strdup(setlocale(LC_MESSAGES, NULL));
#endif
i = strlen(locale);