2 * Copyright (c) 2015 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 #include "common/locale_manager.h"
19 #include <runtime_info.h>
23 #include "common/logger.h"
24 #include "common/file_utils.h"
30 std::string localeToBCP47LangTag(
31 const std::string locale) {
32 // Cut off codepage information from given string (if any exists)
33 // i.e. change en_US.UTF-8 into en_US */
34 std::string lang = locale.substr(0, locale.find_first_of("."));
36 // Replace all '_' with '-'
37 std::replace(lang.begin(), lang.end(), '_', '-');
44 LocaleManager::LocaleManager() {
48 LocaleManager::~LocaleManager() {
49 runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_LANGUAGE);
52 void LocaleManager::EnableAutoUpdate(bool enable) {
54 auto callback = [](runtime_info_key_e, void* user_data) {
55 LocaleManager* locale = static_cast<LocaleManager*>(user_data);
56 locale->UpdateSystemLocale();
58 runtime_info_set_changed_cb(RUNTIME_INFO_KEY_LANGUAGE, callback, this);
60 runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_LANGUAGE);
64 void LocaleManager::SetDefaultLocale(const std::string& locale) {
65 if (!default_locale_.empty() && system_locales_.size() > 0 &&
66 system_locales_.back() == default_locale_) {
67 system_locales_.pop_back();
69 default_locale_ = locale;
70 if (!default_locale_.empty()) {
71 system_locales_.push_back(locale);
75 void LocaleManager::UpdateSystemLocale() {
77 if (RUNTIME_INFO_ERROR_NONE !=
78 runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &str)
82 std::string lang = localeToBCP47LangTag(str);
85 if (lang.length() == 0) {
86 LOGGER(ERROR) << "Language tag was invalid";
90 system_locales_.clear();
92 LOGGER(DEBUG) << "Processing language description: " << lang;
93 system_locales_.push_back(lang);
95 // compatibility with lower language Tag by SDK
96 std::string lower = lang;
97 std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
99 system_locales_.push_back(lower);
101 size_t position = lang.find_last_of("-");
102 if (position == std::string::npos) {
105 lang = lang.substr(0, position);
107 if (!default_locale_.empty()) {
108 system_locales_.push_back(default_locale_);
112 std::string LocaleManager::GetLocalizedString(const StringMap& strmap) {
113 if (strmap.empty()) {
114 return std::string();
117 // find string with system locales
118 for (auto& locale : system_locales_) {
119 auto it = strmap.find(locale);
120 if (it != strmap.end()) {
125 // find string with empty locale
126 auto it = strmap.find("");
127 if (it != strmap.end()) {
131 // If localized string is not found, return first string.
132 return strmap.begin()->second;