Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
(maint) Merge up f83da47 to master
Browse files Browse the repository at this point in the history
Generated by CI

* commit 'f83da4733a10217534328ac4492645cc1fed8cd0':
  (PA-3254) Failing locales causes 'facter -p' to crash
  • Loading branch information
puppetlabs-jenkins committed Jun 24, 2020
2 parents 551023d + f83da47 commit 39b8029
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions locale/inc/leatherman/locale/locale.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#include <vector>
#include <functional>

#include <boost/format.hpp>
#include <boost/regex.hpp>

#ifdef LEATHERMAN_I18N
#include <boost/locale/format.hpp>
#else
#include <boost/format.hpp>
#include <boost/regex.hpp>
// Unset PROJECT_NAME so we only create a single locale.
#undef PROJECT_NAME
#define PROJECT_NAME ""
Expand Down Expand Up @@ -104,6 +105,23 @@ namespace leatherman { namespace locale {
* Anonymous namespace, limiting access to current namespace
*/

/**
* Translates and formats text using boost::regex and boost::format.
* @param trans The translation function.
* @param domain Domain name.
* @param args Format arguments.
* @return The string generated by translating the format string, then applying the arguments.
*/
template <typename... TArgs>
std::string format_disabled_locales(std::function<std::string(const std::string&)>&& trans, std::string domain, TArgs... args) {
// When locales are disabled, use boost::format, which expects %N% style formatting
static const boost::regex match{"\\{(\\d+)\\}"};
static const std::string repl{"%\\1%"};
boost::format form{boost::regex_replace(trans(domain), match, repl)};
(void) std::initializer_list<int>{ ((void)(form % args), 0)... };
return form.str();
}

/**
* Translates and formats text using the locale initialized by this library.
* @param trans The translation function.
Expand All @@ -121,14 +139,13 @@ namespace leatherman { namespace locale {
#ifdef LEATHERMAN_I18N
boost::locale::format form{trans(domain)};
(void) std::initializer_list<int>{ ((void)(form % args), 0)... };
return form.str(get_locale("", domain));
try {
return form.str(get_locale("", domain));
} catch (const std::exception &e) {
return format_disabled_locales(std::move(trans), domain, std::forward<TArgs>(args)...);
}
#else
// When locales are disabled, use boost::format, which expects %N% style formatting
static const boost::regex match{"\\{(\\d+)\\}"};
static const std::string repl{"%\\1%"};
boost::format form{boost::regex_replace(trans(domain), match, repl)};
(void) std::initializer_list<int>{ ((void)(form % args), 0)... };
return form.str();
return format_disabled_locales(std::move(trans), domain, std::forward<TArgs>(args)...);
#endif
}
}
Expand Down

0 comments on commit 39b8029

Please sign in to comment.