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.

有帮助吗?

解决方案

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

$ defaults read /Library/Preferences/.GlobalPreferences AppleLocale

gives the desired result.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top