Release version 0.5.85
[platform/core/api/notification.git] / notification-ex / multi_language.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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 <dlog.h>
18 #include <locale.h>
19 #include <libintl.h>
20
21 #include <memory>
22
23 #include "notification-ex/ex_util.h"
24 #include "notification-ex/multi_language.h"
25 #include "notification-ex/multi_language_implementation.h"
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG "NOTIFICATION_EX"
32 #define MULTI_LANGUAGE_MSG_ID_KEY "__MULTI_LANGUAGE_MSG_ID_KEY__"
33 #define MULTI_LANGUAGE_FORMAT_KEY "__MULTI_LANGUAGE_FORMAT_KEY__"
34 #define MULTI_LANGUAGE_ARGS_KEY "__MULTI_LANGUAGE_ARGS_KEY__"
35 #define MULTI_LANGUAGE_TRANSLATED_KEY "__MULTI_LANGUAGE_TRANSLATED_KEY__"
36 #define MULTI_LANGUAGE_DOMAIN_KEY "__MULTI_LANGUAGE_DOMAIN_KEY__"
37 #define MULTI_LANGUAGE_LOCALE_KEY "__MULTI_LANGUAGE_LOCALE_KEY__"
38
39 using namespace std;
40 using namespace tizen_base;
41
42 namespace notification {
43 namespace item {
44
45 MultiLanguage::MultiLanguage(string msgid, string format,
46       vector<string> args)
47     : impl_(new Impl(msgid, format, args, this)) {
48 }
49
50 MultiLanguage::MultiLanguage(string serialized)
51     : impl_(new Impl("", "", this)) {
52   Bundle b(serialized);
53   Deserialize(b);
54 }
55
56 MultiLanguage::~MultiLanguage() = default;
57 MultiLanguage::Impl::~Impl() = default;
58
59 MultiLanguage::Impl::Impl(string msgid, string format, vector<string> args,
60     MultiLanguage* parent)
61     : msgid_(msgid), format_(format), args_(args), parent_(parent) {
62   LOGI("MultiLanguage impl created");
63 }
64
65 MultiLanguage::Impl::Impl(string msgid, string format,
66     MultiLanguage* parent)
67     : msgid_(msgid), format_(format), parent_(parent) {
68   LOGI("MultiLanguage impl created");
69 }
70
71 Bundle MultiLanguage::Serialize() const {
72   Bundle b;
73   b.Add(MULTI_LANGUAGE_MSG_ID_KEY, impl_->msgid_);
74   b.Add(MULTI_LANGUAGE_FORMAT_KEY, impl_->format_);
75   b.Add(MULTI_LANGUAGE_ARGS_KEY, impl_->args_);
76   b.Add(MULTI_LANGUAGE_TRANSLATED_KEY, impl_->translated_);
77   b.Add(MULTI_LANGUAGE_DOMAIN_KEY, impl_->domain_name_);
78   b.Add(MULTI_LANGUAGE_LOCALE_KEY, impl_->locale_directory_);
79   return b;
80 }
81
82 void MultiLanguage::Deserialize(Bundle b) {
83   impl_->msgid_ = b.GetString(MULTI_LANGUAGE_MSG_ID_KEY);
84   impl_->format_ = b.GetString(MULTI_LANGUAGE_FORMAT_KEY);
85   impl_->args_ = b.GetStringArray(MULTI_LANGUAGE_ARGS_KEY);
86   impl_->translated_ = b.GetString(MULTI_LANGUAGE_TRANSLATED_KEY);
87   impl_->domain_name_ = b.GetString(MULTI_LANGUAGE_DOMAIN_KEY);
88   impl_->locale_directory_ = b.GetString(MULTI_LANGUAGE_LOCALE_KEY);
89 }
90
91 void MultiLanguage::UpdateString() {
92   if (impl_->domain_name_.empty())
93     impl_->domain_name_ = util::GetDomainName();
94   if (impl_->locale_directory_.empty())
95     impl_->locale_directory_ = util::GetLocaleDirectory();
96
97   UpdateString(impl_->domain_name_, impl_->locale_directory_);
98 }
99
100 void MultiLanguage::UpdateString(string domain, string locale_directory) {
101   bindtextdomain(domain.c_str(), locale_directory.c_str());
102   char* ret_str = dgettext(domain.c_str(), impl_->msgid_.c_str());
103   string base_str = impl_->format_;
104
105   LOGI("ret str(%s)", ret_str);
106   if (ret_str != nullptr && (strcmp(ret_str, impl_->msgid_.c_str()) != 0)) {
107     base_str = string(ret_str);
108   }
109
110   LOGI("base str(%s)", base_str.c_str());
111   int arg_pos = 0;
112   size_t pos = base_str.find("%");
113   while (pos != string::npos && (pos + 2) <= base_str.length()) {
114     char next_ch = base_str[pos + 1];
115     if (next_ch != 'd' && next_ch != 'f' && next_ch != 's') {
116       pos++;
117       pos = base_str.find("%", pos);
118       continue;
119     }
120     base_str.replace(pos, 2, impl_->args_[arg_pos]);
121     pos += impl_->args_[arg_pos].length();
122     pos = base_str.find("%", pos);
123     arg_pos++;
124   }
125
126   impl_->translated_ = base_str;
127   LOGI("translated(%s)", impl_->translated_.c_str());
128 }
129
130 string MultiLanguage::GetTranslatedString() {
131   return impl_->translated_;
132 }
133
134 }  // namespace item
135 }  // namespace notification