2019-06-17 03:56:52 -04:00
|
|
|
// Copyright (c) 2019 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_UTIL_TRANSLATION_H
|
|
|
|
#define BITCOIN_UTIL_TRANSLATION_H
|
|
|
|
|
2019-06-28 04:49:40 -04:00
|
|
|
#include <tinyformat.h>
|
2019-10-07 15:11:34 -03:00
|
|
|
#include <functional>
|
2019-06-28 04:49:40 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bilingual messages:
|
|
|
|
* - in GUI: user's native language + untranslated (i.e. English)
|
|
|
|
* - in log and stderr: untranslated only
|
|
|
|
*/
|
|
|
|
struct bilingual_str {
|
|
|
|
std::string original;
|
|
|
|
std::string translated;
|
2020-05-09 07:43:00 -04:00
|
|
|
|
|
|
|
bilingual_str& operator+=(const bilingual_str& rhs)
|
|
|
|
{
|
|
|
|
original += rhs.original;
|
|
|
|
translated += rhs.translated;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-06-01 04:05:15 -04:00
|
|
|
|
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
return original.empty();
|
|
|
|
}
|
2019-06-28 04:49:40 -04:00
|
|
|
};
|
|
|
|
|
2020-05-09 07:43:00 -04:00
|
|
|
inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs)
|
2019-08-19 18:12:35 -04:00
|
|
|
{
|
2020-05-09 07:43:00 -04:00
|
|
|
lhs += rhs;
|
|
|
|
return lhs;
|
2019-08-19 18:12:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Mark a bilingual_str as untranslated */
|
2020-05-04 21:51:29 -04:00
|
|
|
inline bilingual_str Untranslated(std::string original) { return {original, original}; }
|
2019-08-19 18:12:35 -04:00
|
|
|
|
2019-06-28 04:49:40 -04:00
|
|
|
namespace tinyformat {
|
|
|
|
template <typename... Args>
|
|
|
|
bilingual_str format(const bilingual_str& fmt, const Args&... args)
|
|
|
|
{
|
|
|
|
return bilingual_str{format(fmt.original, args...), format(fmt.translated, args...)};
|
|
|
|
}
|
|
|
|
} // namespace tinyformat
|
|
|
|
|
2019-06-17 03:56:52 -04:00
|
|
|
/** Translate a message to the native language of the user. */
|
|
|
|
const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translation function.
|
|
|
|
* If no translation function is set, simply return the input.
|
|
|
|
*/
|
2019-06-28 14:09:58 -04:00
|
|
|
inline bilingual_str _(const char* psz)
|
2019-06-17 03:56:52 -04:00
|
|
|
{
|
2019-06-28 14:09:58 -04:00
|
|
|
return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz};
|
2019-06-17 03:56:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // BITCOIN_UTIL_TRANSLATION_H
|