1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "common/locale_manager.h"
7 #include <runtime_info.h>
11 #include "common/logger.h"
12 #include "common/file_utils.h"
18 std::string localeToBCP47LangTag(
19 const std::string locale) {
20 // Cut off codepage information from given string (if any exists)
21 // i.e. change en_US.UTF-8 into en_US */
22 std::string lang = locale.substr(0, locale.find_first_of("."));
24 // Replace all '_' with '-'
25 std::replace(lang.begin(), lang.end(), '_', '-');
32 LocaleManager::LocaleManager() {
36 LocaleManager::~LocaleManager() {
37 runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_LANGUAGE);
40 void LocaleManager::EnableAutoUpdate(bool enable) {
42 auto callback = [](runtime_info_key_e, void* user_data) {
43 LocaleManager* locale = static_cast<LocaleManager*>(user_data);
44 locale->UpdateSystemLocale();
46 runtime_info_set_changed_cb(RUNTIME_INFO_KEY_LANGUAGE, callback, this);
48 runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_LANGUAGE);
52 void LocaleManager::SetDefaultLocale(const std::string& locale) {
53 if (!default_locale_.empty() && system_locales_.size() > 0 &&
54 system_locales_.back() == default_locale_) {
55 system_locales_.pop_back();
57 default_locale_ = locale;
58 if (!default_locale_.empty()) {
59 system_locales_.push_back(locale);
63 void LocaleManager::UpdateSystemLocale() {
65 if (RUNTIME_INFO_ERROR_NONE !=
66 runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &str)
70 std::string lang = localeToBCP47LangTag(str);
73 if (lang.length() == 0) {
74 LOGGER(ERROR) << "Language tag was invalid";
78 system_locales_.clear();
80 LOGGER(DEBUG) << "Processing language description: " << lang;
81 system_locales_.push_back(lang);
83 // compatibility with lower language Tag by SDK
84 std::string lower = lang;
85 std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
87 system_locales_.push_back(lower);
89 size_t position = lang.find_last_of("-");
90 if (position == std::string::npos) {
93 lang = lang.substr(0, position);
95 if (!default_locale_.empty()) {
96 system_locales_.push_back(default_locale_);