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"
31 #include "FLcl_LocaleManagerImpl.h"
33 using namespace Tizen::Base;
35 namespace Tizen { namespace Locales
38 _LocaleImpl::_LocaleImpl(void)
43 _LocaleImpl::_LocaleImpl(const char* pLocaleCode)
44 : __icuLocale(pLocaleCode)
46 if (pLocaleCode == null || *pLocaleCode == '\0')
48 __icuLocale.setToBogus();
52 _LocaleImpl::_LocaleImpl(const char* pLanguage, const char* pCountry, const char* pVariant)
53 : __icuLocale(pLanguage, pCountry, pVariant)
55 if (pLanguage == null || *pLanguage == '\0' || pCountry == null || *pCountry == '\0')
57 __icuLocale.setToBogus();
61 _LocaleImpl::_LocaleImpl(const U_ICU_NAMESPACE::Locale& icuLocale)
62 : __icuLocale(icuLocale)
66 _LocaleImpl::~_LocaleImpl(void)
71 _LocaleImpl::CloneN(void)
73 return new (std::nothrow) _LocaleImpl(__icuLocale);
76 U_ICU_NAMESPACE::Locale
77 _LocaleImpl::GetIcuLocale (void) const
83 _LocaleImpl::GetOspLocale(void)
85 LanguageCode languageCode = Locale::StringToLanguageCode(GetLanguageCodeString(false));
86 CountryCode countryCode = Locale::StringToCountryCode(Get2LetterCountryCodeString());
87 String variant(GetVariantCodeString());
88 return Locale(languageCode, countryCode, &variant);
92 _LocaleImpl::operator ==(const _LocaleImpl& otherLocale) const
94 return __icuLocale == otherLocale.__icuLocale;
98 _LocaleImpl::operator !=(const _LocaleImpl& otherLocale) const
100 return !(*this == otherLocale);
104 _LocaleImpl::Equals(const Object& obj) const
106 const _LocaleImpl* pOtherLocale = dynamic_cast< const _LocaleImpl* >(&obj);
109 return *this == *pOtherLocale;
115 _LocaleImpl::GetHashCode(void) const
117 return __icuLocale.hashCode();
121 _LocaleImpl::GetLanguageCodeString(bool isTwoLetter)
123 String languageCode((isTwoLetter ? __icuLocale.getLanguage() : __icuLocale.getISO3Language()));
124 String languageScriptTmp(__icuLocale.getScript());
126 if (!languageScriptTmp.IsEmpty())
128 String languageScript;
129 languageScriptTmp.ToLowerCase(languageScript);
130 languageCode.Append(L"-");
131 languageCode.Append(languageScript);
137 _LocaleImpl::GetLanguageName(String& languageName) const
139 Locale loc = _LocaleManagerImpl::GetSystemLocale();
140 return GetLanguageName(*loc.__pLocaleImpl, languageName);
144 _LocaleImpl::GetLanguageName(const _LocaleImpl& otherLocale, String& languageName) const
146 U_ICU_NAMESPACE::UnicodeString icuStr;
147 languageName = _LocaleData::GetOspString(__icuLocale.getDisplayLanguage(otherLocale.__icuLocale, icuStr));
148 if (!languageName.IsEmpty())
150 String languageScriptTmp(__icuLocale.getScript());
152 if (!languageScriptTmp.IsEmpty())
154 U_ICU_NAMESPACE::UnicodeString icuScriptStr;
155 String languageScriptName = _LocaleData::GetOspString(__icuLocale.getDisplayScript(otherLocale.__icuLocale, icuScriptStr));
156 languageName.Insert(L" (", languageName.GetLength());
157 languageName.Insert(languageScriptName, languageName.GetLength());
158 languageName.Insert(L")", languageName.GetLength());
162 return E_UNSUPPORTED_OPERATION;
166 _LocaleImpl::Get2LetterCountryCodeString(void) const
168 return __icuLocale.getCountry();
172 _LocaleImpl::Get3LetterCountryCodeString(void) const
174 return __icuLocale.getISO3Country();
178 _LocaleImpl::GetCountryName(String& countryName) const
180 Locale loc = _LocaleManagerImpl::GetSystemLocale();
181 return GetCountryName(*loc.__pLocaleImpl, countryName);
185 _LocaleImpl::GetCountryName(const _LocaleImpl& otherLocale, String& countryName) const
187 U_ICU_NAMESPACE::UnicodeString icuStr;
188 countryName = _LocaleData::GetOspString(__icuLocale.getDisplayCountry(otherLocale.__icuLocale, icuStr));
189 if (!countryName.IsEmpty())
193 return E_UNSUPPORTED_OPERATION;
198 _LocaleImpl::GetVariantCodeString(void) const
200 return __icuLocale.getVariant();
204 _LocaleImpl::SetVariantCodeString(const char* pVariantCode)
206 __icuLocale = U_ICU_NAMESPACE::Locale(__icuLocale.getLanguage(), __icuLocale.getCountry(), pVariantCode);
210 _LocaleImpl::GetLocaleCodeString(void) const
212 return __icuLocale.getName();
216 _LocaleImpl::IsSupported(void) const
218 return !__icuLocale.isBogus();
222 _LocaleImpl::IsSupported(const Locale& ospLocale)
224 return !ospLocale.__pLocaleImpl->__icuLocale.isBogus();
228 _LocaleImpl::GetLocaleImpl(const Locale& ospLocale)
230 return ospLocale.__pLocaleImpl;
234 _LocaleImpl::Get2LetterLanguageCodeString(const char* pLanguageCodeString)
236 if (pLanguageCodeString)
238 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
239 return localeImpl.GetLanguageCodeString(true);
245 _LocaleImpl::Get3LetterLanguageCodeString(const char* pLanguageCodeString)
247 if (pLanguageCodeString)
249 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
250 return localeImpl.GetLanguageCodeString(false);
256 _LocaleImpl::Get2LetterCountryCodeString(const char* pCountryCodeString)
258 if (pCountryCodeString && strlen(pCountryCodeString) == 3)
260 _LocaleImpl localeImpl("en", pCountryCodeString, null);
261 return localeImpl.Get2LetterCountryCodeString();
267 _LocaleImpl::Get3LetterCountryCodeString(const char* pCountryCodeString)
269 if (pCountryCodeString)
271 _LocaleImpl localeImpl("en", pCountryCodeString, null);
272 return localeImpl.Get3LetterCountryCodeString();