-// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
#include "common/locale_manager.h"
-#include <runtime_info.h>
+#include <system_settings.h>
#include <memory>
#include <algorithm>
}
LocaleManager::~LocaleManager() {
+ system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE);
+}
+
+void LocaleManager::EnableAutoUpdate(bool enable) {
+ if (enable) {
+ auto callback = [](system_settings_key_e, void* user_data) {
+ LocaleManager* locale = static_cast<LocaleManager*>(user_data);
+ locale->UpdateSystemLocale();
+ };
+ system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, callback, this);
+ } else {
+ system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE);
+ }
}
void LocaleManager::SetDefaultLocale(const std::string& locale) {
void LocaleManager::UpdateSystemLocale() {
char* str = NULL;
- if (RUNTIME_INFO_ERROR_NONE !=
- runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &str)
+ if (SYSTEM_SETTINGS_ERROR_NONE !=
+ system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &str)
|| str == NULL) {
return;
}
std::string lang = localeToBCP47LangTag(str);
+ free(str);
if (lang.length() == 0) {
- LoggerE("Language tag was invalid");
+ LOGGER(ERROR) << "Language tag was invalid";
return;
}
system_locales_.clear();
while (true) {
- LoggerD("Processing language description: %s", lang.c_str());
+ LOGGER(DEBUG) << "Processing language description: " << lang;
system_locales_.push_back(lang);
// compatibility with lower language Tag by SDK
}
}
+std::string LocaleManager::GetLocalizedString(const StringMap& strmap) {
+ if (strmap.empty()) {
+ return std::string();
+ }
+
+ // find string with system locales
+ for (auto& locale : system_locales_) {
+ auto it = strmap.find(locale);
+ if (it != strmap.end()) {
+ return it->second;
+ }
+ }
+
+ // find string with empty locale
+ auto it = strmap.find("");
+ if (it != strmap.end()) {
+ return it->second;
+ }
+
+ // If localized string is not found, return first string.
+ return strmap.begin()->second;
+}
+
} // namespace wrt