2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file localization_utils.cpp
18 * @author Bartosz Janiak (b.janiak@samsung.com)
22 #include <dpl/localization/localization_utils.h>
24 #include <appcore-efl.h>
26 #include <dpl/framework_efl.h>
28 #include <dpl/foreach.h>
29 #include <dpl/mutex.h>
30 #include <dpl/wrt-dao-rw/widget_dao.h>
34 static int LanguageChanged(void *)
36 char* lang = vconf_get_str(VCONFKEY_LANGSET);
38 LogError("Cannot get locale settings from vconf");
41 LogDebug("Language set to: " << lang);
43 using namespace LocalizationUtils;
45 LanguageTagsList list;
46 list.push_back(DPL::FromUTF8String(lang));
47 SetSystemLanguageTags(list);
49 LogDebug("LanguageChanged to " << lang);
55 namespace LocalizationUtils {
56 static LanguageTagsList m_systemLanguageTags;
57 static LanguageTagsList m_userLanguageTags;
58 static LanguageTagsList m_languageTags;
59 static DPL::ReadWriteMutex m_readWriteMutex;
61 template<typename StringType>
62 void FindAndReplace(StringType& source,
63 const StringType& find,
64 const StringType& replace)
67 while ((pos = source.find(find, pos)) != StringType::npos) {
68 source.replace(pos, find.length(), replace);
69 pos += replace.length();
73 DPL::String BCP47LanguageTagToLocale(const DPL::String& inLanguageTag)
75 DPL::String languageTag(inLanguageTag);
76 FindAndReplace(languageTag, DPL::String(L"-"), DPL::String(L"_"));
80 DPL::String LocaleToBCP47LanguageTag(const DPL::String& inLocaleString)
82 DPL::String localeString = inLocaleString.substr(
86 FindAndReplace(localeString, DPL::String(L"_"), DPL::String(L"-"));
90 void UpdateUserAgentLanguageTags()
92 // WARNING!!!!! This function shall be called
93 // only when mutex is locked in readWriteMode!
95 LanguageTagsList list = m_userLanguageTags;
96 list.insert(list.begin(),
97 m_systemLanguageTags.begin(),
98 m_systemLanguageTags.end());
99 m_languageTags.clear();
102 DPL::String tag = LocaleToBCP47LanguageTag(*i);
103 while (true) { //W3C Packaging 9. Step 5. 2. D
104 if (tag.empty()) { continue; }
106 LogDebug("Adding user locale '" << tag << "'");
107 m_languageTags.push_back(tag);
109 size_t subtagPos = tag.find_last_of(L"-");
110 if (subtagPos == DPL::String::npos) {
113 tag = tag.substr(0, subtagPos);
117 m_languageTags.push_back(L"en");
118 m_languageTags.push_back(L"");
121 void SetUserLanguageTags(const LanguageTagsList& tags)
123 DPL::ReadWriteMutex::ScopedWriteLock lock(&m_readWriteMutex);
124 if (m_userLanguageTags != tags) {
125 m_userLanguageTags = tags;
126 UpdateUserAgentLanguageTags();
130 void SetSystemLanguageTags(const LanguageTagsList& tags)
132 DPL::ReadWriteMutex::ScopedWriteLock lock(&m_readWriteMutex);
133 if (m_systemLanguageTags != tags) {
134 m_systemLanguageTags = tags;
135 UpdateUserAgentLanguageTags();
139 LanguageTagsList GetUserAgentLanguageTags()
141 DPL::ReadWriteMutex::ScopedReadLock lock(&m_readWriteMutex);
142 return m_languageTags;
147 appcore_set_event_callback(
148 APPCORE_EVENT_LANG_CHANGE,
152 LanguageChanged(NULL); // updating language for the first time