| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #ifdef HAVE_CONFIG_H |
|---|
| 22 | # include <config.h> |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | |
|---|
| 27 | #if HAVE_CFPREFERENCESCOPYAPPVALUE |
|---|
| 28 | # include <string.h> |
|---|
| 29 | # include <CFPreferences.h> |
|---|
| 30 | # include <CFPropertyList.h> |
|---|
| 31 | # include <CFArray.h> |
|---|
| 32 | # include <CFString.h> |
|---|
| 33 | extern void _nl_locale_name_canonicalize (char *name); |
|---|
| 34 | #endif |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | const char * |
|---|
| 44 | _nl_language_preferences_default (void) |
|---|
| 45 | { |
|---|
| 46 | #if HAVE_CFPREFERENCESCOPYAPPVALUE |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | static const char *cached_languages; |
|---|
| 50 | static int cache_initialized; |
|---|
| 51 | |
|---|
| 52 | if (!cache_initialized) |
|---|
| 53 | { |
|---|
| 54 | CFTypeRef preferences = |
|---|
| 55 | CFPreferencesCopyAppValue (CFSTR ("AppleLanguages"), |
|---|
| 56 | kCFPreferencesCurrentApplication); |
|---|
| 57 | if (preferences != NULL |
|---|
| 58 | && CFGetTypeID (preferences) == CFArrayGetTypeID ()) |
|---|
| 59 | { |
|---|
| 60 | CFArrayRef prefArray = (CFArrayRef)preferences; |
|---|
| 61 | int n = CFArrayGetCount (prefArray); |
|---|
| 62 | char buf[256]; |
|---|
| 63 | size_t size = 0; |
|---|
| 64 | int i; |
|---|
| 65 | |
|---|
| 66 | for (i = 0; i < n; i++) |
|---|
| 67 | { |
|---|
| 68 | CFTypeRef element = CFArrayGetValueAtIndex (prefArray, i); |
|---|
| 69 | if (element != NULL |
|---|
| 70 | && CFGetTypeID (element) == CFStringGetTypeID () |
|---|
| 71 | && CFStringGetCString ((CFStringRef)element, |
|---|
| 72 | buf, sizeof (buf), |
|---|
| 73 | kCFStringEncodingASCII)) |
|---|
| 74 | { |
|---|
| 75 | _nl_locale_name_canonicalize (buf); |
|---|
| 76 | size += strlen (buf) + 1; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | if (strcmp (buf, "en") == 0) |
|---|
| 83 | break; |
|---|
| 84 | } |
|---|
| 85 | else |
|---|
| 86 | break; |
|---|
| 87 | } |
|---|
| 88 | if (size > 0) |
|---|
| 89 | { |
|---|
| 90 | char *languages = (char *) malloc (size); |
|---|
| 91 | |
|---|
| 92 | if (languages != NULL) |
|---|
| 93 | { |
|---|
| 94 | char *p = languages; |
|---|
| 95 | |
|---|
| 96 | for (i = 0; i < n; i++) |
|---|
| 97 | { |
|---|
| 98 | CFTypeRef element = |
|---|
| 99 | CFArrayGetValueAtIndex (prefArray, i); |
|---|
| 100 | if (element != NULL |
|---|
| 101 | && CFGetTypeID (element) == CFStringGetTypeID () |
|---|
| 102 | && CFStringGetCString ((CFStringRef)element, |
|---|
| 103 | buf, sizeof (buf), |
|---|
| 104 | kCFStringEncodingASCII)) |
|---|
| 105 | { |
|---|
| 106 | _nl_locale_name_canonicalize (buf); |
|---|
| 107 | strcpy (p, buf); |
|---|
| 108 | p += strlen (buf); |
|---|
| 109 | *p++ = ':'; |
|---|
| 110 | if (strcmp (buf, "en") == 0) |
|---|
| 111 | break; |
|---|
| 112 | } |
|---|
| 113 | else |
|---|
| 114 | break; |
|---|
| 115 | } |
|---|
| 116 | *--p = '\0'; |
|---|
| 117 | |
|---|
| 118 | cached_languages = languages; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | cache_initialized = 1; |
|---|
| 123 | } |
|---|
| 124 | if (cached_languages != NULL) |
|---|
| 125 | return cached_languages; |
|---|
| 126 | } |
|---|
| 127 | #endif |
|---|
| 128 | |
|---|
| 129 | return NULL; |
|---|
| 130 | } |
|---|