Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9ad6463

Browse files
author
Marina Polyakova
committed
ICU: fix the use of locales for collators
If the locale is bad the ICU default locale is used in the collator. But if the ICU default locale is not specified by the uloc_setDefault function, its value depends on the environment variables for the locales.
1 parent a27be48 commit 9ad6463

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

src/port/chklocale.c

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
#define FREE(s) free(s)
4141
#endif
4242

43+
#ifdef USE_ICU
44+
#define ICU_ROOT_LOCALE "root"
45+
#endif
4346

4447
/*
4548
* This table needs to recognize all the CODESET spellings for supported
@@ -712,8 +715,45 @@ UCollator *
712715
open_collator(const char *collate)
713716
{
714717
UCollator *collator;
715-
UErrorCode status = U_ZERO_ERROR;
718+
UErrorCode status;
719+
const char *save = uloc_getDefault();
720+
char *save_dup;
721+
722+
if (!save)
723+
{
724+
#ifdef FRONTEND
725+
fprintf(stderr, _("ICU error: uloc_getDefault() failed"));
726+
/* keep newline separate so there's only one translatable string */
727+
fputc('\n', stderr);
728+
#else
729+
ereport(ERROR, (errmsg("ICU error: uloc_getDefault() failed")));
730+
#endif
731+
return NULL;
732+
}
716733

734+
/* save may be pointing at a modifiable scratch variable, so copy it. */
735+
save_dup = STRDUP(save);
736+
737+
/* set the default locale to root */
738+
status = U_ZERO_ERROR;
739+
uloc_setDefault(ICU_ROOT_LOCALE, &status);
740+
if (U_FAILURE(status))
741+
{
742+
#ifdef FRONTEND
743+
fprintf(stderr, _("ICU error: failed to set the default locale to \"%s\": %s"),
744+
ICU_ROOT_LOCALE, u_errorName(status));
745+
/* keep newline separate so there's only one translatable string */
746+
fputc('\n', stderr);
747+
#else
748+
ereport(ERROR,
749+
(errmsg("ICU error: failed to set the default locale to \"%s\": %s",
750+
ICU_ROOT_LOCALE, u_errorName(status))));
751+
#endif
752+
return NULL;
753+
}
754+
755+
/* get a collator for this collate */
756+
status = U_ZERO_ERROR;
717757
collator = ucol_open(collate, &status);
718758
if (U_FAILURE(status))
719759
{
@@ -730,6 +770,25 @@ open_collator(const char *collate)
730770
collator = NULL;
731771
}
732772

773+
/* restore old value of the default locale. */
774+
status = U_ZERO_ERROR;
775+
uloc_setDefault(save_dup, &status);
776+
if (U_FAILURE(status))
777+
{
778+
#ifdef FRONTEND
779+
fprintf(stderr, _("ICU error: failed to restore old locale \"%s\": %s"),
780+
save_dup, u_errorName(status));
781+
/* keep newline separate so there's only one translatable string */
782+
fputc('\n', stderr);
783+
#else
784+
ereport(ERROR,
785+
(errmsg("ICU error: failed to restore old locale \"%s\": %s",
786+
save_dup, u_errorName(status))));
787+
#endif
788+
return NULL;
789+
}
790+
FREE(save_dup);
791+
733792
return collator;
734793
}
735794

0 commit comments

Comments
 (0)