質問

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