Fix invalid licenses
[platform/framework/web/nwrt.git] / src / common / locale_manager.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "common/locale_manager.h"
18
19 #include <runtime_info.h>
20 #include <memory>
21 #include <algorithm>
22
23 #include "common/logger.h"
24 #include "common/file_utils.h"
25
26 namespace wrt {
27
28 namespace {
29
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("."));
35
36   // Replace all '_' with '-'
37   std::replace(lang.begin(), lang.end(), '_', '-');
38   return lang;
39 }
40
41 }  // namespace
42
43
44 LocaleManager::LocaleManager() {
45   UpdateSystemLocale();
46 }
47
48 LocaleManager::~LocaleManager() {
49   runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_LANGUAGE);
50 }
51
52 void LocaleManager::EnableAutoUpdate(bool enable) {
53   if (enable) {
54     auto callback = [](runtime_info_key_e, void* user_data) {
55         LocaleManager* locale = static_cast<LocaleManager*>(user_data);
56         locale->UpdateSystemLocale();
57     };
58     runtime_info_set_changed_cb(RUNTIME_INFO_KEY_LANGUAGE, callback, this);
59   } else {
60     runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_LANGUAGE);
61   }
62 }
63
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();
68   }
69   default_locale_ = locale;
70   if (!default_locale_.empty()) {
71     system_locales_.push_back(locale);
72   }
73 }
74
75 void LocaleManager::UpdateSystemLocale() {
76   char* str = NULL;
77   if (RUNTIME_INFO_ERROR_NONE !=
78       runtime_info_get_value_string(RUNTIME_INFO_KEY_LANGUAGE, &str)
79      || str == NULL) {
80     return;
81   }
82   std::string lang = localeToBCP47LangTag(str);
83   free(str);
84
85   if (lang.length() == 0) {
86     LOGGER(ERROR) << "Language tag was invalid";
87     return;
88   }
89
90   system_locales_.clear();
91   while (true) {
92     LOGGER(DEBUG) << "Processing language description: " << lang;
93     system_locales_.push_back(lang);
94
95     // compatibility with lower language Tag by SDK
96     std::string lower = lang;
97     std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
98     if (lower != lang) {
99       system_locales_.push_back(lower);
100     }
101     size_t position = lang.find_last_of("-");
102     if (position == std::string::npos) {
103       break;
104     }
105     lang = lang.substr(0, position);
106   }
107   if (!default_locale_.empty()) {
108     system_locales_.push_back(default_locale_);
109   }
110 }
111
112 std::string LocaleManager::GetLocalizedString(const StringMap& strmap) {
113   if (strmap.empty()) {
114     return std::string();
115   }
116
117   // find string with system locales
118   for (auto& locale : system_locales_) {
119     auto it = strmap.find(locale);
120     if (it != strmap.end()) {
121       return it->second;
122     }
123   }
124
125   // find string with empty locale
126   auto it = strmap.find("");
127   if (it != strmap.end()) {
128     return it->second;
129   }
130
131   // If localized string is not found, return first string.
132   return strmap.begin()->second;
133 }
134
135 }  // namespace wrt