mirror of https://github.com/kiwix/libkiwix.git
Made some i18n API public
This commit is contained in:
parent
2535f210b3
commit
82ff88f5d8
|
@ -0,0 +1,90 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2024 Veloman Yunkan <veloman.yunkan@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace kiwix
|
||||||
|
{
|
||||||
|
|
||||||
|
std::string getTranslatedString(const std::string& lang, const std::string& key);
|
||||||
|
|
||||||
|
namespace i18n
|
||||||
|
{
|
||||||
|
|
||||||
|
typedef std::map<std::string, std::string> 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
|
|
@ -10,7 +10,8 @@ headers = [
|
||||||
'kiwixserve.h',
|
'kiwixserve.h',
|
||||||
'name_mapper.h',
|
'name_mapper.h',
|
||||||
'tools.h',
|
'tools.h',
|
||||||
'version.h'
|
'version.h',
|
||||||
|
'i18n.h'
|
||||||
]
|
]
|
||||||
|
|
||||||
install_headers(headers, subdir:'kiwix')
|
install_headers(headers, subdir:'kiwix')
|
||||||
|
|
|
@ -20,8 +20,7 @@
|
||||||
#ifndef KIWIX_SERVER_I18N_UTILS
|
#ifndef KIWIX_SERVER_I18N_UTILS
|
||||||
#define KIWIX_SERVER_I18N_UTILS
|
#define KIWIX_SERVER_I18N_UTILS
|
||||||
|
|
||||||
#include <map>
|
#include "i18n.h"
|
||||||
#include <string>
|
|
||||||
#include <mustache.hpp>
|
#include <mustache.hpp>
|
||||||
|
|
||||||
namespace kiwix
|
namespace kiwix
|
||||||
|
@ -40,36 +39,9 @@ struct I18nStringTable {
|
||||||
const char* get(const std::string& key) const;
|
const char* get(const std::string& key) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string getTranslatedString(const std::string& lang, const std::string& key);
|
|
||||||
|
|
||||||
namespace i18n
|
namespace i18n
|
||||||
{
|
{
|
||||||
|
|
||||||
typedef std::map<std::string, std::string> 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
|
class GetTranslatedStringWithMsgId
|
||||||
{
|
{
|
||||||
typedef kainjow::mustache::basic_data<std::string> MustacheString;
|
typedef kainjow::mustache::basic_data<std::string> MustacheString;
|
||||||
|
@ -94,33 +66,6 @@ private:
|
||||||
|
|
||||||
} // namespace i18n
|
} // 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
|
struct LangPreference
|
||||||
{
|
{
|
||||||
const std::string lang;
|
const std::string lang;
|
||||||
|
|
Loading…
Reference in New Issue