2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FLcl_LocaleImpl.cpp
20 * @brief This is the implementation file for _LocaleImpl class.
23 #include <unicode/errorcode.h>
24 #include <unicode/ustring.h>
26 #include <FBaseSysLog.h>
27 #include <FLclLocale.h>
29 #include "FLcl_LocaleImpl.h"
30 #include "FLcl_LocaleData.h"
32 using namespace Tizen::Base;
34 namespace Tizen { namespace Locales
37 _LocaleImpl::_LocaleImpl(void)
42 _LocaleImpl::_LocaleImpl(const char* pLocaleCode)
43 : __icuLocale(pLocaleCode)
45 if (pLocaleCode == null || *pLocaleCode == '\0')
47 __icuLocale.setToBogus();
51 _LocaleImpl::_LocaleImpl(const char* pLanguage, const char* pCountry, const char* pVariant)
52 : __icuLocale(pLanguage, pCountry, pVariant)
54 if (pLanguage == null || *pLanguage == '\0' || pCountry == null || *pCountry == '\0')
56 __icuLocale.setToBogus();
60 _LocaleImpl::_LocaleImpl(const U_ICU_NAMESPACE::Locale& icuLocale)
61 : __icuLocale(icuLocale)
65 _LocaleImpl::~_LocaleImpl(void)
70 _LocaleImpl::CloneN(void)
72 return new (std::nothrow) _LocaleImpl(__icuLocale);
75 U_ICU_NAMESPACE::Locale
76 _LocaleImpl::GetIcuLocale (void) const
82 _LocaleImpl::GetOspLocale(void)
84 LanguageCode languageCode = Locale::StringToLanguageCode(GetLanguageCodeString(false));
85 CountryCode countryCode = Locale::StringToCountryCode(Get2LetterCountryCodeString());
86 String variant(GetVariantCodeString());
87 return Locale(languageCode, countryCode, &variant);
91 _LocaleImpl::operator ==(const _LocaleImpl& otherLocale) const
93 return __icuLocale == otherLocale.__icuLocale;
97 _LocaleImpl::operator !=(const _LocaleImpl& otherLocale) const
99 return !(*this == otherLocale);
103 _LocaleImpl::Equals(const Object& obj) const
105 const _LocaleImpl* pOtherLocale = dynamic_cast< const _LocaleImpl* >(&obj);
108 return *this == *pOtherLocale;
114 _LocaleImpl::GetHashCode(void) const
116 return __icuLocale.hashCode();
120 _LocaleImpl::GetLanguageCodeString(bool isTwoLetter)
122 String languageCode((isTwoLetter ? __icuLocale.getLanguage() : __icuLocale.getISO3Language()));
123 String languageScriptTmp(__icuLocale.getScript());
125 if (!languageScriptTmp.IsEmpty())
127 String languageScript;
128 languageScriptTmp.ToLowerCase(languageScript);
129 languageCode.Append(L"-");
130 languageCode.Append(languageScript);
136 _LocaleImpl::GetLanguageName(String& languageName) const
138 U_ICU_NAMESPACE::UnicodeString icuStr;
139 languageName = _LocaleData::GetOspString(__icuLocale.getDisplayLanguage(icuStr));
140 if (!languageName.IsEmpty())
142 String languageScriptTmp(__icuLocale.getScript());
144 if (!languageScriptTmp.IsEmpty())
146 U_ICU_NAMESPACE::UnicodeString icuScriptStr;
147 String languageScriptName = _LocaleData::GetOspString(__icuLocale.getDisplayScript(icuScriptStr));
148 languageName.Insert(L" (", languageName.GetLength());
149 languageName.Insert(languageScriptName, languageName.GetLength());
150 languageName.Insert(L")", languageName.GetLength());
154 return E_UNSUPPORTED_OPERATION;
158 _LocaleImpl::GetLanguageName(const _LocaleImpl& otherLocale, String& languageName) const
160 U_ICU_NAMESPACE::UnicodeString icuStr;
161 languageName = _LocaleData::GetOspString(__icuLocale.getDisplayLanguage(otherLocale.__icuLocale, icuStr));
162 if (!languageName.IsEmpty())
164 String languageScriptTmp(__icuLocale.getScript());
166 if (!languageScriptTmp.IsEmpty())
168 U_ICU_NAMESPACE::UnicodeString icuScriptStr;
169 String languageScriptName = _LocaleData::GetOspString(__icuLocale.getDisplayScript(otherLocale.__icuLocale, icuScriptStr));
170 languageName.Insert(L" (", languageName.GetLength());
171 languageName.Insert(languageScriptName, languageName.GetLength());
172 languageName.Insert(L")", languageName.GetLength());
176 return E_UNSUPPORTED_OPERATION;
180 _LocaleImpl::Get2LetterCountryCodeString(void) const
182 return __icuLocale.getCountry();
186 _LocaleImpl::Get3LetterCountryCodeString(void) const
188 return __icuLocale.getISO3Country();
192 _LocaleImpl::GetCountryName(String& countryName) const
194 U_ICU_NAMESPACE::UnicodeString icuStr;
195 countryName = _LocaleData::GetOspString(__icuLocale.getDisplayCountry(icuStr));
196 if (!countryName.IsEmpty())
200 return E_UNSUPPORTED_OPERATION;
204 _LocaleImpl::GetCountryName(const _LocaleImpl& otherLocale, String& countryName) const
206 U_ICU_NAMESPACE::UnicodeString icuStr;
207 countryName = _LocaleData::GetOspString(__icuLocale.getDisplayCountry(otherLocale.__icuLocale, icuStr));
208 if (!countryName.IsEmpty())
212 return E_UNSUPPORTED_OPERATION;
217 _LocaleImpl::GetVariantCodeString(void) const
219 return __icuLocale.getVariant();
223 _LocaleImpl::SetVariantCodeString(const char* pVariantCode)
225 __icuLocale = U_ICU_NAMESPACE::Locale(__icuLocale.getLanguage(), __icuLocale.getCountry(), pVariantCode);
229 _LocaleImpl::GetLocaleCodeString(void) const
231 return __icuLocale.getName();
235 _LocaleImpl::IsSupported(void) const
237 return !__icuLocale.isBogus();
241 _LocaleImpl::IsSupported(const Locale& ospLocale)
243 return !ospLocale.__pLocaleImpl->__icuLocale.isBogus();
247 _LocaleImpl::GetLocaleImpl(const Locale& ospLocale)
249 return ospLocale.__pLocaleImpl;
253 _LocaleImpl::Get2LetterLanguageCodeString(const char* pLanguageCodeString)
255 if (pLanguageCodeString)
257 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
258 return localeImpl.GetLanguageCodeString(true);
264 _LocaleImpl::Get3LetterLanguageCodeString(const char* pLanguageCodeString)
266 if (pLanguageCodeString)
268 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
269 return localeImpl.GetLanguageCodeString(false);
275 _LocaleImpl::Get2LetterCountryCodeString(const char* pCountryCodeString)
277 if (pCountryCodeString && strlen(pCountryCodeString) == 3)
279 _LocaleImpl localeImpl("en", pCountryCodeString, null);
280 return localeImpl.Get2LetterCountryCodeString();
286 _LocaleImpl::Get3LetterCountryCodeString(const char* pCountryCodeString)
288 if (pCountryCodeString)
290 _LocaleImpl localeImpl("en", pCountryCodeString, null);
291 return localeImpl.Get3LetterCountryCodeString();