+ char availLocMinOrParent[kLangScriptRegMaxLen + 1];
+ int32_t distance;
+ // first check for special Apple parents of availLocNorm -
+ // - the number of locales with such parents is small -
+ // or if not such parent, then try stripping region.
+ const char *availLocParent = getLocParent(availLocNorm[availLocIndex], &distance);
+ if (availLocParent) {
+#if DEBUG_UALOC
+ printf(" # availLocAppleParentName %s\n", availLocParent);
+#endif
+ if (uprv_strcmp(prefLangBaseName, availLocParent) == 0 && distance < minDistance) {
+ availLocIndexBackup = availLocIndex; // records where the match occurred
+ backupMatchPrefLangIndex = prefLangIndex;
+ minDistance = distance;
+#if DEBUG_UALOC
+ printf(" # BACKUP: LocAppleParent matched prefLangNormName with distance %d\n", distance);
+#endif
+ continue;
+ }
+ }
+ if (minDistance <= 1) {
+ continue; // we can't get any closer in the rest of this iteration
+ }
+ if (availLocParent == NULL) {
+ tmpStatus = U_ZERO_ERROR;
+ int32_t regLen = uloc_getCountry(availLocNorm[availLocIndex], availLocMinOrParent, kLangScriptRegMaxLen, &tmpStatus);
+ if (U_SUCCESS(tmpStatus) && regLen > 1) {
+ uloc_addLikelySubtags(availLocNorm[availLocIndex], availLocMinOrParent, kLangScriptRegMaxLen, &tmpStatus);
+ if (U_SUCCESS(tmpStatus)) {
+ availLocMinOrParent[kLangScriptRegMaxLen] = 0; // ensure 0 termination, could have U_STRING_NOT_TERMINATED_WARNING
+#if DEBUG_UALOC
+ printf(" # availLocRegMaxName %s\n", availLocMinOrParent);
+#endif
+ char availLocTemp[kLangScriptRegMaxLen + 1];
+ uloc_getParent(availLocMinOrParent, availLocTemp, kLangScriptRegMaxLen, &tmpStatus);
+ if (U_SUCCESS(tmpStatus)) {
+ availLocTemp[kLangScriptRegMaxLen] = 0;
+ uloc_minimizeSubtags(availLocTemp, availLocMinOrParent, kLangScriptRegMaxLen, &tmpStatus);
+ if (U_SUCCESS(tmpStatus)) {
+ availLocMinOrParent[kLangScriptRegMaxLen] = 0;
+#if DEBUG_UALOC
+ printf(" # availLocNoRegParentName %s\n", availLocMinOrParent);
+#endif
+ if (uprv_strcmp(prefLangBaseName, availLocMinOrParent) == 0) {
+ availLocIndexBackup = availLocIndex; // records where the match occurred
+ backupMatchPrefLangIndex = prefLangIndex;
+ minDistance = 1;
+#if DEBUG_UALOC
+ printf(" # BACKUP: LocNoRegParent matched prefLangNormName with distance 1\n");
+#endif
+ continue;
+ }
+ }
+ }
+ }
+ }
+ }
+ // then check against minimized version of availLocNorm
+ tmpStatus = U_ZERO_ERROR;
+ uloc_minimizeSubtags(availLocNorm[availLocIndex], availLocMinOrParent, kLangScriptRegMaxLen, &tmpStatus);
+ if (U_FAILURE(tmpStatus)) {
+ continue;
+ }
+ availLocMinOrParent[kLangScriptRegMaxLen] = 0; // ensure 0 termination, could have U_STRING_NOT_TERMINATED_WARNING
+#if DEBUG_UALOC
+ printf(" # availLocMinimized %s\n", availLocMinOrParent);
+#endif
+ if (uprv_strcmp(prefLangBaseName, availLocMinOrParent) == 0) {
+ availLocIndexBackup = availLocIndex; // records where the match occurred
+ backupMatchPrefLangIndex = prefLangIndex;
+ minDistance = 1;
+#if DEBUG_UALOC
+ printf(" # BACKUP: LocMinimized matched prefLangNormName with distance 1\n");
+#endif