macOS: infer primary language if $LANG is empty #9345

The macOS preferences have a section called `Language & Region`. There is always
at least one language defined, the primary language.

CFLocaleCopyPreferredLanguages() returns the languages defined in that section,
the first element being the primary language.

Use the primary language in case CFLocaleCopyCurrent() returns NULL.

In the case that the above fallback does not work either, which is very
unlikely, log the error and continue with an empty $LANG.

References #9134
This commit is contained in:
Marco Hinz 2018-12-11 21:58:35 +01:00 committed by Justin M. Keyes
parent 5a90761e8a
commit 57acfceabe
1 changed files with 24 additions and 7 deletions

View File

@ -17,14 +17,31 @@ void lang_init(void)
{
#ifdef __APPLE__
if (os_getenv("LANG") == NULL) {
CFLocaleRef cf_locale = CFLocaleCopyCurrent();
CFTypeRef cf_lang_region = CFLocaleGetValue(cf_locale,
kCFLocaleIdentifier);
CFRetain(cf_lang_region);
CFRelease(cf_locale);
const char *lang_region = NULL;
CFTypeRef cf_lang_region = NULL;
CFLocaleRef cf_locale = CFLocaleCopyCurrent();
if (cf_locale) {
cf_lang_region = CFLocaleGetValue(cf_locale, kCFLocaleIdentifier);
CFRetain(cf_lang_region);
lang_region = CFStringGetCStringPtr(cf_lang_region,
kCFStringEncodingUTF8);
CFRelease(cf_locale);
} else {
// Use the primary language defined in Preferences -> Language & Region
CFArrayRef cf_langs = CFLocaleCopyPreferredLanguages();
if (cf_langs && CFArrayGetCount(cf_langs) > 0) {
cf_lang_region = CFArrayGetValueAtIndex(cf_langs, 0);
CFRetain(cf_lang_region);
CFRelease(cf_langs);
lang_region = CFStringGetCStringPtr(cf_lang_region,
kCFStringEncodingUTF8);
} else {
ELOG("$LANG is empty and your primary language cannot be inferred.");
return;
}
}
const char *lang_region = CFStringGetCStringPtr(cf_lang_region,
kCFStringEncodingUTF8);
if (lang_region) {
os_setenv("LANG", lang_region, true);
} else {