diff --git a/include/i18n.h b/include/i18n.h new file mode 100644 index 000000000..381232985 --- /dev/null +++ b/include/i18n.h @@ -0,0 +1,90 @@ +/* + * Copyright 2024 Veloman Yunkan + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#ifndef KIWIX_I18N +#define KIWIX_I18N + +#include +#include + +namespace kiwix +{ + +std::string getTranslatedString(const std::string& lang, const std::string& key); + +namespace i18n +{ + +typedef std::map Parameters; + +std::string expandParameterizedString(const std::string& lang, + const std::string& key, + const Parameters& params); + +class GetTranslatedString +{ +public: + explicit GetTranslatedString(const std::string& lang) : m_lang(lang) {} + + std::string operator()(const std::string& key) const + { + return getTranslatedString(m_lang, key); + } + + std::string operator()(const std::string& key, const Parameters& params) const + { + return expandParameterizedString(m_lang, key, params); + } + +private: + const std::string m_lang; +}; + +} // namespace i18n + +class ParameterizedMessage +{ +public: // types + typedef i18n::Parameters Parameters; + +public: // functions + ParameterizedMessage(const std::string& msgId, const Parameters& params) + : msgId(msgId) + , params(params) + {} + + std::string getText(const std::string& lang) const; + + const std::string& getMsgId() const { return msgId; } + const Parameters& getParams() const { return params; } + +private: // data + const std::string msgId; + const Parameters params; +}; + +inline ParameterizedMessage nonParameterizedMessage(const std::string& msgId) +{ + const ParameterizedMessage::Parameters noParams; + return ParameterizedMessage(msgId, noParams); +} + +} // namespace kiwix + +#endif // KIWIX_I18N diff --git a/include/meson.build b/include/meson.build index 03e6f0465..4735c2226 100644 --- a/include/meson.build +++ b/include/meson.build @@ -10,7 +10,8 @@ headers = [ 'kiwixserve.h', 'name_mapper.h', 'tools.h', - 'version.h' + 'version.h', + 'i18n.h' ] install_headers(headers, subdir:'kiwix') diff --git a/src/server/i18n_utils.h b/src/server/i18n_utils.h index 5d20d489c..c9672501d 100644 --- a/src/server/i18n_utils.h +++ b/src/server/i18n_utils.h @@ -20,8 +20,7 @@ #ifndef KIWIX_SERVER_I18N_UTILS #define KIWIX_SERVER_I18N_UTILS -#include -#include +#include "i18n.h" #include namespace kiwix @@ -40,36 +39,9 @@ struct I18nStringTable { const char* get(const std::string& key) const; }; -std::string getTranslatedString(const std::string& lang, const std::string& key); - namespace i18n { -typedef std::map Parameters; - -std::string expandParameterizedString(const std::string& lang, - const std::string& key, - const Parameters& params); - -class GetTranslatedString -{ -public: - explicit GetTranslatedString(const std::string& lang) : m_lang(lang) {} - - std::string operator()(const std::string& key) const - { - return getTranslatedString(m_lang, key); - } - - std::string operator()(const std::string& key, const Parameters& params) const - { - return expandParameterizedString(m_lang, key, params); - } - -private: - const std::string m_lang; -}; - class GetTranslatedStringWithMsgId { typedef kainjow::mustache::basic_data MustacheString; @@ -94,33 +66,6 @@ private: } // namespace i18n -class ParameterizedMessage -{ -public: // types - typedef i18n::Parameters Parameters; - -public: // functions - ParameterizedMessage(const std::string& msgId, const Parameters& params) - : msgId(msgId) - , params(params) - {} - - std::string getText(const std::string& lang) const; - - const std::string& getMsgId() const { return msgId; } - const Parameters& getParams() const { return params; } - -private: // data - const std::string msgId; - const Parameters params; -}; - -inline ParameterizedMessage nonParameterizedMessage(const std::string& msgId) -{ - const ParameterizedMessage::Parameters noParams; - return ParameterizedMessage(msgId, noParams); -} - struct LangPreference { const std::string lang;