cpp-i18n is a C++ library for internationalization (i18n) of C++ applications, which was made to be as simple as possible to use, with easy integration into existing projects.
cpp-i18n can be installed using CMake's FetchContent
module.
include(FetchContent)
FetchContent_Declare(
cpp-i18n
GIT_REPOSITORY https://github.com/Sinan-Karakaya/cpp-i18n.git
GIT_TAG v0.2.2
)
FetchContent_MakeAvailable(cpp-i18n)
target_link_libraries(
${PROJECT_NAME}
PRIVATE
cpp-i18n
)
target_include_directories(
${PROJECT_NAME}
PRIVATE
${cpp-i18n_SOURCE_DIR}/include
)
assets/locales/en/basic.json
{
"hello": "Hello, world!"
}
#include <cpp-i18n/Translator.hpp>
int main()
{
i18n::Translator t;
std::cout << t.translate("hello", "basic") << std::endl; // "Hello, world!"
// or
std::cout << t("hello", "basic") << std::endl; // "Hello, world!"
return 0;
}
assets/locales/en/basic.json
{
"hello": "Hello, world!"
}
assets/locales/fr/basic.json
{
"hello": "Bonjour, monde!"
}
#include <cpp-i18n/Translator.hpp>
int main()
{
i18n::LocaleConfig config;
config.supportedLocales = {"en", "fr"};
i18n::Translator t(config);
std::cout << t("hello", "basic") << std::endl; // "Hello, world!"
t.setLocale("fr");
std::cout << t("hello", "basic") << std::endl; // "Bonjour, monde!"
return 0;
}
assets/locales/en/basic.json
{
"hello": "Hello, world!"
}
assets/locales/en/other.json
{
"goodbye": "Goodbye, world!"
}
#include <cpp-i18n/Translator.hpp>
int main()
{
i18n::Translator t;
std::cout << t("hello", "basic") << std::endl; // "Hello, world!"
std::cout << t("goodbye", "other") << std::endl; // "Goodbye, world!"
return 0;
}
path/to/locales/en/basic.json
{
"hello": "Hello, world!"
}
#include <cpp-i18n/Translator.hpp>
int main()
{
i18n::LocaleConfig config;
config.localesDir = "path/to/locales";
i18n::Translator t(config);
std::cout << t("hello", "basic") << std::endl; // "Hello, world!"
return 0;
}
assets/locales/en/parameters.json
{
"hello": "Hello, my name is {{ name }}!"
}
#include <cpp-i18n/Translator.hpp>
int main()
{
i18n::Translator t;
std::cout << t("hello", "parameters", {{ "name", "John" }}) << std::endl; // "Hello, my name is John!"
return 0;
}
The format {{ paramName }}
is space sensitive, so {{paramName}}
will not work.