langprefs.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Determine the user's language preferences.
  2. Copyright (C) 2004-2006 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  14. USA. */
  15. /* Written by Bruno Haible <bruno@clisp.org>. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <stdlib.h>
  20. #if HAVE_CFPREFERENCESCOPYAPPVALUE
  21. # include <string.h>
  22. # include <CoreFoundation/CFPreferences.h>
  23. # include <CoreFoundation/CFPropertyList.h>
  24. # include <CoreFoundation/CFArray.h>
  25. # include <CoreFoundation/CFString.h>
  26. extern void _nl_locale_name_canonicalize (char *name);
  27. #endif
  28. /* Determine the user's language preferences, as a colon separated list of
  29. locale names in XPG syntax
  30. language[_territory][.codeset][@modifier]
  31. The result must not be freed; it is statically allocated.
  32. The LANGUAGE environment variable does not need to be considered; it is
  33. already taken into account by the caller. */
  34. const char *
  35. _nl_language_preferences_default (void)
  36. {
  37. #if HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.2 or newer */
  38. {
  39. /* Cache the preferences list, since CoreFoundation calls are expensive. */
  40. static const char *cached_languages;
  41. static int cache_initialized;
  42. if (!cache_initialized)
  43. {
  44. CFTypeRef preferences =
  45. CFPreferencesCopyAppValue (CFSTR ("AppleLanguages"),
  46. kCFPreferencesCurrentApplication);
  47. if (preferences != NULL
  48. && CFGetTypeID (preferences) == CFArrayGetTypeID ())
  49. {
  50. CFArrayRef prefArray = (CFArrayRef)preferences;
  51. int n = CFArrayGetCount (prefArray);
  52. char buf[256];
  53. size_t size = 0;
  54. int i;
  55. for (i = 0; i < n; i++)
  56. {
  57. CFTypeRef element = CFArrayGetValueAtIndex (prefArray, i);
  58. if (element != NULL
  59. && CFGetTypeID (element) == CFStringGetTypeID ()
  60. && CFStringGetCString ((CFStringRef)element,
  61. buf, sizeof (buf),
  62. kCFStringEncodingASCII))
  63. {
  64. _nl_locale_name_canonicalize (buf);
  65. size += strlen (buf) + 1;
  66. /* Most GNU programs use msgids in English and don't ship
  67. an en.mo message catalog. Therefore when we see "en"
  68. in the preferences list, arrange for gettext() to
  69. return the msgid, and ignore all further elements of
  70. the preferences list. */
  71. if (strcmp (buf, "en") == 0)
  72. break;
  73. }
  74. else
  75. break;
  76. }
  77. if (size > 0)
  78. {
  79. char *languages = (char *) malloc (size);
  80. if (languages != NULL)
  81. {
  82. char *p = languages;
  83. for (i = 0; i < n; i++)
  84. {
  85. CFTypeRef element =
  86. CFArrayGetValueAtIndex (prefArray, i);
  87. if (element != NULL
  88. && CFGetTypeID (element) == CFStringGetTypeID ()
  89. && CFStringGetCString ((CFStringRef)element,
  90. buf, sizeof (buf),
  91. kCFStringEncodingASCII))
  92. {
  93. _nl_locale_name_canonicalize (buf);
  94. strcpy (p, buf);
  95. p += strlen (buf);
  96. *p++ = ':';
  97. if (strcmp (buf, "en") == 0)
  98. break;
  99. }
  100. else
  101. break;
  102. }
  103. *--p = '\0';
  104. cached_languages = languages;
  105. }
  106. }
  107. }
  108. cache_initialized = 1;
  109. }
  110. if (cached_languages != NULL)
  111. return cached_languages;
  112. }
  113. #endif
  114. return NULL;
  115. }