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>
33 static int LanguageChanged(void *)
35 char* lang = vconf_get_str(VCONFKEY_LANGSET);
37 LogError("Cannot get locale settings from vconf");
40 LogDebug("Language set to: " << lang);
42 using namespace LocalizationUtils;
44 LanguageTagsList list;
45 list.push_back(DPL::FromUTF8String(lang));
46 SetSystemLanguageTags(list);
48 LogDebug("LanguageChanged to " << lang);
54 namespace LocalizationUtils {
55 static LanguageTagsList m_systemLanguageTags;
56 static LanguageTagsList m_userLanguageTags;
57 static LanguageTagsList m_languageTags;
58 static DPL::ReadWriteMutex m_readWriteMutex;
60 template<typename StringType>
61 void FindAndReplace(StringType& source,
62 const StringType& find,
63 const StringType& replace)
66 while ((pos = source.find(find, pos)) != StringType::npos) {
67 source.replace(pos, find.length(), replace);
68 pos += replace.length();
72 DPL::String BCP47LanguageTagToLocale(const DPL::String& inLanguageTag)
74 DPL::String languageTag(inLanguageTag);
75 FindAndReplace(languageTag, DPL::String(L"-"), DPL::String(L"_"));
79 DPL::String LocaleToBCP47LanguageTag(const DPL::String& inLocaleString)
81 DPL::String localeString = inLocaleString.substr(
85 FindAndReplace(localeString, DPL::String(L"_"), DPL::String(L"-"));
89 void UpdateUserAgentLanguageTags()
91 // WARNING!!!!! This function shall be called
92 // only when mutex is locked in readWriteMode!
94 LanguageTagsList list = m_userLanguageTags;
95 list.insert(list.begin(),
96 m_systemLanguageTags.begin(),
97 m_systemLanguageTags.end());
98 m_languageTags.clear();
101 DPL::String tag = LocaleToBCP47LanguageTag(*i);
102 while (true) { //W3C Packaging 9. Step 5. 2. D
103 if (tag.empty()) { continue; }
105 LogDebug("Adding user locale '" << tag << "'");
106 m_languageTags.push_back(tag);
108 size_t subtagPos = tag.find_last_of(L"-");
109 if (subtagPos == DPL::String::npos) {
112 tag = tag.substr(0, subtagPos);
116 m_languageTags.push_back(L"en");
117 m_languageTags.push_back(L"");
120 void SetUserLanguageTags(const LanguageTagsList& tags)
122 DPL::ReadWriteMutex::ScopedWriteLock lock(&m_readWriteMutex);
123 if (m_userLanguageTags != tags) {
124 m_userLanguageTags = tags;
125 UpdateUserAgentLanguageTags();
129 void SetSystemLanguageTags(const LanguageTagsList& tags)
131 DPL::ReadWriteMutex::ScopedWriteLock lock(&m_readWriteMutex);
132 if (m_systemLanguageTags != tags) {
133 m_systemLanguageTags = tags;
134 UpdateUserAgentLanguageTags();
138 LanguageTagsList GetUserAgentLanguageTags()
140 DPL::ReadWriteMutex::ScopedReadLock lock(&m_readWriteMutex);
141 return m_languageTags;
146 appcore_set_event_callback(
147 APPCORE_EVENT_LANG_CHANGE,
151 LanguageChanged(NULL); // updating language for the first time