Support fmtlib formatted translated strings with _fmt()
This commit is contained in:
@@ -18,6 +18,7 @@ xgettext --default-domain="cataclysm-dda" \
|
||||
--sort-by-file \
|
||||
--output="lang/po/base.pot" \
|
||||
--keyword="_" \
|
||||
--keyword="_fmt" \
|
||||
--keyword="pgettext:1c,2" \
|
||||
--keyword="n_gettext:1,2" \
|
||||
--keyword="npgettext:1c,2,3" \
|
||||
|
||||
@@ -83,6 +83,8 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
#include <fmt/format.h>
|
||||
#include <fmt/printf.h>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
# include "platform_win.h"
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
/**
|
||||
* One of the problems of filesystem paths is they lack contextual awareness
|
||||
* of 'where' they are. They're just a list of segments pointing into the
|
||||
@@ -162,4 +164,11 @@ class cata_path
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct fmt::formatter<cata_path> : formatter<std::string_view> {
|
||||
auto format( const cata_path &path, fmt::format_context &ctx ) const {
|
||||
return formatter<std::string_view>::format( path.generic_u8string(), ctx );
|
||||
}
|
||||
};
|
||||
|
||||
#endif // CATA_SRC_CATA_PATH_H
|
||||
|
||||
+1
-2
@@ -383,8 +383,7 @@ submap *mapbuffer::unserialize_submaps( const tripoint_abs_sm &p )
|
||||
std::optional<zzip> z = zzip::load( zzip_name.get_unrelative_path(),
|
||||
( PATH_INFO::world_base_save_path() / "maps.dict" ).get_unrelative_path() );
|
||||
if( !z ) {
|
||||
debugmsg( "Failed to load submaps from %s, could not open zzip.",
|
||||
zzip_name.generic_u8string().c_str() );
|
||||
debugmsg( _fmt( "Failed to load submaps from {0}, could not open zzip.", zzip_name ) );
|
||||
return false;
|
||||
}
|
||||
if( !z->has_file( file_name_path ) ) {
|
||||
|
||||
@@ -2,12 +2,23 @@
|
||||
|
||||
#include <exception>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <fmt/printf.h>
|
||||
|
||||
#include "cata_path.h"
|
||||
#include "debug.h"
|
||||
#include "translation.h"
|
||||
|
||||
std::string string_vfmt( fmt::string_view format, fmt::format_args args )
|
||||
{
|
||||
try {
|
||||
return fmt::vformat( format, args );
|
||||
} catch( const std::exception &err ) {
|
||||
debugmsg( std::string{ err.what() } + ": " + std::string{ format.data(), format.size() } );
|
||||
return err.what();
|
||||
}
|
||||
}
|
||||
|
||||
const char *printf_converter<cata_path>::convert( const cata_path &p )
|
||||
{
|
||||
buffer = p.generic_u8string();
|
||||
|
||||
+2
-15
@@ -8,24 +8,11 @@
|
||||
|
||||
#include "printf_converter.h"
|
||||
|
||||
// #include <fmt/format.h>
|
||||
#include <fmt/printf.h>
|
||||
|
||||
/*
|
||||
template<typename T, typename = std::enable_if_t<std::is_enum_v<T>>>
|
||||
inline auto format_as( T e )
|
||||
{
|
||||
return static_cast<std::underlying_type_t<T>>( e );
|
||||
}
|
||||
extern std::string string_vfmt( fmt::string_view format, fmt::format_args args );
|
||||
|
||||
namespace cata
|
||||
{
|
||||
template<typename T, typename = std::enable_if_t<std::is_enum_v<T>>>
|
||||
inline auto format_as( T e )
|
||||
{
|
||||
return static_cast<std::underlying_type_t<T>>( e );
|
||||
}
|
||||
} // namespace cata
|
||||
*/
|
||||
extern std::string string_vprintf( fmt::string_view format, fmt::printf_args args );
|
||||
|
||||
template<typename ...Args>
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include <fmt/printf.h>
|
||||
|
||||
#include "translation_cache.h"
|
||||
#include "value_ptr.h"
|
||||
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
// so preemptively include it before the gettext overrides.
|
||||
#include <locale> // IWYU pragma: keep
|
||||
|
||||
// pragma export needed because IWYU gets confused about FMT_STRING
|
||||
#include <fmt/format.h> // IWYU pragma: export
|
||||
|
||||
#include "string_formatter.h"
|
||||
#include "translation.h"
|
||||
|
||||
#if !defined(translate_marker)
|
||||
@@ -95,6 +99,30 @@ inline const char *npgettext( const char *const context, const char *const msgid
|
||||
|
||||
#endif // LOCALIZE
|
||||
|
||||
// Always statically typecheck format strings even if not using localization.
|
||||
// type_pack<...>::apply<T> instantiates T<...>
|
||||
// We use this trick to instantiate the fmtlib typechecking type in a macro context.
|
||||
template<typename ...Ts>
|
||||
struct type_pack {
|
||||
template<template<typename ...> typename T> using apply = T<Ts...>;
|
||||
};
|
||||
|
||||
template<typename ...Ts>
|
||||
constexpr static auto make_type_pack( Ts &&... )
|
||||
{
|
||||
return type_pack<Ts...> {};
|
||||
}
|
||||
|
||||
template<typename ...Args>
|
||||
// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
|
||||
inline std::string _fmt_inner( std::string_view translation, Args &&...args )
|
||||
{
|
||||
return string_vfmt( translation, fmt::make_format_args( args... ) );
|
||||
}
|
||||
|
||||
#define _fmt( msg, ... ) \
|
||||
(decltype(make_type_pack(__VA_ARGS__))::apply<fmt::format_string>{ FMT_STRING(msg) }, _fmt_inner( _(msg), __VA_ARGS__ ))
|
||||
|
||||
// Avoid using these functions from libintl.h which won't work because we have
|
||||
// our own implementations with different names.
|
||||
#pragma GCC diagnostic push
|
||||
|
||||
@@ -7205,4 +7205,5 @@ zweihänders
|
||||
zweitimber
|
||||
zweitimbers
|
||||
zygomaticus
|
||||
zzip
|
||||
zzz
|
||||
|
||||
Reference in New Issue
Block a user