mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Fix translations using boost::locale::translate
This requires a std::locale generated from a boost::locale::generator The examples already in our code base used the wrong message_path while creating the generator and as a result our message catalogs weren't found. As with the std::locale I have added code to create a locale via boost::locale only once instead of having each c++ file redo the work. This code expects a message_path to set for the boost generator. An earlier attempt queried for this path directly from within gnc-locale-utils using gnc_get_locale_dir (from gnc-path.h). That however broke several c++ tests depending on gnc_locale_utils as those then also needed to be linked against gnc-path.o. I couldn't get the linker to do this properly so I worked around it for now by splitting the boost_locale functionality in two steps: - an initializer step that takes the messages_path as a string and will generate the locale - a getter to get the locale. The initializer should only be run once, and before the getter is called. It won't hurt though if the initializer is called more often. If the getter is called before the initializer it will still generate a std::locale but without setting a messages_path. It will then also log a warning explaining translations may not be properly found.
This commit is contained in:
@@ -594,7 +594,7 @@ static std::string migrate_gnc_datahome()
|
||||
gen.add_messages_domain(PROJECT_NAME);
|
||||
|
||||
std::stringstream migration_msg;
|
||||
migration_msg.imbue(gnc_get_locale());
|
||||
migration_msg.imbue(gnc_get_boost_locale());
|
||||
|
||||
/* Step 1: copy directory $HOME/.gnucash to $GNC_DATA_HOME */
|
||||
auto full_copy = copy_recursive (old_dir, gnc_userdata_home);
|
||||
|
||||
@@ -26,6 +26,7 @@ extern "C"
|
||||
#include <clocale>
|
||||
#include <boost/locale.hpp>
|
||||
#include "gnc-locale-utils.hpp"
|
||||
#include <config.h>
|
||||
|
||||
/** Cache the UI locale
|
||||
*
|
||||
@@ -72,3 +73,48 @@ gnc_get_locale()
|
||||
}
|
||||
|
||||
|
||||
static std::locale boost_cached;
|
||||
static bool tried_boost_already = false;
|
||||
|
||||
void
|
||||
gnc_init_boost_locale (const std::string& messages_path)
|
||||
{
|
||||
if (!tried_boost_already)
|
||||
{
|
||||
tried_boost_already = true;
|
||||
|
||||
try
|
||||
{
|
||||
boost::locale::generator gen;
|
||||
if (!messages_path.empty())
|
||||
gen.add_messages_path(messages_path);
|
||||
else
|
||||
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
|
||||
"Attempt to initialize boost_locale without a message_path. "
|
||||
"If message catalogs are not installed in the system's default locations "
|
||||
"user interface strings will not be translated.");
|
||||
gen.add_messages_domain(PROJECT_NAME);
|
||||
boost_cached = gen ("");
|
||||
}
|
||||
catch (const std::runtime_error& err)
|
||||
{
|
||||
char* locale = g_strdup(setlocale(LC_ALL, ""));
|
||||
|
||||
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
|
||||
"Failed to create C++ default locale from"
|
||||
"%s because %s. Using the 'C' locale for C++.",
|
||||
locale, err.what());
|
||||
boost_cached = std::locale::classic();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
const std::locale&
|
||||
gnc_get_boost_locale()
|
||||
{
|
||||
return boost_cached;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#define GNC_LOCALE_UTILS_HPP
|
||||
|
||||
#include <locale>
|
||||
#include <string>
|
||||
|
||||
/** Get the default application locale.
|
||||
*
|
||||
@@ -36,4 +37,30 @@
|
||||
*/
|
||||
const std::locale& gnc_get_locale();
|
||||
|
||||
|
||||
|
||||
/** Create default boost locale.
|
||||
*
|
||||
* std::locale has very limited used on Windows so for translation work we rely
|
||||
* on boost::locale instead. Calling boost::locale("") is expensive,
|
||||
* so call gnc_get_boost_locale instead.
|
||||
*
|
||||
* However before that funcation can be called the locale should be initialized
|
||||
* with gnc_init_boost_locale.
|
||||
*/
|
||||
void gnc_init_boost_locale(const std::string& messages_path);
|
||||
|
||||
|
||||
|
||||
/** Get the default boost locale.
|
||||
*
|
||||
* std::locale has very limited used on Windows so for translation work we rely
|
||||
* on boost::locale instead. Calling boost::locale("") is expensive,
|
||||
* so call this instead.
|
||||
*
|
||||
* @returns A static std::locale representing the one set with
|
||||
* setlocale() in main(), but generated from boost::locale.
|
||||
*/
|
||||
const std::locale& gnc_get_boost_locale();
|
||||
|
||||
#endif /* GNC_LOCALE_UTILS_HPP */
|
||||
|
||||
Reference in New Issue
Block a user