ICU-7342 add ULocaleDisplayNames, enhance tests to call all ULDN API and via it all LocaleDisplayNames API

X-SVN-Rev: 27267
This commit is contained in:
Doug Felt 2010-01-15 02:35:02 +00:00
parent d3b29178e0
commit 763489b754
6 changed files with 576 additions and 23 deletions

View File

@ -1922,6 +1922,46 @@
RelativePath=".\locdspnm.cpp"
>
</File>
<File
RelativePath=".\unicode\uldnames.h"
>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCustomBuildTool"
CommandLine="copy &quot;$(InputPath)&quot; ..\..\include\unicode&#x0D;&#x0A;"
Outputs="..\..\include\unicode\$(InputFileName)"
/>
</FileConfiguration>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCustomBuildTool"
CommandLine="copy &quot;$(InputPath)&quot; ..\..\include\unicode&#x0D;&#x0A;"
Outputs="..\..\include\unicode\$(InputFileName)"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|x64"
>
<Tool
Name="VCCustomBuildTool"
CommandLine="copy &quot;$(InputPath)&quot; ..\..\include\unicode&#x0D;&#x0A;"
Outputs="..\..\include\unicode\$(InputFileName)"
/>
</FileConfiguration>
<FileConfiguration
Name="Debug|x64"
>
<Tool
Name="VCCustomBuildTool"
CommandLine="copy &quot;$(InputPath)&quot; ..\..\include\unicode&#x0D;&#x0A;"
Outputs="..\..\include\unicode\$(InputFileName)"
/>
</FileConfiguration>
</File>
<File
RelativePath=".\unicode\locdspnm.h"
>

View File

