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