root/trunk/intl/langprefs.c

Revision 2, 3.7 kB (checked in by jordi, 2 years ago)

Added the initial source layout with autotools and gettext support. Still no code for the project.

Line 
1/* Determine the user's language preferences.
2   Copyright (C) 2004-2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17   USA.  */
18
19/* Written by Bruno Haible <bruno@clisp.org>.  */
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>
33extern void _nl_locale_name_canonicalize (char *name);
34#endif
35
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.  */
42
43const char *
44_nl_language_preferences_default (void)
45{
46#if HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.2 or newer */
47  {
48    /* Cache the preferences list, since CoreFoundation calls are expensive.  */
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            /* 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;
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}
Note: See TracBrowser for help on using the browser.