@ -198,13 +198,13 @@ DefaultLocaleDisplayNames::getDialectHandling() const {
UnicodeString&
DefaultLocaleDisplayNames::localeDisplayName(const Locale& locale,
UnicodeString& result) const {
return result.remove();
return result = UnicodeString(locale.getName(), -1, US_INV);
}
UnicodeString&
DefaultLocaleDisplayNames::localeDisplayName(const char* localeId,
UnicodeString& result) const {
return result.remove();
return result = UnicodeString(localeId, -1, US_INV);
}
UnicodeString&
@ -501,6 +501,180 @@ LocaleDisplayNames::createInstance(const Locale& locale,
return new LocaleDisplayNamesImpl(locale, dialectHandling);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
U_DRAFT const ULocaleDisplayNames * U_EXPORT2
uldn_open(const char * locale,
UDialectHandling dialectHandling,
UErrorCode *pErrorCode) {
if (U_FAILURE(*pErrorCode)) {
return 0;
}
if (locale == NULL) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), dialectHandling);
}
U_DRAFT void U_EXPORT2
uldn_close(ULocaleDisplayNames *ldn) {
delete (LocaleDisplayNames *)ldn;
}
U_DRAFT const char * U_EXPORT2
uldn_getLocale(const ULocaleDisplayNames *ldn) {
if (ldn) {
return ((LocaleDisplayNames *)ldn)->getLocale().getName();
}
return NULL;
}
U_DRAFT UDialectHandling U_EXPORT2
uldn_getDialectHandling(const ULocaleDisplayNames *ldn) {
if (ldn) {
return ((LocaleDisplayNames *)ldn)->getDialectHandling();
}
return ULDN_STANDARD_NAMES;
}
U_DRAFT int32_t U_EXPORT2
uldn_localeDisplayName(const ULocaleDisplayNames *ldn,
const char *locale,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode) {
if (U_FAILURE(*pErrorCode)) {
return 0;
}
if (ldn == NULL || locale == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
UnicodeString temp(result, 0, maxResultSize);
((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp);
return temp.extract(result, maxResultSize, *pErrorCode);
}
U_DRAFT int32_t U_EXPORT2
uldn_languageDisplayName(const ULocaleDisplayNames *ldn,
const char *lang,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode) {
if (U_FAILURE(*pErrorCode)) {
return 0;
}
if (ldn == NULL || lang == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
UnicodeString temp(result, 0, maxResultSize);
((const LocaleDisplayNames *)ldn)->languageDisplayName(lang, temp);
return temp.extract(result, maxResultSize, *pErrorCode);
}
U_DRAFT int32_t U_EXPORT2
uldn_scriptDisplayName(const ULocaleDisplayNames *ldn,
const char *script,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode) {
if (U_FAILURE(*pErrorCode)) {
return 0;
}
if (ldn == NULL || script == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
UnicodeString temp(result, 0, maxResultSize);
((const LocaleDisplayNames *)ldn)->scriptDisplayName(script, temp);
return temp.extract(result, maxResultSize, *pErrorCode);
}
U_DRAFT int32_t U_EXPORT2
uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn,
UScriptCode scriptCode,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode) {
return uldn_scriptDisplayName(ldn, uscript_getName(scriptCode), result, maxResultSize, pErrorCode);
}
U_DRAFT int32_t U_EXPORT2
uldn_regionDisplayName(const ULocaleDisplayNames *ldn,
const char *region,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode) {
if (U_FAILURE(*pErrorCode)) {
return 0;
}
if (ldn == NULL || region == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
UnicodeString temp(result, 0, maxResultSize);
((const LocaleDisplayNames *)ldn)->regionDisplayName(region, temp);
return temp.extract(result, maxResultSize, *pErrorCode);
}
U_DRAFT int32_t U_EXPORT2
uldn_variantDisplayName(const ULocaleDisplayNames *ldn,
const char *variant,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode) {
if (U_FAILURE(*pErrorCode)) {
return 0;
}
if (ldn == NULL || variant == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
UnicodeString temp(result, 0, maxResultSize);
((const LocaleDisplayNames *)ldn)->variantDisplayName(variant, temp);
return temp.extract(result, maxResultSize, *pErrorCode);
}
U_DRAFT int32_t U_EXPORT2
uldn_keyDisplayName(const ULocaleDisplayNames *ldn,
const char *key,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode) {
if (U_FAILURE(*pErrorCode)) {
return 0;
}
if (ldn == NULL || key == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
UnicodeString temp(result, 0, maxResultSize);
((const LocaleDisplayNames *)ldn)->keyDisplayName(key, temp);
return temp.extract(result, maxResultSize, *pErrorCode);
}
U_DRAFT int32_t U_EXPORT2
uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn,
const char *key,
const char *value,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode) {
if (U_FAILURE(*pErrorCode)) {
return 0;
}
if (ldn == NULL || key == NULL || value == NULL || (result == NULL && maxResultSize > 0)
|| maxResultSize < 0) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
UnicodeString temp(result, 0, maxResultSize);
((const LocaleDisplayNames *)ldn)->keyValueDisplayName(key, value, temp);
return temp.extract(result, maxResultSize, *pErrorCode);
}
U_NAMESPACE_END
#endif

View File

@ -15,31 +15,11 @@
* \brief C++ API: Provides display names of Locale and its components.
*/
/**
* Enum used in LocaleDisplayNames::createInstance.
* @draft ICU 4.4
*/
typedef enum {
/**
* Use standard names when generating a locale name,
* e.g. en_GB displays as 'English (United Kingdom)'.
* @draft ICU 4.4
*/
ULDN_STANDARD_NAMES = 0,
/**
* Use dialect names, when generating a locale name,
* e.g. en_GB displays as 'British English'.
* @draft ICU 4.4
*/
ULDN_DIALECT_NAMES
} UDialectHandling;
#if !UCONFIG_NO_FORMATTING
#include "unicode/locid.h"
#include "unicode/uscript.h"
#include "unicode/uldnames.h"
U_NAMESPACE_BEGIN

View File

@ -0,0 +1,248 @@
/*
*******************************************************************************
* Copyright (C) 2010, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
#ifndef __ULDNAMES_H__
#define __ULDNAMES_H__
/**
* \file
* \brief C++ API: Provides display names of Locale ids and their components.
*/
#include "unicode/utypes.h"
/**
* Enum used in LocaleDisplayNames::createInstance.
* @draft ICU 4.4
*/
typedef enum {
/**
* Use standard names when generating a locale name,
* e.g. en_GB displays as 'English (United Kingdom)'.
* @draft ICU 4.4
*/
ULDN_STANDARD_NAMES = 0,
/**
* Use dialect names, when generating a locale name,
* e.g. en_GB displays as 'British English'.
* @draft ICU 4.4
*/
ULDN_DIALECT_NAMES
} UDialectHandling;
/**
* Opaque C service object type for the locale display names API
* @draft ICU 4.4
*/
struct ULocaleDisplayNames;
/**
* C typedef for struct ULocaleDisplayNames.
* @draft ICU 4.4
*/
typedef struct ULocaleDisplayNames ULocaleDisplayNames;
#if !UCONFIG_NO_FORMATTING
/**
* Returns an instance of LocaleDisplayNames that returns names
* formatted for the provided locale, using the provided
* dialectHandling. The usual value for dialectHandling is
* ULOC_STANDARD_NAMES.
*
* @param locale the display locale
* @param dialectHandling how to select names for locales
* @return a ULocaleDisplayNames instance
* @draft ICU 4.4
*/
U_DRAFT ULocaleDisplayNames * U_EXPORT2
uldn_open(const char * locale,
UDialectHandling dialectHandling,
UErrorCode *pErrorCode);
/**
* Closes a ULocaleDisplayNames instance obtained from uldn_open().
* @param ldn the ULocaleDisplayNames instance to be closed
* @draft ICU 4.4
*/
U_DRAFT void U_EXPORT2
uldn_close(ULocaleDisplayNames *ldn);
/* getters for state */
/**
* Returns the locale used to determine the display names. This is
* not necessarily the same locale passed to {@link #uldn_open}.
* @param ldn the LocaleDisplayNames instance
* @return the display locale
* @draft ICU 4.4
*/
U_DRAFT const char * U_EXPORT2
uldn_getLocale(const ULocaleDisplayNames *ldn);
/**
* Returns the dialect handling used in the display names.
* @param ldn the LocaleDisplayNames instance
* @return the dialect handling enum
* @draft ICU 4.4
*/
U_DRAFT UDialectHandling U_EXPORT2
uldn_getDialectHandling(const ULocaleDisplayNames *ldn);
/* names for entire locales */
/**
* Returns the display name of the provided locale.
* @param ldn the LocaleDisplayNames instance
* @param locale the locale whose display name to return
* @param result receives the display name
* @param maxResultSize the size of the result buffer
* @param pErrorCode the status code
* @return the actual buffer size needed for the display name. If it's
* greater than maxResultSize, the returned name will be truncated.
* @draft ICU 4.4
*/
U_DRAFT int32_t U_EXPORT2
uldn_localeDisplayName(const ULocaleDisplayNames *ldn,
const char *locale,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode);
/* names for components of a locale */
/**
* Returns the display name of the provided language code.
* @param ldn the LocaleDisplayNames instance
* @param lang the language code whose display name to return
* @param result receives the display name
* @param maxResultSize the size of the result buffer
* @param pErrorCode the status code
* @return the actual buffer size needed for the display name. If it's
* greater than maxResultSize, the returned name will be truncated.
* @draft ICU 4.4
*/
U_DRAFT int32_t U_EXPORT2
uldn_languageDisplayName(const ULocaleDisplayNames *ldn,
const char *lang,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode);
/**
* Returns the display name of the provided script.
* @param ldn the LocaleDisplayNames instance
* @param script the script whose display name to return
* @param result receives the display name
* @param maxResultSize the size of the result buffer
* @param pErrorCode the status code
* @return the actual buffer size needed for the display name. If it's
* greater than maxResultSize, the returned name will be truncated.
* @draft ICU 4.4
*/
U_DRAFT int32_t U_EXPORT2
uldn_scriptDisplayName(const ULocaleDisplayNames *ldn,
const char *script,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode);
/**
* Returns the display name of the provided script code.
* @param ldn the LocaleDisplayNames instance
* @param scriptCode the script code whose display name to return
* @param result receives the display name
* @param maxResultSize the size of the result buffer
* @param pErrorCode the status code
* @return the actual buffer size needed for the display name. If it's
* greater than maxResultSize, the returned name will be truncated.
* @draft ICU 4.4
*/
U_DRAFT int32_t U_EXPORT2
uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn,
UScriptCode scriptCode,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode);
/**
* Returns the display name of the provided region code.
* @param ldn the LocaleDisplayNames instance
* @param region the region code whose display name to return
* @param result receives the display name
* @param maxResultSize the size of the result buffer
* @param pErrorCode the status code
* @return the actual buffer size needed for the display name. If it's
* greater than maxResultSize, the returned name will be truncated.
* @draft ICU 4.4
*/
U_DRAFT int32_t U_EXPORT2
uldn_regionDisplayName(const ULocaleDisplayNames *ldn,
const char *region,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode);
/**
* Returns the display name of the provided variant
* @param ldn the LocaleDisplayNames instance
* @param variant the variant whose display name to return
* @param result receives the display name
* @param maxResultSize the size of the result buffer
* @param pErrorCode the status code
* @return the actual buffer size needed for the display name. If it's
* greater than maxResultSize, the returned name will be truncated.
* @draft ICU 4.4
*/
U_DRAFT int32_t U_EXPORT2
uldn_variantDisplayName(const ULocaleDisplayNames *ldn,
const char *variant,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode);
/**
* Returns the display name of the provided locale key
* @param ldn the LocaleDisplayNames instance
* @param key the locale key whose display name to return
* @param result receives the display name
* @param maxResultSize the size of the result buffer
* @param pErrorCode the status code
* @return the actual buffer size needed for the display name. If it's
* greater than maxResultSize, the returned name will be truncated.
* @draft ICU 4.4
*/
U_DRAFT int32_t U_EXPORT2
uldn_keyDisplayName(const ULocaleDisplayNames *ldn,
const char *key,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode);
/**
* Returns the display name of the provided value (used with the provided key).
* @param ldn the LocaleDisplayNames instance
* @param key the locale key
* @param value the locale key's value
* @param result receives the display name
* @param maxResultSize the size of the result buffer
* @param pErrorCode the status code
* @return the actual buffer size needed for the display name. If it's
* greater than maxResultSize, the returned name will be truncated.
* @draft ICU 4.4
*/
U_DRAFT int32_t U_EXPORT2
uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn,
const char *key,
const char *value,
UChar *result,
int32_t maxResultSize,
UErrorCode *pErrorCode);
#endif /* !UCONFIG_NO_FORMATTING */
#endif /* __ULDNAMES_H__ */

View File

@ -67,6 +67,10 @@ void LocaleDisplayNamesTest::runIndexedTest(int32_t index, UBool exec, const cha
TESTCASE(0, TestCreate);
TESTCASE(1, TestCreateDialect);
TESTCASE(2, TestWithKeywordsAndEverything);
TESTCASE(3, TestUldnOpen);
TESTCASE(4, TestUldnOpenDialect);
TESTCASE(5, TestUldnWithKeywordsAndEverything);
TESTCASE(6, TestUldnComponents);
#endif
default:
name = "";
@ -100,3 +104,106 @@ void LocaleDisplayNamesTest::TestWithKeywordsAndEverything() {
delete ldn;
test_assert_equal(target, temp);
}
void LocaleDisplayNamesTest::TestUldnOpen() {
UErrorCode status = U_ZERO_ERROR;
const int32_t kMaxResultSize = 150; // long enough
UChar result[150];
ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
int32_t len = uldn_localeDisplayName(ldn, "de_DE", result, kMaxResultSize, &status);
uldn_close(ldn);
test_assert(U_SUCCESS(status));
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal("Deutsch (Deutschland)", str);
}
void LocaleDisplayNamesTest::TestUldnOpenDialect() {
UErrorCode status = U_ZERO_ERROR;
const int32_t kMaxResultSize = 150; // long enough
UChar result[150];
ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES, &status);
int32_t len = uldn_localeDisplayName(ldn, "en_GB", result, kMaxResultSize, &status);
uldn_close(ldn);
test_assert(U_SUCCESS(status));
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal("British English", str);
}
void LocaleDisplayNamesTest::TestUldnWithKeywordsAndEverything() {
UErrorCode status = U_ZERO_ERROR;
const int32_t kMaxResultSize = 150; // long enough
UChar result[150];
const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
const char *target = "English (Traditional Han, United States, VALLEY, "
"calendar=Gregorian Calendar, collation=Phonebook Sort Order)";
ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_STANDARD_NAMES, &status);
int32_t len = uldn_localeDisplayName(ldn, locname, result, kMaxResultSize, &status);
uldn_close(ldn);
test_assert(U_SUCCESS(status));
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal(target, str);
}
void LocaleDisplayNamesTest::TestUldnComponents() {
UErrorCode status = U_ZERO_ERROR;
const int32_t kMaxResultSize = 150; // long enough
UChar result[150];
ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
test_assert(U_SUCCESS(status));
if (U_FAILURE(status)) {
return;
}
// "en_Hant_US_PRE_EURO@calendar=gregorian";
{
int32_t len = uldn_languageDisplayName(ldn, "en", result, kMaxResultSize, &status);
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal("Englisch", str);
}
{
int32_t len = uldn_scriptDisplayName(ldn, "Hant", result, kMaxResultSize, &status);
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal("Traditionelle Chinesische Schrift", str);
}
{
int32_t len = uldn_scriptCodeDisplayName(ldn, USCRIPT_TRADITIONAL_HAN, result, kMaxResultSize,
&status);
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal("Traditionelle Chinesische Schrift", str);
}
{
int32_t len = uldn_regionDisplayName(ldn, "US", result, kMaxResultSize, &status);
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal("Vereinigte Staaten", str);
}
{
int32_t len = uldn_variantDisplayName(ldn, "PRE_EURO", result, kMaxResultSize, &status);
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal("PRE_EURO", str);
}
{
int32_t len = uldn_keyDisplayName(ldn, "calendar", result, kMaxResultSize, &status);
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal("Kalender", str);
}
{
int32_t len = uldn_keyValueDisplayName(ldn, "calendar", "gregorian", result,
kMaxResultSize, &status);
UnicodeString str(result, len, kMaxResultSize);
test_assert_equal("Gregorianischer Kalender", str);
}
uldn_close(ldn);
}

View File

@ -24,5 +24,9 @@ public:
void TestCreate(void);
void TestCreateDialect(void);
void TestWithKeywordsAndEverything(void);
void TestUldnOpen(void);
void TestUldnOpenDialect(void);
void TestUldnWithKeywordsAndEverything(void);
void TestUldnComponents(void);
#endif
};