8000 How do I support gettext to internationalize multilingualism? · Issue #3363 · gabime/spdlog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

How do I support gettext to internationalize multilingualism? #3363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fengdaolong opened this issue Mar 20, 2025 · 6 comments
Closed

How do I support gettext to internationalize multilingualism? #3363

fengdaolong opened this issue Mar 20, 2025 · 6 comments

Comments

@fengdaolong
Copy link

I want to use gettext for internationalization support, my code is as follows:

    std::string world = "World";
    SPDLOG_INFO(boost::locale::gettext("Hello {}"), world);

When I compile, the following error is reported. It seems that the first argument to the SPDLOG_INFO macro does not support strings computed at runtime. What should I do?

In file included from /tmp/develop/linux_agent/src/main.cpp:67:
/tmp/develop/linux_agent/src/main.cpp: In function ‘int main(int, const char**)’:
/tmp/develop/linux_agent/thirdparty/spdlog/include/spdlog/spdlog.h:288:22: error: call to non-‘constexpr’ function ‘std::__cxx11::basic_string<_CharT> boost::locale::gettext(const CharType*, const std::locale&) [with CharType = char]’
  288 |         (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
      |         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/develop/linux_agent/thirdparty/spdlog/include/spdlog/spdlog.h:314:9: note: in expansion of macro ‘SPDLOG_LOGGER_CALL’
  314 |         SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
      |         ^~~~~~~~~~~~~~~~~~
/tmp/develop/linux_agent/thirdparty/spdlog/include/spdlog/spdlog.h:315:30: note: in expansion of macro ‘SPDLOG_LOGGER_INFO’
  315 |     #define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
      |                              ^~~~~~~~~~~~~~~~~~
/tmp/develop/linux_agent/src/main.cpp:357:5: note: in expansion of macro ‘SPDLOG_INFO’
  357 |     SPDLOG_INFO(boost::locale::gettext("Hello {}"), world);
      |     ^~~~~~~~~~~
In file included from /usr/include/boost/locale/format.hpp:13,
                 from /usr/include/boost/locale.hpp:16,
                 from /tmp/develop/linux_agent/src/common/localization.hpp:4,
                 from /tmp/develop/linux_agent/src/main.cpp:65:
/usr/include/boost/locale/message.hpp:432:33: note: ‘std::__cxx11::basic_string<_CharT> boost::locale::gettext(const CharType*, const std::locale&) [with CharType = char]’ declared here
  432 |     std::basic_string<CharType> gettext(const CharType* id, const std::locale& loc = std::locale())
      |                                 ^~~~~~~
@gabime
Copy link
Owner
gabime commented Mar 20, 2025

Try wrapping with SPDLOG_FMT_RUNTIME

@fengdaolong
Copy link
Author

Use SPDLOG_FMT_RUNTIME wrapping can be used, thanks for guidance!

@fengdaolong
Copy link
Author

Now I have a new problem:

I defined the following macro to simplify gettext string extraction. However, the _(str) macro can only wrap the Spdlog string, and a compilation error occurs when this macro wraps other strings in the project. What am I supposed to do?

#define _(str) SPDLOG_FMT_RUNTIME(boost::locale::gettext(str).c_str())

std::string world = "World";
SPDLOG_INFO(_("Hello {}"), world);

@fengdaolong fengdaolong reopened this Mar 21, 2025
@tt4g
Copy link
Contributor
tt4g commented Mar 21, 2025

I don't see why it should be wrapped using the SPDLOG_FMT_RUNTIME macro when it is not a string to be passed to spdlog.
Translation and logging have different purposes. Why try to accomplish different purposes with one macro?

@fengdaolong
Copy link
Author

The reason for wrapping the string with the same macro is purely for the convenience of unification.
However, I don't know if there is any compatible way for the spdlog string.

@tt4g
Copy link
Contributor
tt4g commented Mar 24, 2025

SPDLOG_FMT_RUNTIME does not return a string.
It returns a structure to identify that the format string is not determined until runtime.

@gabime gabime closed this as completed May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0