2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "notification-ex/ex_util.h"
24 #include "notification-ex/multi_language.h"
25 #include "notification-ex/multi_language_implementation.h"
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__"
40 using namespace tizen_base;
42 namespace notification {
45 MultiLanguage::MultiLanguage(string msgid, string format,
47 : impl_(new Impl(msgid, format, args, this)) {
50 MultiLanguage::MultiLanguage(string serialized)
51 : impl_(new Impl("", "", this)) {
56 MultiLanguage::~MultiLanguage() = default;
57 MultiLanguage::Impl::~Impl() = default;
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");
65 MultiLanguage::Impl::Impl(string msgid, string format,
66 MultiLanguage* parent)
67 : msgid_(msgid), format_(format), parent_(parent) {
68 LOGI("MultiLanguage impl created");
71 Bundle MultiLanguage::Serialize() const {
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_);
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);
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();
97 UpdateString(impl_->domain_name_, impl_->locale_directory_);
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_;
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);
110 LOGI("base str(%s)", base_str.c_str());
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') {
117 pos = base_str.find("%", pos);
120 base_str.replace(pos, 2, impl_->args_[arg_pos]);
121 pos += impl_->args_[arg_pos].length();
122 pos = base_str.find("%", pos);
126 impl_->translated_ = base_str;
127 LOGI("translated(%s)", impl_->translated_.c_str());
130 string MultiLanguage::GetTranslatedString() {
131 return impl_->translated_;
135 } // namespace notification