Common::String OSystem_MacOSX::getSystemLanguage() const { #if defined(USE_DETECTLANG) && defined(USE_TRANSLATION) CFArrayRef availableLocalizations = CFBundleCopyBundleLocalizations(CFBundleGetMainBundle()); if (availableLocalizations) { CFArrayRef preferredLocalizations = CFBundleCopyPreferredLocalizationsFromArray(availableLocalizations); CFRelease(availableLocalizations); if (preferredLocalizations) { CFIndex localizationsSize = CFArrayGetCount(preferredLocalizations); // Since we have a list of sorted preferred localization, I would like here to // check that they are supported by the TranslationManager and take the first // one that is supported. The listed localizations are taken from the Bundle // plist file, so they should all be supported, unless the plist file is not // synchronized with the translations.dat file. So this is not really a big // issue. And because getSystemLanguage() is called from the constructor of // TranslationManager (therefore before the instance pointer is set), calling // TransMan here results in an infinite loop and creation of a lot of TransMan // instances. /* for (CFIndex i = 0 ; i < localizationsSize ; ++i) { CFStringRef language = (CFStringRef)CFArrayGetValueAtIndex(preferredLocalizations, i); char buffer[10]; CFStringGetCString(language, buffer, 50, kCFStringEncodingASCII); int32 languageId = TransMan.findMatchingLanguage(buffer); if (languageId != -1) { CFRelease(preferredLocalizations); return TransMan.getLangById(languageId); } } */ if (localizationsSize > 0) { CFStringRef language = (CFStringRef)CFArrayGetValueAtIndex(preferredLocalizations, 0); char buffer[10]; CFStringGetCString(language, buffer, 50, kCFStringEncodingASCII); CFRelease(preferredLocalizations); return buffer; } CFRelease(preferredLocalizations); } } // Falback to POSIX implementation return OSystem_POSIX::getSystemLanguage(); #else // USE_DETECTLANG return OSystem_POSIX::getSystemLanguage(); #endif // USE_DETECTLANG }
static const char * /* O - Locale string */ appleLangDefault(void) { int i; /* Looping var */ CFBundleRef bundle; /* Main bundle (if any) */ CFArrayRef bundleList; /* List of localizations in bundle */ CFPropertyListRef localizationList = NULL; /* List of localization data */ CFStringRef languageName; /* Current name */ CFStringRef localeName; /* Canonical from of name */ char *lang; /* LANG environment variable */ _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ DEBUG_puts("2appleLangDefault()"); /* * Only do the lookup and translation the first time. */ if (!cg->language[0]) { if (getenv("SOFTWARE") != NULL && (lang = getenv("LANG")) != NULL) { DEBUG_printf(("3appleLangDefault: Using LANG=%s", lang)); strlcpy(cg->language, lang, sizeof(cg->language)); return (cg->language); } else if ((bundle = CFBundleGetMainBundle()) != NULL && (bundleList = CFBundleCopyBundleLocalizations(bundle)) != NULL) { CFURLRef resources = CFBundleCopyResourcesDirectoryURL(bundle); DEBUG_puts("3appleLangDefault: Getting localizationList from bundle."); if (resources) { CFStringRef cfpath = CFURLCopyPath(resources); char path[1024]; if (cfpath) { /* * See if we have an Info.plist file in the bundle... */ CFStringGetCString(cfpath, path, sizeof(path), kCFStringEncodingUTF8); DEBUG_printf(("3appleLangDefault: Got a resource URL (\"%s\")", path)); strlcat(path, "Contents/Info.plist", sizeof(path)); if (!access(path, R_OK)) localizationList = CFBundleCopyPreferredLocalizationsFromArray(bundleList); else DEBUG_puts("3appleLangDefault: No Info.plist, ignoring resource URL..."); CFRelease(cfpath); } CFRelease(resources); } else DEBUG_puts("3appleLangDefault: No resource URL."); CFRelease(bundleList); } if (!localizationList) { DEBUG_puts("3appleLangDefault: Getting localizationList from preferences."); localizationList = CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), kCFPreferencesCurrentApplication); } if (localizationList) { #ifdef DEBUG if (CFGetTypeID(localizationList) == CFArrayGetTypeID()) DEBUG_printf(("3appleLangDefault: Got localizationList, %d entries.", (int)CFArrayGetCount(localizationList))); else DEBUG_puts("3appleLangDefault: Got localizationList but not an array."); #endif /* DEBUG */ if (CFGetTypeID(localizationList) == CFArrayGetTypeID() && CFArrayGetCount(localizationList) > 0) { languageName = CFArrayGetValueAtIndex(localizationList, 0); if (languageName && CFGetTypeID(languageName) == CFStringGetTypeID()) { localeName = CFLocaleCreateCanonicalLocaleIdentifierFromString( kCFAllocatorDefault, languageName); if (localeName) { CFStringGetCString(localeName, cg->language, sizeof(cg->language), kCFStringEncodingASCII); CFRelease(localeName); DEBUG_printf(("3appleLangDefault: cg->language=\"%s\"", cg->language)); /* * Map new language identifiers to locales... */ for (i = 0; i < (int)(sizeof(apple_language_locale) / sizeof(apple_language_locale[0])); i ++) { if (!strcmp(cg->language, apple_language_locale[i].language)) { DEBUG_printf(("3appleLangDefault: mapping \"%s\" to \"%s\"...", cg->language, apple_language_locale[i].locale)); strlcpy(cg->language, apple_language_locale[i].locale, sizeof(cg->language)); break; } } /* * Convert language subtag into region subtag... */ if (cg->language[2] == '-') cg->language[2] = '_'; if (!strchr(cg->language, '.')) strlcat(cg->language, ".UTF-8", sizeof(cg->language)); } else DEBUG_puts("3appleLangDefault: Unable to get localeName."); } } CFRelease(localizationList); } /* * If we didn't find the language, default to en_US... */ if (!cg->language[0]) { DEBUG_puts("3appleLangDefault: Defaulting to en_US."); strlcpy(cg->language, "en_US.UTF-8", sizeof(cg->language)); } } else DEBUG_printf(("3appleLangDefault: Using previous locale \"%s\".", cg->language)); /* * Return the cached locale... */ return (cg->language); }