diff --git a/lang/update_pot.sh b/lang/update_pot.sh index 2d573492413..a4409623064 100755 --- a/lang/update_pot.sh +++ b/lang/update_pot.sh @@ -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" \ diff --git a/pch/main-pch.hpp b/pch/main-pch.hpp index 9b93f982adb..e100c966f7c 100644 --- a/pch/main-pch.hpp +++ b/pch/main-pch.hpp @@ -83,6 +83,8 @@ #include #include #include +#include +#include #if defined(_MSC_VER) # include "platform_win.h" diff --git a/src/cata_path.h b/src/cata_path.h index 12e6579ecd4..4894769da31 100644 --- a/src/cata_path.h +++ b/src/cata_path.h @@ -7,6 +7,8 @@ #include +#include + /** * 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 : formatter { + auto format( const cata_path &path, fmt::format_context &ctx ) const { + return formatter::format( path.generic_u8string(), ctx ); + } +}; + #endif // CATA_SRC_CATA_PATH_H diff --git a/src/mapbuffer.cpp b/src/mapbuffer.cpp index 5b120f925f3..cc430e94b70 100644 --- a/src/mapbuffer.cpp +++ b/src/mapbuffer.cpp @@ -383,8 +383,7 @@ submap *mapbuffer::unserialize_submaps( const tripoint_abs_sm &p ) std::optional 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 ) ) { diff --git a/src/string_formatter.cpp b/src/string_formatter.cpp index 2fe9f298391..d4114202301 100644 --- a/src/string_formatter.cpp +++ b/src/string_formatter.cpp @@ -2,12 +2,23 @@ #include +#include #include #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::convert( const cata_path &p ) { buffer = p.generic_u8string(); diff --git a/src/string_formatter.h b/src/string_formatter.h index 345388847ae..b4f4730e593 100644 --- a/src/string_formatter.h +++ b/src/string_formatter.h @@ -8,24 +8,11 @@ #include "printf_converter.h" +// #include #include -/* -template>> - inline auto format_as( T e ) -{ - return static_cast>( e ); -} +extern std::string string_vfmt( fmt::string_view format, fmt::format_args args ); -namespace cata -{ -template>> - inline auto format_as( T e ) -{ - return static_cast>( e ); -} -} // namespace cata -*/ extern std::string string_vprintf( fmt::string_view format, fmt::printf_args args ); template diff --git a/src/translation.h b/src/translation.h index fff30982642..8b4fb268fa1 100644 --- a/src/translation.h +++ b/src/translation.h @@ -5,8 +5,6 @@ #include #include -#include - #include "translation_cache.h" #include "value_ptr.h" diff --git a/src/translations.h b/src/translations.h index f9dd30e7c61..e2b5f5996de 100644 --- a/src/translations.h +++ b/src/translations.h @@ -6,6 +6,10 @@ // so preemptively include it before the gettext overrides. #include // IWYU pragma: keep +// pragma export needed because IWYU gets confused about FMT_STRING +#include // 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 instantiates T<...> +// We use this trick to instantiate the fmtlib typechecking type in a macro context. +template +struct type_pack { + template typename T> using apply = T; +}; + +template +constexpr static auto make_type_pack( Ts &&... ) +{ + return type_pack {}; +} + +template +// 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_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 diff --git a/tools/spell_checker/dictionary.txt b/tools/spell_checker/dictionary.txt index dd9969d9499..2411a330737 100644 --- a/tools/spell_checker/dictionary.txt +++ b/tools/spell_checker/dictionary.txt @@ -7205,4 +7205,5 @@ zweihänders zweitimber zweitimbers zygomaticus +zzip zzz