Skip to content

Commit

Permalink
ICU-21316 Fix bug under different default locales
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Oct 8, 2020
1 parent 5de5cab commit f1a8a63
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
5 changes: 4 additions & 1 deletion icu4c/source/common/locid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,10 @@ AliasReplacer::replaceTerritory(UVector& toBeFreed, UErrorCode& status)
if (firstSpace != nullptr) {
// If there are are more than one region in the replacement.
// We need to check which one match based on the language.
Locale l(language, nullptr, script);
// Cannot use nullptr for language because that will construct
// the default locale, in that case, use "und" to get the correct
// locale.
Locale l(language == nullptr ? "und" : language, nullptr, script);
l.addLikelySubtags(status);
const char* likelyRegion = l.getCountry();
CharString* item = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/i18n/ulocdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ ulocdata_getDelimiter(ULocaleData *uld, ULocaleDataDelimiterType type,
return 0;
}

delimiter = ures_getStringByKey(delimiterBundle, delimiterKeys[type], &len, &localStatus);
delimiter = ures_getStringByKeyWithFallback(delimiterBundle, delimiterKeys[type], &len, &localStatus);
ures_close(delimiterBundle);

if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
Expand Down
14 changes: 12 additions & 2 deletions icu4c/source/test/cintltst/cloctst.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,19 @@ static void TestDisplayNames()
/* test that the default locale has a display name for its own language */
errorCode=U_ZERO_ERROR;
length=uloc_getDisplayLanguage(NULL, NULL, buffer, UPRV_LENGTHOF(buffer), &errorCode);
/* check <=3 to reject getting the language code as a display name */
if(U_FAILURE(errorCode) || (length<=3 && buffer[0]<=0x7f)) {
/* check <=3 to reject getting the language code as a display name */
log_data_err("unable to get a display string for the language of the default locale - %s (Are you missing data?)\n", u_errorName(errorCode));
const char* defaultLocale = uloc_getDefault();
for (int32_t i = 0, count = uloc_countAvailable(); i < count; i++) {
/* Only report error if the default locale is in the available list */
if (uprv_strcmp(defaultLocale, uloc_getAvailable(i)) == 0) {
log_data_err(
"unable to get a display string for the language of the "
"default locale - %s (Are you missing data?)\n",
u_errorName(errorCode));
break;
}
}
}

/* test that we get the language code itself for an unknown language, and a default warning */
Expand Down
20 changes: 16 additions & 4 deletions icu4c/source/test/cintltst/crestst.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ void TestAliasConflict(void) {

void TestResourceBundles()
{
// The test expectation only works if the default locale is not one of the
// locale bundle in the testdata which have those info. Therefore, we skip
// the test if the default locale is te, sh, mc, or them with subtags.
if ( uprv_strncmp(uloc_getDefault(), "te", 2) == 0 ||
uprv_strncmp(uloc_getDefault(), "sh", 2) == 0 ||
uprv_strncmp(uloc_getDefault(), "mc", 2) == 0) {
return;
}

UErrorCode status = U_ZERO_ERROR;
loadTestData(&status);
if(U_FAILURE(status)) {
Expand Down Expand Up @@ -595,16 +604,19 @@ TestOpenDirect(void) {
ures_close(casing);

/*
* verify that ures_open("ne") finds the root bundle but
* ures_openDirect("ne") does not
* verify that ures_open("ne") finds the root bundle or default locale
* bundle but ures_openDirect("ne") does not.
*/
errorCode=U_ZERO_ERROR;
ne=ures_open("testdata", "ne", &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("ures_open(\"ne\") failed (expected to get root): %s\n", u_errorName(errorCode));
}
if(errorCode!=U_USING_DEFAULT_WARNING || 0!=uprv_strcmp("root", ures_getLocale(ne, &errorCode))) {
log_err("ures_open(\"ne\") found something other than \"root\" - %s\n", u_errorName(errorCode));
if( errorCode!=U_USING_DEFAULT_WARNING ||
(0!=uprv_strcmp("root", ures_getLocale(ne, &errorCode)) &&
0!=uprv_strcmp(uloc_getDefault(), ures_getLocale(ne, &errorCode)))) {
log_err("ures_open(\"ne\") found something other than \"root\" "
"or default locale \"%s\" - %s\n", uloc_getDefault(), u_errorName(errorCode));
}
ures_close(ne);

Expand Down
18 changes: 18 additions & 0 deletions icu4c/source/test/cintltst/creststn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,15 @@ static void TestGetVersionColl(){

static void TestResourceBundles()
{
// The test expectation only works if the default locale is not one of the
// locale bundle in the testdata which have those info. Therefore, we skip
// the test if the default locale is te, sh, mc, or them with subtags.
if ( uprv_strncmp(uloc_getDefault(), "te", 2) == 0 ||
uprv_strncmp(uloc_getDefault(), "sh", 2) == 0 ||
uprv_strncmp(uloc_getDefault(), "mc", 2) == 0) {
return;
}

UErrorCode status = U_ZERO_ERROR;
loadTestData(&status);
if(U_FAILURE(status)) {
Expand All @@ -1532,6 +1541,15 @@ static void TestResourceBundles()

static void TestConstruction1()
{
// The test expectation only works if the default locale is not one of the
// locale bundle in the testdata which have those info. Therefore, we skip
// the test if the default locale is te, sh, mc, or them with subtags.
if ( uprv_strncmp(uloc_getDefault(), "te", 2) == 0 ||
uprv_strncmp(uloc_getDefault(), "sh", 2) == 0 ||
uprv_strncmp(uloc_getDefault(), "mc", 2) == 0) {
return;
}

UResourceBundle *test1 = 0, *test2 = 0,*empty = 0;
const UChar *result1, *result2;
UErrorCode status= U_ZERO_ERROR;
Expand Down

0 comments on commit f1a8a63

Please sign in to comment.