void AlphabeticIndexTest::TestChineseZhuyin() {
    UErrorCode status = U_ZERO_ERROR;
    char loc[100];
    uloc_forLanguageTag("zh-u-co-zhuyin", loc, LENGTHOF(loc), NULL, &status);
    AlphabeticIndex index(loc, status);
    LocalPointer<AlphabeticIndex::ImmutableIndex> immIndex(index.buildImmutableIndex(status));
    TEST_CHECK_STATUS; 
    assertEquals("getBucketCount()", 38, immIndex->getBucketCount());
    assertEquals("label 1", UnicodeString((UChar)0x3105), immIndex->getBucket(1)->getLabel());
    assertEquals("label 2", UnicodeString((UChar)0x3106), immIndex->getBucket(2)->getLabel());
    assertEquals("label 3", UnicodeString((UChar)0x3107), immIndex->getBucket(3)->getLabel());
    assertEquals("label 4", UnicodeString((UChar)0x3108), immIndex->getBucket(4)->getLabel());
    assertEquals("label 5", UnicodeString((UChar)0x3109), immIndex->getBucket(5)->getLabel());
}
int main() {
  UErrorCode status = U_ZERO_ERROR;
  char theirLocale[ULOC_FULLNAME_CAPACITY];
  uloc_forLanguageTag("es-419-u-nu-roman", // IETF BCP 47
		      theirLocale, ULOC_FULLNAME_CAPACITY, NULL, &status);
  // theirLocale is now: “es_419@numbers=roman”
  UChar str[256];
  const char *ourLocale = uloc_getDefault();
  ULocaleDisplayNames *ldn = uldn_open(ourLocale,
				       ULDN_DIALECT_NAMES, &status);
  uldn_localeDisplayName(ldn, theirLocale, str, 256, &status);
  uldn_close(ldn);
  if(U_FAILURE(status)) { puts(u_errorName(status)); return 1; }
  u_printf("\n==> %S!\n \n", str);
  return 0;
}
static jstring ICU_localeForLanguageTag(JNIEnv* env, jclass, jstring languageTag, jboolean strict) {
    ScopedUtfChars languageTagChars(env, languageTag);

    // Naively assume that in the average case, the size of
    // the normalized language tag will be very nearly the same as the
    // size of the input. This is generally true for language
    // tags that are "simple" language-region-variant combinations
    // that don't contain any grandfathered tags.
    const size_t initialBufferSize = languageTagChars.size() + 32;
    std::vector<char> buffer(initialBufferSize);
    int32_t parsedLength = 0;

    UErrorCode status = U_ZERO_ERROR;
    while (true) {
        const size_t outputLength = uloc_forLanguageTag(languageTagChars.c_str(),
               &buffer[0], buffer.size(), &parsedLength, &status);
        if (U_FAILURE(status)) {
            return NULL;
        }

        // Assume that we've run out of buffer space when this happens. Double
        // the buffer size and try again. This should happen very infrequently.
        if (outputLength == buffer.size()) {
            buffer.resize(buffer.size() << 1);
        } else {
            break;
        }
    }

    if (parsedLength < 0) {
        return NULL;
    }

    // By default, ICU will ignore all subtags starting at the first unparseable
    // or invalid subtag. Our "strict" mode is specified to throw an error if
    // that happens.
    //
    // NOTE: The cast is safe because parsedLength can never be negative thanks
    // to the check above. ICU does not document any negative return values for
    // that field, but check for it anyway.
    if ((strict == JNI_TRUE) &&
        (static_cast<uint32_t>(parsedLength) != languageTagChars.size())) {
        return NULL;
    }

    return env->NewStringUTF(&buffer[0]);
}
Exemple #4
0
int icu_canonicalize_language(lua_State *L) {
  const char* lang = luaL_checkstring(L, 1);
  char locale[200], minimized[200], result[200];
  UErrorCode error = 0;
  uloc_forLanguageTag(lang, locale, sizeof(locale), NULL, &error);
  if (!error) {
    uloc_minimizeSubtags(locale, minimized, sizeof(minimized), &error);
  }
  if (!error) {
    uloc_toLanguageTag(minimized, result, sizeof(result),
               /* strict */ 1, &error);
  }
  if (!error) {
    lua_pushstring(L, result);
  } else {
    lua_pushstring(L, "und");
  }
  return 1;
}