2 *******************************************************************************
3 * Copyright (C) 2001-2004, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 *******************************************************************************
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_SERVICE
13 #include "unicode/resbund.h"
23 #define UNDERSCORE_CHAR ((UChar)0x005f)
24 #define AT_SIGN_CHAR ((UChar)64)
25 #define PERIOD_CHAR ((UChar)46)
30 LocaleKey::createWithCanonicalFallback(const UnicodeString
* primaryID
,
31 const UnicodeString
* canonicalFallbackID
,
34 return LocaleKey::createWithCanonicalFallback(primaryID
, canonicalFallbackID
, KIND_ANY
, status
);
38 LocaleKey::createWithCanonicalFallback(const UnicodeString
* primaryID
,
39 const UnicodeString
* canonicalFallbackID
,
43 if (primaryID
== NULL
|| U_FAILURE(status
)) {
46 UnicodeString canonicalPrimaryID
;
47 LocaleUtility::canonicalLocaleString(primaryID
, canonicalPrimaryID
);
48 return new LocaleKey(*primaryID
, canonicalPrimaryID
, canonicalFallbackID
, kind
);
51 LocaleKey::LocaleKey(const UnicodeString
& primaryID
,
52 const UnicodeString
& canonicalPrimaryID
,
53 const UnicodeString
* canonicalFallbackID
,
55 : ICUServiceKey(primaryID
)
57 , _primaryID(canonicalPrimaryID
)
61 _fallbackID
.setToBogus();
62 if (_primaryID
.length() != 0) {
63 if (canonicalFallbackID
!= NULL
&& _primaryID
!= *canonicalFallbackID
) {
64 _fallbackID
= *canonicalFallbackID
;
68 _currentID
= _primaryID
;
71 LocaleKey::~LocaleKey() {}
74 LocaleKey::prefix(UnicodeString
& result
) const {
75 if (_kind
!= KIND_ANY
) {
77 uprv_itou(buffer
, 64, _kind
, 10, 0);
78 UnicodeString
temp(buffer
);
85 LocaleKey::kind() const {
90 LocaleKey::canonicalID(UnicodeString
& result
) const {
91 return result
.append(_primaryID
);
95 LocaleKey::currentID(UnicodeString
& result
) const {
96 if (!_currentID
.isBogus()) {
97 result
.append(_currentID
);
103 LocaleKey::currentDescriptor(UnicodeString
& result
) const {
104 if (!_currentID
.isBogus()) {
105 prefix(result
).append(PREFIX_DELIMITER
).append(_currentID
);
113 LocaleKey::canonicalLocale(Locale
& result
) const {
114 return LocaleUtility::initLocaleFromName(_primaryID
, result
);
118 LocaleKey::currentLocale(Locale
& result
) const {
119 return LocaleUtility::initLocaleFromName(_currentID
, result
);
123 LocaleKey::fallback() {
124 if (!_currentID
.isBogus()) {
125 int x
= _currentID
.lastIndexOf(UNDERSCORE_CHAR
);
127 _currentID
.remove(x
); // truncate current or fallback, whichever we're pointing to
131 if (!_fallbackID
.isBogus()) {
132 _currentID
= _fallbackID
;
133 _fallbackID
.setToBogus();
137 if (_currentID
.length() > 0) {
138 _currentID
.remove(0); // completely truncate
142 _currentID
.setToBogus();
149 LocaleKey::isFallbackOf(const UnicodeString
& id
) const {
150 UnicodeString
temp(id
);
152 return temp
.indexOf(_primaryID
) == 0 &&
153 (temp
.length() == _primaryID
.length() ||
154 temp
.charAt(_primaryID
.length()) == UNDERSCORE_CHAR
);
159 LocaleKey::debug(UnicodeString
& result
) const
161 ICUServiceKey::debug(result
);
162 result
.append(" kind: ");
163 result
.append(_kind
);
164 result
.append(" primaryID: ");
165 result
.append(_primaryID
);
166 result
.append(" fallbackID: ");
167 result
.append(_fallbackID
);
168 result
.append(" currentID: ");
169 result
.append(_currentID
);
174 LocaleKey::debugClass(UnicodeString
& result
) const
176 return result
.append("LocaleKey ");
180 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKey
)
184 /* !UCONFIG_NO_SERVICE */