Merge "Fix build warning based on GCC-9" into tizen
[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
37 using namespace std;
38 using namespace tizen_base;
39
40 namespace notification {
41 namespace item {
42
43 MultiLanguage::MultiLanguage(string msgid, string format,
44       vector<string> args)
45     : impl_(new Impl(msgid, format, args, this)) {
46 }
47
48 MultiLanguage::MultiLanguage(string serialized)
49     : impl_(new Impl("", "", this)) {
50   Bundle b(serialized);
51   Deserialize(b);
52 }
53
54 MultiLanguage::~MultiLanguage() = default;
55 MultiLanguage::Impl::~Impl() = default;
56
57 MultiLanguage::Impl::Impl(string msgid, string format, vector<string> args,
58     MultiLanguage* parent)
59     : msgid_(msgid), format_(format), args_(args), parent_(parent) {
60   LOGI("MultiLanguage impl created");
61 }
62
63 MultiLanguage::Impl::Impl(string msgid, string format,
64     MultiLanguage* parent)
65     : msgid_(msgid), format_(format), parent_(parent) {
66   LOGI("MultiLanguage impl created");
67 }
68
69 Bundle MultiLanguage::Serialize() const {
70   Bundle b;
71   b.Add(MULTI_LANGUAGE_MSG_ID_KEY, impl_->msgid_);
72   b.Add(MULTI_LANGUAGE_FORMAT_KEY, impl_->format_);
73   b.Add(MULTI_LANGUAGE_ARGS_KEY, impl_->args_);
74   b.Add(MULTI_LANGUAGE_TRANSLATED_KEY, impl_->translated_);
75   return b;
76 }
77
78 void MultiLanguage::Deserialize(Bundle b) {
79   impl_->msgid_ = b.GetString(MULTI_LANGUAGE_MSG_ID_KEY);
80   impl_->format_ = b.GetString(MULTI_LANGUAGE_FORMAT_KEY);
81   impl_->args_ = b.GetStringArray(MULTI_LANGUAGE_ARGS_KEY);
82   impl_->translated_ = b.GetString(MULTI_LANGUAGE_TRANSLATED_KEY);
83 }
84
85 void MultiLanguage::UpdateString() {
86   UpdateString(util::GetDomainName(), util::GetLocaleDirectory());
87 }
88
89 void MultiLanguage::UpdateString(string domain, string locale_directory) {
90   bindtextdomain(domain.c_str(), locale_directory.c_str());
91   char* ret_str = dgettext(domain.c_str(), impl_->msgid_.c_str());
92   string base_str = impl_->format_;
93
94   LOGI("ret str(%s)", ret_str);
95   if (ret_str != nullptr && (strcmp(ret_str, impl_->msgid_.c_str()) != 0)) {
96     base_str = string(ret_str);
97   }
98
99   LOGI("base str(%s)", base_str.c_str());
100   int arg_pos = 0;
101   for (string::iterator it = base_str.begin(); *it; ++it) {
102     if (*it != '%')
103       continue;
104     char next_ch = *(it + 1);
105     if (next_ch != 'd' && next_ch != 'f' && next_ch != 's')
106       continue;
107
108     size_t pos = base_str.find("%" + string(1, next_ch));
109     base_str = base_str.replace(pos, 2, impl_->args_[arg_pos]);
110     arg_pos++;
111   }
112   impl_->translated_ = base_str;
113   LOGI("translated(%s)", impl_->translated_.c_str());
114 }
115
116 string MultiLanguage::GetTranslatedString() {
117   return impl_->translated_;
118 }
119
120 }  // namespace item
121 }  // namespace notification