From d511ac0562438adbd7c49d0ebfa3ce3e0c4efbfd Mon Sep 17 00:00:00 2001 From: Kamil Lipiszko Date: Wed, 26 Oct 2016 13:04:18 +0200 Subject: [PATCH] utils: translation static class Adds Translate class to enable translating with its static methods. Change-Id: I2e6a85e58f121939d82688ec0a7c3f4f7c07aed6 --- clock/inc/Utils/Translate.h | 63 +++++++++++++++++++++++++++++++++++++++++++ clock/src/Utils/Translate.cpp | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 clock/inc/Utils/Translate.h create mode 100644 clock/src/Utils/Translate.cpp diff --git a/clock/inc/Utils/Translate.h b/clock/inc/Utils/Translate.h new file mode 100644 index 0000000..5bd2908 --- /dev/null +++ b/clock/inc/Utils/Translate.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _CLOCK_UTILS_TRANSLATOR_H_ +#define _CLOCK_UTILS_TRANSLATOR_H_ + +#include + +namespace utils { + +class Translate { +public: + + /** + * @brief Returns string with translation dependent on local language + * in singular form. + * + * Function gets message id set up in po file and returns string + * in currently set language. + * + * @param[in] msgid The id of the text set up in po files. + * @param[in] ... Arguments for the format. + * + * @return String of the text in local language. + */ + static std::string Sprintf(const char *msgid, ...); + + /** + * @brief Returns string with translation dependent on local language + * in singular or plural form. + * + * Function gets message id of the singular set up in po file and + * returns string in currently set language with variadic number + * of arguments and form dependent on count number. + * + * @param[in] count Value the form depends on. + * @param[in] singularForm The id of the singular form. + * @param[in] pluralForm The id of the plural form. + * @param[in] ... Arguments for the format. + * + * @return String of the text in local language.. + */ + static std::string Sprintf(int count, const char *singularForm, const char *pluralForm, ...); + +private: + Translate() {}; +}; +} + +#endif //_CLOCK_UTILS_TRANSLATOR_H_ diff --git a/clock/src/Utils/Translate.cpp b/clock/src/Utils/Translate.cpp new file mode 100644 index 0000000..4125794 --- /dev/null +++ b/clock/src/Utils/Translate.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "Utils/Translate.h" + +namespace utils { + +std::string Translate::Sprintf(const char *msgid, ...) +{ + char *result = NULL; + va_list args; + va_start(args, msgid); + + int ret = vasprintf(&result, gettext(msgid), args); + if (ret == -1) + throw std::bad_alloc(); + va_end(args); + + std::string resultStr(result); + free(result); + + return resultStr; +} + +std::string Translate::Sprintf(int count, const char *singularForm, const char *pluralForm, ...) +{ + char *result = NULL; + va_list args, copy; + va_start(args, pluralForm); + + int ret = vasprintf(&result, ngettext(singularForm, pluralForm, count), args); + if (ret == -1) + throw std::bad_alloc(); + va_end(args); + + std::string resultStr(result); + free(result); + + return resultStr; +} + +} -- 2.7.4