1
/* Determine the user's language preferences.
2
Copyright (C) 2004-2005 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)
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,
19
/* Written by Bruno Haible <bruno@clisp.org>. */
27
#if HAVE_CFPREFERENCESCOPYAPPVALUE
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);
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. */
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);
66
for (i = 0; i < n; i++)
68
CFTypeRef element = CFArrayGetValueAtIndex (prefArray, i);
70
&& CFGetTypeID (element) == CFStringGetTypeID ()
71
&& CFStringGetCString ((CFStringRef)element,
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)
90
char *languages = (char *) malloc (size);
92
if (languages != NULL)
96
for (i = 0; i < n; i++)
99
CFArrayGetValueAtIndex (prefArray, i);
101
&& CFGetTypeID (element) == CFStringGetTypeID ()
102
&& CFStringGetCString ((CFStringRef)element,
104
kCFStringEncodingASCII))
106
_nl_locale_name_canonicalize (buf);
110
if (strcmp (buf, "en") == 0)
118
cached_languages = languages;
122
cache_initialized = 1;
124
if (cached_languages != NULL)
125
return cached_languages;