If (in terminal) I type 'env', I'll see a line that looks like:
LANG=en_GB.UTF-8
And I can parse that to get the 2-char 'en' locale-code, the sub-domain 'GB' and the character-set encoding of UTF-8. All well and good.
However in a Cocoa app, I can't seem to find the equivalent for the "UTF-8" part. This is a cross-platform app, but at this point I'll go with any solution...
I've tried:
NSLocale *loc = NSLocale.currentLocale;
NSString *lang = loc.localeIdentifier;
setlocale(LC_ALL, NULL);
char *text = nl_langinfo(CODESET);
if (text)
NSString *charset = [NSString stringWithUTF8String:text];
NSLog(@"lang:%@\nchar:%@\n",lang, charset);
which displays:
lang:en-GB
char:US-ASCII
Also tried:
// Search for locale info by preferred environment variable
NSProcessInfo *pi = NSProcessInfo.processInfo;
NSDictionary<NSString *,NSString *> *env = pi.environment;
NSString *spec = env[@"LC_ALL"];
if (spec == nil)
spec = env[@"LC_CTYPE"];
if (spec == nil)
spec = env[@"LANG"];
NSLog(@"spec:%@\n", spec);
which displays:
spec:(null)
Also tried:
CFStringEncoding sys = CFStringGetSystemEncoding();
CFStringRef enc = CFStringConvertEncodingToIANACharSetName(sys);
NSString *nsEnc = (__bridge NSString *)enc;
NSLog(@"iana:%@", nsEnc);
enc = CFStringGetNameOfEncoding(sys);
nsEnc = (__bridge NSString *)enc;
NSLog(@"name:%@", nsEnc);
CFStringEncoding compat = CFStringGetMostCompatibleMacStringEncoding(sys);
enc = CFStringGetNameOfEncoding(compat);
nsEnc = (__bridge NSString *)enc;
NSLog(@"name:%@", nsEnc);
which displays:
iana:macintosh
name:Western (Mac OS Roman)
name:Western (Mac OS Roman)
Any ideas ?