Skip to content

Commit

Permalink
OSX: Get the locale for account trees from the OS instead of setlocal…
Browse files Browse the repository at this point in the history
…e().

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.
  • Loading branch information
jralls committed Mar 20, 2016
1 parent dca13d6 commit 9854876
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions src/gnome/assistant-hierarchy.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit 9854876

Please sign in to comment.