i18ncpp provides a powerful and flexible solution for adding internationalization (i18n) capabilities to your C++ projects. It offers a wide range of features for translation, pluralization, number formatting, currency formatting, and date/time formatting.
This library is a C++ implementation of the smiti18n Lua library, providing similar functionality with a C++ interface.
- Translation Management: Load and manage translations from JSON files
- Variable Interpolation: Insert dynamic values into translations
- Pluralization: Handle grammatically correct plural forms for different languages
- Locale Fallbacks: Automatically use fallback translations when a specific locale is not available
- Nested Keys: Support for structured translation hierarchies
- Variants: Choose translations based on contextual parameters
- Number Formatting: Format numbers with locale-specific separators
- Currency Formatting: Format monetary values with appropriate currency symbols
- Date/Time Formatting: Format dates and times according to locale preferences
# Clone the repository
git clone https://github.com/yourusername/i18ncpp.git
# Create a build directory
mkdir build && cd build
# Configure and build
cmake ..
cmake --build .
# Install (optional)
cmake --install .
- C++17 or higher
- nlohmann/json for JSON parsing
#include <i18ncpp.h>
#include <iostream>
int main() {
i18n::I18N i18n;
// Load translations from a JSON file
i18n.loadLocaleFromFile("en.json");
i18n.loadLocaleFromFile("fr.json");
// Set active locale
i18n.setLocale("en");
// Basic translation
std::cout << i18n.tr("greeting") << std::endl; // Hello!
// Translation with parameter
std::cout << i18n.tr("welcome", "John") << std::endl; // Welcome, John!
// Pluralization
std::cout << i18n.trPlural("items", 1) << std::endl; // 1 item
std::cout << i18n.trPlural("items", 5) << std::endl; // 5 items
// Change locale
i18n.setLocale("fr");
std::cout << i18n.tr("greeting") << std::endl; // Bonjour!
return 0;
}
Translation files use JSON format. Here's an example:
{
"greeting": "Hello!",
"welcome": "Welcome, {}!",
"items": {
"one": "{} item",
"other": "{} items"
},
"user": {
"login": "Log in",
"logout": "Log out",
"profile": "User profile"
},
"_formats": {
"number": {
"decimal_symbol": ".",
"thousand_separator": ","
},
"currency": {
"symbol": "$",
"name": "US Dollar",
"short_name": "USD"
}
}
}
loadLocale(locale, filePath)
: Load translations for a specific locale from a fileloadLocaleFromFile(filePath)
: Load translations, extracting locale from filenamesetLocale(locale)
: Set the active localesetFallbackLocale(locale)
: Set the fallback localegetLocale()
: Get the current active localegetFallbackLocale()
: Get the current fallback locale
tr(key)
: Get a translation by keytr(key, param1)
: Get a translation with a single parametertr(key, param1, param2, ...)
: Get a translation with multiple parameterstr(key, paramVector)
: Get a translation with a vector of parameterstrv(key, ...args)
: Get a translation with variadic parameterstrPlural(key, count)
: Get a pluralized translationtrPlural(key, count, param1, ...)
: Get a pluralized translation with parameterstrPluralv(key, count, ...args)
: Get a pluralized translation with variadic parameters
formatNumber(number)
: Format a number according to locale settingsformatPrice(amount)
: Format a monetary amount with currency symbolformatDate(pattern, date)
: Format a date/time valueconfigure(formats)
: Configure formatting options
The library supports pluralization rules for different languages, including:
- English-like languages (en, de, es, etc.)
- Slavic languages (ru, uk, pl, etc.)
- Arabic
- French
- Czech/Slovak
- And more
Several interpolation formats are supported:
{}
or{0}
,{1}
, etc. for indexed parameters%{key}
for named parameters%<key>.fmt
for formatted parameters wherefmt
is a format specifier:%<num>.d
- integer format%<num>.f
- floating-point format%<num>.s
- string format
Contributions are welcome! Please feel free to submit a Pull Request.