diff options
Diffstat (limited to 'gnulib-tests/localename.c')
-rw-r--r-- | gnulib-tests/localename.c | 86 |
1 files changed, 42 insertions, 44 deletions
diff --git a/gnulib-tests/localename.c b/gnulib-tests/localename.c index 96f4a20..20be646 100644 --- a/gnulib-tests/localename.c +++ b/gnulib-tests/localename.c @@ -1,5 +1,5 @@ /* Determine name of the currently selected locale. - Copyright (C) 1995-2018 Free Software Foundation, Inc. + Copyright (C) 1995-2020 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -34,6 +34,7 @@ #include <string.h> #include "flexmember.h" +#include "setlocale_null.h" /* We cannot support uselocale() on platforms where the locale_t type is fake. See intl-thread-locale.m4 for details. */ @@ -66,13 +67,9 @@ extern char * getlocalename_l(int, locale_t); # endif #endif -#if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE +#if HAVE_CFPREFERENCESCOPYAPPVALUE # include <CoreFoundation/CFString.h> -# if HAVE_CFLOCALECOPYCURRENT -# include <CoreFoundation/CFLocale.h> -# elif HAVE_CFPREFERENCESCOPYAPPVALUE -# include <CoreFoundation/CFPreferences.h> -# endif +# include <CoreFoundation/CFPreferences.h> #endif #if defined _WIN32 && !defined __CYGWIN__ @@ -1155,8 +1152,12 @@ extern char * getlocalename_l(int, locale_t); # endif #endif +/* We want to use the system's setlocale() function here, not the gnulib + override. */ +#undef setlocale + -#if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE +#if HAVE_CFPREFERENCESCOPYAPPVALUE /* Mac OS X 10.4 or newer */ /* Canonicalize a Mac OS X locale name to a Unix locale name. @@ -2296,8 +2297,8 @@ gl_locale_name_from_win32_LANGID (LANGID langid) } return "wen"; case LANG_SOTHO: - /* <https://msdn.microsoft.com/en-us/library/dd318693.aspx> calls - it "Sesotho sa Leboa"; according to + /* <https://docs.microsoft.com/en-us/windows/desktop/Intl/language-identifier-constants-and-strings> + calls it "Sesotho sa Leboa"; according to <https://www.ethnologue.com/show_language.asp?code=nso> <https://www.ethnologue.com/show_language.asp?code=sot> it's the same as Northern Sotho. */ @@ -2729,7 +2730,7 @@ get_locale_t_name (int category, locale_t locale) if (locale == LC_GLOBAL_LOCALE) { /* Query the global locale. */ - const char *name = setlocale (category, NULL); + const char *name = setlocale_null (category); if (name != NULL) return struniq (name); else @@ -3191,6 +3192,8 @@ gl_locale_name_thread_unsafe (int category, const char *categoryname) return ""; } # endif +# elif defined _AIX && HAVE_NAMELESS_LOCALES + return get_locale_t_name (category, thread_locale); # elif defined __CYGWIN__ /* Cygwin < 2.6 lacks uselocale and thread-local locales altogether. Cygwin <= 2.6.1 lacks NL_LOCALE_NAME, requiring peeking inside @@ -3245,20 +3248,15 @@ gl_locale_name_posix (int category, const char *categoryname) #if defined WINDOWS_NATIVE if (LC_MIN <= category && category <= LC_MAX) { - const char *locname = setlocale (category, NULL); - LCID lcid; - - /* If CATEGORY is LC_ALL, the result might be a semi-colon - separated list of locales. We need only one, so we take the - one corresponding to LC_CTYPE, as the most important for - character translations. */ - if (category == LC_ALL && strchr (locname, ';')) - locname = setlocale (LC_CTYPE, NULL); + const char *locname = + /* setlocale_null (category) is identical to setlocale (category, NULL) + on this platform. */ + setlocale (category, NULL); /* Convert locale name to LCID. We don't want to use LocaleNameToLCID because (a) it is only available since Vista, and (b) it doesn't accept locale names returned by 'setlocale'. */ - lcid = get_lcid (locname); + LCID lcid = get_lcid (locname); if (lcid > 0) return gl_locale_name_from_win32_LCID (lcid); @@ -3269,8 +3267,8 @@ gl_locale_name_posix (int category, const char *categoryname) /* Use the POSIX methods of looking to 'LC_ALL', 'LC_xxx', and 'LANG'. On some systems this can be done by the 'setlocale' function itself. */ -#if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL - locname = setlocale (category, NULL); +#if defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL + locname = setlocale_null (category); #else /* On other systems we ignore what setlocale reports and instead look at the environment variables directly. This is necessary @@ -3294,15 +3292,16 @@ gl_locale_name_posix (int category, const char *categoryname) /* Convert the locale name from the format returned by setlocale() or found in the environment variables to the XPG syntax. */ #if defined WINDOWS_NATIVE - { - /* Convert locale name to LCID. We don't want to use - LocaleNameToLCID because (a) it is only available since Vista, - and (b) it doesn't accept locale names returned by 'setlocale'. */ - LCID lcid = get_lcid (locname); + if (locname != NULL) + { + /* Convert locale name to LCID. We don't want to use + LocaleNameToLCID because (a) it is only available since Vista, + and (b) it doesn't accept locale names returned by 'setlocale'. */ + LCID lcid = get_lcid (locname); - if (lcid > 0) - return gl_locale_name_from_win32_LCID (lcid); - } + if (lcid > 0) + return gl_locale_name_from_win32_LCID (lcid); + } #endif return locname; } @@ -3325,7 +3324,7 @@ gl_locale_name_environ (int category, const char *categoryname) retval = getenv ("LANG"); if (retval != NULL && retval[0] != '\0') { -#if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE +#if HAVE_CFPREFERENCESCOPYAPPVALUE /* Mac OS X 10.2 or newer. Ignore invalid LANG value set by the Terminal application. */ if (strcmp (retval, "UTF-8") != 0) @@ -3372,7 +3371,7 @@ gl_locale_name_default (void) "C.UTF-8" locale, which operates in the same way as the "C" locale. */ -#if !(HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE || defined WINDOWS_NATIVE || defined __CYGWIN__) +#if !(HAVE_CFPREFERENCESCOPYAPPVALUE || defined WINDOWS_NATIVE || defined __CYGWIN__) /* The system does not have a way of setting the locale, other than the POSIX specified environment variables. We use C as default locale. */ @@ -3385,8 +3384,17 @@ gl_locale_name_default (void) context, because message catalogs are not specific to a single codeset. */ -# if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE +# if HAVE_CFPREFERENCESCOPYAPPVALUE /* Mac OS X 10.4 or newer */ + /* Don't use the API introduced in Mac OS X 10.5, CFLocaleCopyCurrent, + because in macOS 10.13.4 it has the following behaviour: + When two or more languages are specified in the + "System Preferences > Language & Region > Preferred Languages" panel, + it returns en_CC where CC is the territory (even when English is not among + the preferred languages!). What we want instead is what + CFLocaleCopyCurrent returned in earlier macOS releases and what + CFPreferencesCopyAppValue still returns, namely ll_CC where ll is the + first among the preferred languages and CC is the territory. */ { /* Cache the locale name, since CoreFoundation calls are expensive. */ static const char *cached_localename; @@ -3394,17 +3402,12 @@ gl_locale_name_default (void) if (cached_localename == NULL) { char namebuf[256]; -# if HAVE_CFLOCALECOPYCURRENT /* Mac OS X 10.5 or newer */ - CFLocaleRef locale = CFLocaleCopyCurrent (); - CFStringRef name = CFLocaleGetIdentifier (locale); -# elif HAVE_CFPREFERENCESCOPYAPPVALUE /* Mac OS X 10.4 or newer */ CFTypeRef value = CFPreferencesCopyAppValue (CFSTR ("AppleLocale"), kCFPreferencesCurrentApplication); if (value != NULL && CFGetTypeID (value) == CFStringGetTypeID ()) { CFStringRef name = (CFStringRef)value; -# endif if (CFStringGetCString (name, namebuf, sizeof (namebuf), kCFStringEncodingASCII)) @@ -3412,12 +3415,7 @@ gl_locale_name_default (void) gl_locale_name_canonicalize (namebuf); cached_localename = strdup (namebuf); } - -# if HAVE_CFLOCALECOPYCURRENT /* Mac OS X 10.5 or newer */ - CFRelease (locale); -# elif HAVE_CFPREFERENCESCOPYAPPVALUE /* Mac OS X 10.4 or newer */ } -# endif if (cached_localename == NULL) cached_localename = "C"; } |