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() {
39 void LocaleManager::SetDefaultLocale(const std::string& locale) {
40 if (!default_locale_.empty() && system_locales_.size() > 0 &&
41 system_locales_.back() == default_locale_) {
42 system_locales_.pop_back();
44 default_locale_ = locale;
45 if (!default_locale_.empty()) {
46 system_locales_.push_back(locale);
50 void LocaleManager::UpdateSystemLocale() {
52 if (RUNTIME_INFO_ERROR_NONE !=
53 runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &str)
57 std::string lang = localeToBCP47LangTag(str);
59 if (lang.length() == 0) {
60 LoggerE("Language tag was invalid");
64 system_locales_.clear();
66 LoggerD("Processing language description: %s", lang.c_str());
67 system_locales_.push_back(lang);
69 // compatibility with lower language Tag by SDK
70 std::string lower = lang;
71 std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
73 system_locales_.push_back(lower);
75 size_t position = lang.find_last_of("-");
76 if (position == std::string::npos) {
79 lang = lang.substr(0, position);
81 if (!default_locale_.empty()) {
82 system_locales_.push_back(default_locale_);