int32_t GlobalizationNative_GetDefaultLocaleName(UChar* value, int32_t valueLength) { char localeNameBuffer[ULOC_FULLNAME_CAPACITY]; UErrorCode status = U_ZERO_ERROR; const char* defaultLocale = DetectDefaultLocaleName(); uloc_getBaseName(defaultLocale, localeNameBuffer, ULOC_FULLNAME_CAPACITY, &status); u_charsToUChars_safe(localeNameBuffer, value, valueLength, &status); if (U_SUCCESS(status)) { int localeNameLen = FixupLocaleName(value, valueLength); char collationValueTemp[ULOC_KEYWORDS_CAPACITY]; int32_t collationLen = uloc_getKeywordValue(defaultLocale, "collation", collationValueTemp, ULOC_KEYWORDS_CAPACITY, &status); if (U_SUCCESS(status) && collationLen > 0) { // copy the collation; managed uses a "_" to represent collation (not // "@collation=") u_charsToUChars_safe("_", &value[localeNameLen], valueLength - localeNameLen, &status); u_charsToUChars_safe(collationValueTemp, &value[localeNameLen + 1], valueLength - localeNameLen - 1, &status); } } return UErrorCodeToBool(status); }
ERL_NIF_TERM locale_base_name(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { UErrorCode status = U_ZERO_ERROR; int32_t /*value_len,*/ key_len; char value[LOCALE_LEN], key[LOCALE_LEN]; if (argc != 1) return enif_make_badarg(env); key_len = enif_get_atom(env, argv[0], (char*) key, LOCALE_LEN, ERL_NIF_LATIN1); if (!key_len) { return enif_make_badarg(env); } /*value_len =*/ uloc_getBaseName((const char*) key, /* Locale Id */ (char *) value, /* name */ (int32_t) LOCALE_LEN, &status); CHECK(env, status); return enif_make_atom(env, value); }
U_CAPI int32_t U_EXPORT2 ualoc_getAppleParent(const char* localeID, char * parent, int32_t parentCapacity, UErrorCode* err) { UResourceBundle *rb; int32_t len; UErrorCode tempStatus; char locbuf[ULOC_FULLNAME_CAPACITY+1]; char * foundDoubleUnderscore; if (U_FAILURE(*err)) { return 0; } if ( (parent==NULL)? parentCapacity!=0: parentCapacity<0 ) { *err = U_ILLEGAL_ARGUMENT_ERROR; return 0; } len = uloc_getBaseName(localeID, locbuf, ULOC_FULLNAME_CAPACITY, err); /* canonicalize and strip keywords */ if (U_FAILURE(*err)) { return 0; } if (*err == U_STRING_NOT_TERMINATED_WARNING) { locbuf[ULOC_FULLNAME_CAPACITY] = 0; *err = U_ZERO_ERROR; } foundDoubleUnderscore = uprv_strstr(locbuf, "__"); /* __ comes from bad/missing subtag or variant */ if (foundDoubleUnderscore != NULL) { *foundDoubleUnderscore = 0; /* terminate at the __ */ len = uprv_strlen(locbuf); } if (len >= 2 && uprv_strncmp(locbuf, "zh", 2) == 0) { const char ** forceParentPtr = forceParent; const char * testCurLoc; while ( (testCurLoc = *forceParentPtr++) != NULL ) { int cmp = uprv_strcmp(locbuf, testCurLoc); if (cmp <= 0) { if (cmp == 0) { len = uprv_strlen(*forceParentPtr); if (len < parentCapacity) { uprv_strcpy(parent, *forceParentPtr); } else { *err = U_BUFFER_OVERFLOW_ERROR; } return len; } break; } forceParentPtr++; } } tempStatus = U_ZERO_ERROR; rb = ures_openDirect(NULL, locbuf, &tempStatus); if (U_SUCCESS(tempStatus)) { const char * actualLocale = ures_getLocaleByType(rb, ULOC_ACTUAL_LOCALE, &tempStatus); if (U_SUCCESS(tempStatus) && uprv_strcmp(locbuf, actualLocale) != 0) { // we have followed an alias len = uprv_strlen(actualLocale); if (len < parentCapacity) { uprv_strcpy(parent, actualLocale); } else { *err = U_BUFFER_OVERFLOW_ERROR; } ures_close(rb); return len; } tempStatus = U_ZERO_ERROR; const UChar * parentUName = ures_getStringByKey(rb, "%%Parent", &len, &tempStatus); if (U_SUCCESS(tempStatus) && tempStatus != U_USING_FALLBACK_WARNING) { if (len < parentCapacity) { u_UCharsToChars(parentUName, parent, len + 1); } else { *err = U_BUFFER_OVERFLOW_ERROR; } ures_close(rb); return len; } ures_close(rb); } len = uloc_getParent(locbuf, parent, parentCapacity, err); if (U_SUCCESS(*err) && len == 0) { len = 4; if (len < parentCapacity) { uprv_strcpy(parent, "root"); } else { *err = U_BUFFER_OVERFLOW_ERROR; } } return len; }