Merge "[2.2.1] Modify String::Replace() which shared buffer even when the whole strin...
[platform/framework/native/appfw.git] / src / locales / FLcl_LocaleImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file            FLcl_LocaleImpl.cpp
19  * @brief           This is the implementation file for _LocaleImpl class.
20  */
21
22 #include <unicode/errorcode.h>
23 #include <unicode/ustring.h>
24
25 #include <FBaseSysLog.h>
26 #include <FLclLocale.h>
27
28 #include "FLcl_LocaleImpl.h"
29 #include "FLcl_LocaleData.h"
30 #include "FLcl_LocaleManagerImpl.h"
31
32 using namespace Tizen::Base;
33
34 namespace Tizen { namespace Locales
35 {
36
37 _LocaleImpl::_LocaleImpl(void)
38         : __icuLocale()
39 {
40 }
41
42 _LocaleImpl::_LocaleImpl(const char* pLocaleCode)
43         : __icuLocale(pLocaleCode)
44 {
45         if (pLocaleCode == null || *pLocaleCode == '\0')
46         {
47                 __icuLocale.setToBogus();
48         }
49 }
50
51 _LocaleImpl::_LocaleImpl(const char* pLanguage, const char* pCountry, const char* pVariant)
52         : __icuLocale(pLanguage, pCountry, pVariant)
53 {
54         if (pLanguage == null || *pLanguage == '\0' || pCountry == null || *pCountry == '\0')
55         {
56                 __icuLocale.setToBogus();
57         }
58 }
59
60 _LocaleImpl::_LocaleImpl(const U_ICU_NAMESPACE::Locale& icuLocale)
61         : __icuLocale(icuLocale)
62 {
63 }
64
65 _LocaleImpl::~_LocaleImpl(void)
66 {
67 }
68
69 _LocaleImpl*
70 _LocaleImpl::CloneN(void)
71 {
72         return new (std::nothrow) _LocaleImpl(__icuLocale);
73 }
74
75 U_ICU_NAMESPACE::Locale
76 _LocaleImpl::GetIcuLocale (void) const
77 {
78         return __icuLocale;
79 }
80
81 Locale
82 _LocaleImpl::GetOspLocale(void)
83 {
84         LanguageCode languageCode = Locale::StringToLanguageCode(GetLanguageCodeString(false));
85         CountryCode countryCode = Locale::StringToCountryCode(Get2LetterCountryCodeString());
86         String variant(GetVariantCodeString());
87         return Locale(languageCode, countryCode, &variant);
88 }
89
90 bool
91 _LocaleImpl::operator ==(const _LocaleImpl& otherLocale) const
92 {
93         return __icuLocale == otherLocale.__icuLocale;
94 }
95
96 bool
97 _LocaleImpl::operator !=(const _LocaleImpl& otherLocale) const
98 {
99         return !(*this == otherLocale);
100 }
101
102 bool
103 _LocaleImpl::Equals(const Object& obj) const
104 {
105         const _LocaleImpl* pOtherLocale = dynamic_cast< const _LocaleImpl* >(&obj);
106         if (pOtherLocale)
107         {
108                 return *this == *pOtherLocale;
109         }
110         return false;
111 }
112
113 int
114 _LocaleImpl::GetHashCode(void) const
115 {
116         return __icuLocale.hashCode();
117 }
118
119 String
120 _LocaleImpl::GetLanguageCodeString(bool isTwoLetter)
121 {
122         String languageCode((isTwoLetter ? __icuLocale.getLanguage() : __icuLocale.getISO3Language()));
123         String languageScriptTmp(__icuLocale.getScript());
124
125         if (!languageScriptTmp.IsEmpty())
126         {
127                 String languageScript;
128                 languageScriptTmp.ToLowerCase(languageScript);
129                 languageCode.Append(L"-");
130                 languageCode.Append(languageScript);
131         }
132         return languageCode;
133 }
134
135 result
136 _LocaleImpl::GetLanguageName(String& languageName) const
137 {
138         Locale loc = _LocaleManagerImpl::GetSystemLocale();
139         return GetLanguageName(*loc.__pLocaleImpl, languageName);
140 }
141
142 result
143 _LocaleImpl::GetLanguageName(const _LocaleImpl& otherLocale, String& languageName) const
144 {
145         U_ICU_NAMESPACE::UnicodeString icuStr;
146         languageName = _LocaleData::GetOspString(__icuLocale.getDisplayLanguage(otherLocale.__icuLocale, icuStr));
147         if (!languageName.IsEmpty())
148         {
149                 String languageScriptTmp(__icuLocale.getScript());
150
151                 if (!languageScriptTmp.IsEmpty())
152                 {
153                         U_ICU_NAMESPACE::UnicodeString icuScriptStr;
154                         String languageScriptName = _LocaleData::GetOspString(__icuLocale.getDisplayScript(otherLocale.__icuLocale, icuScriptStr));
155                         languageName.Insert(L" (", languageName.GetLength());
156                         languageName.Insert(languageScriptName, languageName.GetLength());
157                         languageName.Insert(L")", languageName.GetLength());
158                 }
159                 return E_SUCCESS;
160         }
161         return E_UNSUPPORTED_OPERATION;
162 }
163
164 const char*
165 _LocaleImpl::Get2LetterCountryCodeString(void) const
166 {
167         return __icuLocale.getCountry();
168 }
169
170 const char*
171 _LocaleImpl::Get3LetterCountryCodeString(void) const
172 {
173         return __icuLocale.getISO3Country();
174 }
175
176 result
177 _LocaleImpl::GetCountryName(String& countryName) const
178 {
179         Locale loc = _LocaleManagerImpl::GetSystemLocale();
180         return GetCountryName(*loc.__pLocaleImpl, countryName);
181 }
182
183 result
184 _LocaleImpl::GetCountryName(const _LocaleImpl& otherLocale, String& countryName) const
185 {
186         U_ICU_NAMESPACE::UnicodeString icuStr;
187         countryName = _LocaleData::GetOspString(__icuLocale.getDisplayCountry(otherLocale.__icuLocale, icuStr));
188         if (!countryName.IsEmpty())
189         {
190                 return E_SUCCESS;
191         }
192         return E_UNSUPPORTED_OPERATION;
193 }
194
195
196 const char*
197 _LocaleImpl::GetVariantCodeString(void) const
198 {
199         return (IsSupported() == true) ? __icuLocale.getVariant() : null;
200 }
201
202 void
203 _LocaleImpl::SetVariantCodeString(const char* pVariantCode)
204 {
205         __icuLocale = U_ICU_NAMESPACE::Locale(__icuLocale.getLanguage(), __icuLocale.getCountry(), pVariantCode);
206 }
207
208 const char*
209 _LocaleImpl::GetLocaleCodeString(void) const
210 {
211         return __icuLocale.getName();
212 }
213
214 bool
215 _LocaleImpl::IsSupported(void) const
216 {
217         return !__icuLocale.isBogus();
218 }
219
220 bool
221 _LocaleImpl::IsSupported(const Locale& ospLocale)
222 {
223     return !ospLocale.__pLocaleImpl->__icuLocale.isBogus();
224 }
225
226 const _LocaleImpl*
227 _LocaleImpl::GetLocaleImpl(const Locale& ospLocale)
228 {
229         return ospLocale.__pLocaleImpl;
230 }
231
232 String
233 _LocaleImpl::Get2LetterLanguageCodeString(const char* pLanguageCodeString)
234 {
235         if (pLanguageCodeString)
236         {
237                 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
238                 return localeImpl.GetLanguageCodeString(true);
239         }
240         return null;
241 }
242
243 String
244 _LocaleImpl::Get3LetterLanguageCodeString(const char* pLanguageCodeString)
245 {
246         if (pLanguageCodeString)
247         {
248                 _LocaleImpl localeImpl(pLanguageCodeString, "US", null);
249                 return localeImpl.GetLanguageCodeString(false);
250         }
251         return null;
252 }
253
254 const char*
255 _LocaleImpl::Get2LetterCountryCodeString(const char* pCountryCodeString)
256 {
257         if (pCountryCodeString && strlen(pCountryCodeString) == 3)
258         {
259                 _LocaleImpl localeImpl("en", pCountryCodeString, null);
260                 return localeImpl.Get2LetterCountryCodeString();
261         }
262         return null;
263 }
264
265 const char*
266 _LocaleImpl::Get3LetterCountryCodeString(const char* pCountryCodeString)
267 {
268         if (pCountryCodeString)
269         {
270                 _LocaleImpl localeImpl("en", pCountryCodeString, null);
271                 return localeImpl.Get3LetterCountryCodeString();
272         }
273         return null;
274 }
275
276 };
277 };      // Tizen::Locales
278