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__"
38 using namespace tizen_base;
40 namespace notification {
43 MultiLanguage::MultiLanguage(string msgid, string format,
45 : impl_(new Impl(msgid, format, args, this)) {
48 MultiLanguage::MultiLanguage(string serialized)
49 : impl_(new Impl("", "", this)) {
54 MultiLanguage::~MultiLanguage() = default;
55 MultiLanguage::Impl::~Impl() = default;
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");
63 MultiLanguage::Impl::Impl(string msgid, string format,
64 MultiLanguage* parent)
65 : msgid_(msgid), format_(format), parent_(parent) {
66 LOGI("MultiLanguage impl created");
69 Bundle MultiLanguage::Serialize() const {
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_);
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);
85 void MultiLanguage::UpdateString() {
86 UpdateString(util::GetDomainName(), util::GetLocaleDirectory());
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_;
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);
99 LOGI("base str(%s)", base_str.c_str());
101 for (string::iterator it = base_str.begin(); *it; ++it) {
104 char next_ch = *(it + 1);
105 if (next_ch != 'd' && next_ch != 'f' && next_ch != 's')
108 size_t pos = base_str.find("%" + string(1, next_ch));
109 base_str = base_str.replace(pos, 2, impl_->args_[arg_pos]);
112 impl_->translated_ = base_str;
113 LOGI("translated(%s)", impl_->translated_.c_str());
116 string MultiLanguage::GetTranslatedString() {
117 return impl_->translated_;
121 } // namespace notification