Question

I am running as root, but have the user's uid (e.g. 504). How can I work out the user's locale (in my case en_GB)? The following does not work:

setuid(user_uid);
fprintf(stderr,
        CFStringGetCStringPtr(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
                              kCFStringEncodingMacRoman);
setuid(0);

This outputs en_US for me.

Était-ce utile?

La solution

This information is contained in GlobalPreferences.plist, so running:

$ defaults read /Library/Preferences/.GlobalPreferences AppleLocale

gives the desired result.

Autres conseils

You can't, because it doesn't exist. The locale is controled by environment variables, and can change dynamically, and from window to window and even from application to application (i.e. if the user started a program with:

env LC_LANG=fr_FR program_name ...

.) Under Unix, you might be able to get user's default locale by doing something like:

FILE* in = popen( "su -c 'env | grep ^LC_ ; env | grep ^LANG' - user", "r" );

, then reading and parsing the input, but I don't think that there's anything simpler.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top