i18n
Loading...
Searching...
No Matches
🌍 P-E-R-R-Y i18n

A modern internationalization (i18n) library.

Build Docs License


✨ Overview

P-E-R-R-Y i18n is a lightweight, header-only C++11 C++20 library for internationalization.
It provides a clean, type-safe way to define and switch between language locales at runtime.

Core goals:

  • 🧩 Strongly typed locales using concepts and type traits
  • ⚙️ System locale detection (macOS & Linux)
  • 🔄 Runtime locale switching
  • 🚀 Zero dependencies, header-only, C++20 concepts
  • Unit-tested with GoogleTest

🧱 Features

  • Detects the system locale (fr, en, es, …)
  • Fallback chain (system → en → first registered)
  • Singleton pattern for shared locale management
  • Compile-time locale registration with setSupportedLocales
  • Works with tuples or parameter packs

🧩 Example

#include "I18n.hpp"
#include "ILocale.hpp"
#include <tuple>
// Define your base locale interface
class DefaultLocale : public ILocale {
public:
virtual const std::string getSignUpTitle() const = 0;
virtual const std::string getSignInTitle() const = 0;
};
// Define concrete implementations
class LocaleEn : public DefaultLocale {
public:
const std::string languageCode() const override { return "en"; }
const std::string getSignUpTitle() const override { return "Sign Up"; }
const std::string getSignInTitle() const override { return "Sign In"; }
};
class LocaleFr : public DefaultLocale {
public:
const std::string languageCode() const override { return "fr"; }
const std::string getSignUpTitle() const override { return "Inscription"; }
const std::string getSignInTitle() const override { return "Connexion"; }
};
using SupportedLocales = std::tuple<LocaleEn, LocaleFr>
// Usage
int main() {
i18n.setSupportedLocales<SupportedLocales>();
//cpp `i18n.setSupportedLocales<LocaleEn, LocaleFr>();`
//set Locale over the default one -> 1st: system, 2nd: "en", 3rd: 1st_one
i18n.setLocale("en");
std::cout << i18n.getLocale()->getSignInTitle() << std::endl;
}
std::tuple< LocaleEn, LocaleEs, LocaleFr, LocaleIt > SupportedLocales
Definition SupportedLocales.hpp:23
int main()
Definition TestCXX11.cpp:92
static I18n< T > & getInstance()
Get the I18n singleton instance.
Definition I18n.hpp:46
Base interface for all locale implementations.
Definition ILocale.hpp:34
virtual const std::string languageCode() const =0
Retrieve the two-letter language code of the locale.