diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index bae97539fc8..0acfd0fea29 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -2243,46 +2243,44 @@ check_icu_locale_encoding(int user_enc) } /* - * Check that ICU accepts the locale name; or if not specified, retrieve the - * default ICU locale. + * Determine default ICU locale by opening the default collator and reading + * its locale. + * + * NB: The default collator (opened using NULL) is different from the collator + * for the root locale (opened with "", "und", or "root"). The former depends + * on the environment (useful at initdb time) and the latter does not. */ -static void -check_icu_locale(void) +static char * +default_icu_locale(void) { #ifdef USE_ICU UCollator *collator; UErrorCode status; + const char *valid_locale; + char *default_locale; status = U_ZERO_ERROR; - collator = ucol_open(icu_locale, &status); + collator = ucol_open(NULL, &status); + if (U_FAILURE(status)) + pg_fatal("could not open collator for default locale: %s", + u_errorName(status)); + + status = U_ZERO_ERROR; + valid_locale = ucol_getLocaleByType(collator, ULOC_VALID_LOCALE, + &status); if (U_FAILURE(status)) { - if (icu_locale) - pg_fatal("could not open collator for locale \"%s\": %s", - icu_locale, u_errorName(status)); - else - pg_fatal("could not open collator for default locale: %s", - u_errorName(status)); + ucol_close(collator); + pg_fatal("could not determine default ICU locale"); } - /* if not specified, get locale from default collator */ - if (icu_locale == NULL) - { - const char *default_locale; - - status = U_ZERO_ERROR; - default_locale = ucol_getLocaleByType(collator, ULOC_VALID_LOCALE, - &status); - if (U_FAILURE(status)) - { - ucol_close(collator); - pg_fatal("could not determine default ICU locale"); - } - - icu_locale = pg_strdup(default_locale); - } + default_locale = pg_strdup(valid_locale); ucol_close(collator); + + return default_locale; +#else + pg_fatal("ICU is not supported in this build"); #endif } @@ -2339,7 +2337,9 @@ setlocales(void) if (locale_provider == COLLPROVIDER_ICU) { - check_icu_locale(); + /* acquire default locale from the environment, if not specified */ + if (icu_locale == NULL) + icu_locale = default_icu_locale(); /* * In supported builds, the ICU locale ID will be checked by the