40
40
#define FREE (s ) free(s)
41
41
#endif
42
42
43
+ #ifdef USE_ICU
44
+ #define ICU_ROOT_LOCALE "root"
45
+ #endif
43
46
44
47
/*
45
48
* This table needs to recognize all the CODESET spellings for supported
@@ -712,8 +715,45 @@ UCollator *
712
715
open_collator (const char * collate )
713
716
{
714
717
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
+ }
716
733
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 ;
717
757
collator = ucol_open (collate , & status );
718
758
if (U_FAILURE (status ))
719
759
{
@@ -730,6 +770,25 @@ open_collator(const char *collate)
730
770
collator = NULL ;
731
771
}
732
772
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
+
733
792
return collator ;
734
793
}
735
794
0 commit comments