Overview / Text & language

i18n

Makes a product speak more than one language: CLDR 48.2 plurals, locale-aware formatting, ICU MessageFormat messages and compiled catalogs generated by the <code>etl-i18n</code> tool.

Always compiled: part of the core build.Include #include <ethereal/cpp/i18n/i18n_inc.hpp>C layer

i18n_inc.hpp

#include <ethereal/cpp/i18n/i18n_inc.hpp>

Aggregate entry point of the I18n module.

Plurals, locale formatting (numbers, currencies, dates, lists, relative time, file sizes, durations), messages (MessageFormat) and compiled catalogs.

i18n_format.hpp

#include <ethereal/cpp/i18n/i18n_format.hpp>

Formatting for people, by locale, for the I18n module.

All functions follow the same contract. The caller gives the buffer (Out), receives in *pWritten the size of the whole result, and gets eBufferTooSmall when it did not fit (what fits is written). None of them allocates. The formatter is written by hand, with no snprintf and no iostreams, and the rounding is the CLDR one (half to even).

A double is formatted from its shortest form (the one that reads back as the same double), as in ICU: 0.285 is "0,285", not 0,28499999999999998. For exact values (money) there are versions that take the number as text ("1234.50").

struct

SNumberOptions

Ethereal::I18n#
C++
struct   SNumberOptions
{
    /// @brief  Minimum fraction digits. -1 uses the locale pattern (or the currency).
    std::int8_t        iMinFraction = -1;

    /// @brief  Maximum fraction digits. -1 uses the locale pattern (or the currency).
    std::int8_t        iMaxFraction = -1;

    /// @brief  Thousands separators (and the Indian lakh and crore).
    bool            bGrouping = true;
};

Number formatting options.

function

FormatNumber

Ethereal::I18n#
EI18nStatus FormatNumber( const CLocale & L, double dValue, std::span< char > Out, std::size_t * pWritten, const SNumberOptions & O = {} ) noexcept;

Formats a double by the locale.

L
The locale.
dValue
The number. NaN and infinity use the locale symbols.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
O
Options.
eOk, eBufferTooSmall, eInvalidArgument or eBadPattern.
function

FormatNumber

Ethereal::I18n#
EI18nStatus FormatNumber( const CLocale & L, std::int64_t iValue, std::span< char > Out, std::size_t * pWritten, const SNumberOptions & O = {} ) noexcept;

Formats an integer by the locale.

L
The locale.
iValue
The number.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
O
Options.
eOk, eBufferTooSmall or eBadPattern.
function

FormatDecimal

Ethereal::I18n#
EI18nStatus FormatDecimal( const CLocale & L, std::string_view sDecimal, std::span< char > Out, std::size_t * pWritten, const SNumberOptions & O = {} ) noexcept;

Formats a number written as text, exactly.

L
The locale.
sDecimal
The number ("-1234.5678", no exponent).
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
O
Options.
eOk, eBufferTooSmall, eInvalidArgument or eBadPattern.
function

FormatPercent

Ethereal::I18n#
EI18nStatus FormatPercent( const CLocale & L, double dFraction, std::span< char > Out, std::size_t * pWritten, const SNumberOptions & O = {} ) noexcept;

Formats a fraction as a percentage: 0.25 is "25%" ("25 %" in French).

L
The locale.
dFraction
The fraction.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
O
Options.
eOk, eBufferTooSmall, eInvalidArgument or eBadPattern.
enum

ECurrencyDisplay

Ethereal::I18n#
C++
enum class   ECurrencyDisplay : std::uint8_t
{
    /// @brief  "US$", "R$", "€": the symbol of the locale, or the code when the
    ///             locale has none.
    eSymbol,

    /// @brief  "$".
    eNarrowSymbol,

    /// @brief  "USD".
    eCode,
};

How the currency is shown.

function

CurrencyDigits

Ethereal::I18n#
unsigned CurrencyDigits( std::string_view sIsoCode ) noexcept;

Returns the fraction digits of a currency (ISO 4217): JPY 0, KWD 3, USD 2.

sIsoCode
Three letter code.
The number of digits. Unknown codes give 2.
function

FormatCurrency

Ethereal::I18n#
EI18nStatus FormatCurrency( const CLocale & L, std::string_view sAmount, std::string_view sIsoCode, std::span< char > Out, std::size_t * pWritten, ECurrencyDisplay eDisplay = ECurrencyDisplay::eSymbol, const SNumberOptions & O = {} ) noexcept;

Formats an amount written as text in a currency, with the fraction digits of that currency unless O changes them.

L
The locale.
sAmount
The amount ("1234.50").
sIsoCode
Three letter code, upper case.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
eDisplay
How the currency is shown.
O
Options.
eOk, eBufferTooSmall, eInvalidArgument or eBadPattern.
function

FormatCurrency

Ethereal::I18n#
EI18nStatus FormatCurrency( const CLocale & L, double dAmount, std::string_view sIsoCode, std::span< char > Out, std::size_t * pWritten, ECurrencyDisplay eDisplay = ECurrencyDisplay::eSymbol, const SNumberOptions & O = {} ) noexcept;

Formats a double amount in a currency, with the fraction digits of that currency unless O changes them.

L
The locale.
dAmount
The amount.
sIsoCode
Three letter code, upper case.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
eDisplay
How the currency is shown.
O
Options.
eOk, eBufferTooSmall, eInvalidArgument or eBadPattern.
function

FormatFileSize

Ethereal::I18n#
EI18nStatus FormatFileSize( const CLocale & L, std::uint64_t uBytes, std::span< char > Out, std::size_t * pWritten ) noexcept;

Formats a file size in decimal units (kB = 1000 B): "1,5 MB".

One fraction digit below 10 units, none from there on. The unit takes the plural of the written number.

L
The locale.
uBytes
The size in bytes.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
eOk, eBufferTooSmall or eBadPattern.
enum

EUnitWidth

Ethereal::I18n#
C++
enum class   EUnitWidth : std::uint8_t { eShort, eNarrow };

Width of the unit names.

function

FormatDuration

Ethereal::I18n#
EI18nStatus FormatDuration( const CLocale & L, std::int64_t iSeconds, std::span< char > Out, std::size_t * pWritten, EUnitWidth eWidth = EUnitWidth::eShort ) noexcept;

Formats a duration in days, hours, minutes and seconds.

The parts that are zero are left out, and the rest is joined by the unit list of the locale: "1 h, 5 min", "1h 5m". A negative duration uses the absolute value.

L
The locale.
iSeconds
The duration in seconds.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
eWidth
Width of the unit names.
eOk, eBufferTooSmall or eBadPattern.
enum

EDateStyle

Ethereal::I18n#
C++
enum class   EDateStyle : std::uint8_t { eNone, eShort, eMedium, eLong, eFull };

Length of a date or time pattern of the locale.

struct

SDateOptions

Ethereal::I18n#
C++
struct   SDateOptions
{
    /// @brief  Style of the date part. eNone leaves it out.
    EDateStyle        eDate = EDateStyle::eMedium;

    /// @brief  Style of the time part. eNone leaves it out.
    EDateStyle        eTime = EDateStyle::eNone;

    /// @brief  Time zone of the date, in minutes east of UTC. Only shown, in
    ///             the patterns that show it ("full" and "long" times). The date
    ///             is already in local time.
    std::int32_t    iUtcOffsetMinutes = 0;

    /// @brief  The zone is GMT/UTC: it is shown by its CLDR name ("Greenwich
    ///             Mean Time"). Other zones show as "GMT+10:30", because their
    ///             names need the time zone database, which the Time module
    ///             leaves out.
    bool            bZoneIsGmt = false;

    /// @brief  true uses the "at" glue ("Monday, July 1, 2024 at 8:50 AM").
    ///             false uses the normal one (with a comma).
    bool            bAtTime = true;
};

Date formatting options.

function

FormatDateTime

Ethereal::I18n#
EI18nStatus FormatDateTime( const CLocale & L, const Time::SCivilTime & T, std::span< char > Out, std::size_t * pWritten, const SDateOptions & O = {} ) noexcept;

Formats a date and/or time with the short, medium, long or full patterns of the locale.

L
The locale.
T
The date, in local time.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
O
Options.
eOk, eBufferTooSmall, eInvalidArgument (both styles eNone, or a bad month) or eBadPattern.
function

FormatDatePattern

Ethereal::I18n#
EI18nStatus FormatDatePattern( const CLocale & L, const Time::SCivilTime & T, std::string_view sPattern, std::span< char > Out, std::size_t * pWritten, const SDateOptions & O = {} ) noexcept;

Formats a date with a CLDR pattern ("EEEE, d 'de' MMMM") and the names of the locale.

L
The locale.
T
The date, in local time.
sPattern
The CLDR pattern.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
O
Options (the zone fields).
eOk, eBufferTooSmall, eInvalidArgument, or eBadPattern when the pattern has a letter that is not supported (see the README).
enum

ERelativeUnit

Ethereal::I18n#
C++
enum class   ERelativeUnit : std::uint8_t { eYear, eMonth, eWeek, eDay, eHour, eMinute, eSecond };

Unit of a relative time.

function

FormatRelativeTime

Ethereal::I18n#
EI18nStatus FormatRelativeTime( const CLocale & L, std::int64_t iValue, ERelativeUnit eUnit, std::span< char > Out, std::size_t * pWritten, bool bNumeric = true ) noexcept;

Formats a relative time: "daqui a 3 dias", "3 days ago".

L
The locale.
iValue
The amount. Negative is in the past.
eUnit
The unit.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
bNumeric
false uses the words the locale has for -1, 0 and 1 ("ontem", "hoje", "amanha", "last year").
eOk, eBufferTooSmall, eInvalidArgument or eBadPattern.
enum

EListStyle

Ethereal::I18n#
C++
enum class   EListStyle : std::uint8_t
{
    /// @brief  "A, B e C".
    eAnd,

    /// @brief  "A, B ou C".
    eOr,

    /// @brief  "3 feet, 7 inches".
    eUnit,

    /// @brief  Short unit list.
    eUnitShort,

    /// @brief  Narrow unit list.
    eUnitNarrow,
};

Kind of list.

function

FormatList

Ethereal::I18n#
EI18nStatus FormatList( const CLocale & L, std::span< const std::string_view > Items, std::span< char > Out, std::size_t * pWritten, EListStyle eStyle = EListStyle::eAnd ) noexcept;

Joins items into a list by the locale.

L
The locale.
Items
The items, in order.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
eStyle
Kind of list.
eOk, eBufferTooSmall, eInvalidArgument or eBadPattern.

i18n_locale.hpp

#include <ethereal/cpp/i18n/i18n_locale.hpp>

Locales with formatting data for the I18n module.

A chosen subset. The whole CLDR is tens of megabytes. The library ships the formatting data of 21 locales (the list is in ShippedLocales and in the README), generated from the CLDR (the version is in the README): numbers, currencies, dates, lists, relative time, file sizes and durations. The plural rules cover every locale in the CLDR (see CPluralRules).

Fallback: the caller asks for a BCP 47 tag ("pt-BR", "en-AU", "zh-TW") and gets the shipped locale the CLDR would give it. That means the explicit parents (en-AU -> en-001, es-MX -> es-419), the likely script (zh-TW -> zh-Hant) and dropping subtags (pt-BR -> pt). With no match the result is English and Find returns false.

Other locales on demand: 'tools/i18n_gen/gen_i18n.py --etlloc de_CH de-CH.etlloc' writes the data of any CLDR locale to a file, and CLocaleData opens it at run time (mapped, like the catalogs), with no rebuild of the library.

enum

EI18nStatus

Ethereal::I18n#
C++
enum class   EI18nStatus : std::uint8_t
{
    /// @brief  Success.
    eOk = 0,

    /// @brief  The output did not fit. '*pWritten' holds the full size.
    eBufferTooSmall,

    /// @brief  A null pointer or a value out of range.
    eInvalidArgument,

    /// @brief  The file does not exist.
    eNotFound,

    /// @brief  A pattern (number, date or message) that does not parse.
    eBadPattern,

    /// @brief  A '.etlcat' or '.etlloc' that is corrupt, of another version,
    ///             or from another source.
    eBadCatalog,

    /// @brief  A read error.
    eIoError,

    /// @brief  Refused: no license was accepted and the evaluation of this machine has
    ///             ended, or the license passed was refused (see Bundle::UnlockBundle).
    eNotLicensed,
};

Result of the I18n functions.

function

StatusName

Ethereal::I18n#
const char * StatusName( EI18nStatus e ) noexcept;

Returns a short text for a status.

e
The status.
A static string.
class

CLocale

Ethereal::I18n#

A locale with formatting data.

class CLocale

Copied as three pointers and the plural rules. Default constructed it is English ("en"). A locale taken from a CLocaleData is valid while that data stays open.

constructor

CLocale

#
CLocale() noexcept;

Default constructor. The locale is English ("en").

function

Find

#
static bool Find( std::string_view sTag, CLocale * pOut ) noexcept;

Finds the shipped locale for a tag (see the fallback in the file header).

sTag
BCP 47 tag.
pOut
Receives the locale. English when nothing matches.
false when no shipped locale matches.
function

Tag

#
std::string_view Tag() const noexcept;

Tag of the shipped locale ("pt", "en-001", "zh-Hant").

function

Plural

#
const CPluralRules & Plural() const noexcept

Plural rules of the language.

function

FirstDayOfWeek

#
unsigned FirstDayOfWeek() const noexcept;

First day of the week (0 = Sunday), from the likely region of the locale.

function

ShippedLocales

Ethereal::I18n#
std::size_t ShippedLocales( std::span< const char * > Out ) noexcept;

Lists the tags of the shipped locales, in order.

Out
Receives as many tags as fit.
Number of shipped locales, which may be more than Out holds.
class

CLocaleData

Ethereal::I18n#

Formatting data of one locale in a '.etlloc' file (written by 'gen_i18n.py --etlloc'), mapped.

class CLocaleData

Moving transfers the mapping. Destroying (or Close) unmaps it, and the CLocale objects taken from it become invalid.

constructor

CLocaleData

#
CLocaleData() noexcept = default;

Default constructor. Nothing is open.

constructor

CLocaleData

#
CLocaleData( CLocaleData && o ) noexcept;

Move constructor. Takes over the mapping.

operator

operator=

#
CLocaleData & operator=( CLocaleData && o ) noexcept;

Move assignment. Closes the current data and takes over the mapping.

function

Open

#
static EI18nStatus Open( const char * pPath, CLocaleData * pOut ) noexcept;

Maps a file and checks all of it.

pPath
Path in UTF-8.
pOut
Receives the data.
eOk, eNotFound, eIoError, eInvalidArgument, or eBadCatalog when the file is corrupt or was generated for another key list (another version). Also eNotLicensed when the library is not licensed (see Bundle::UnlockBundle).
function

FromMemory

#
static EI18nStatus FromMemory( std::span< const std::uint8_t > Blob, CLocaleData * pOut ) noexcept;

Uses data in memory that the caller keeps. No copy is made.

Blob
The data, aligned to 4 bytes.
pOut
Receives the data.
eOk, eInvalidArgument (null or misaligned) or eBadCatalog. Also eNotLicensed when the library is not licensed (see Bundle::UnlockBundle).
function

Close

#
void Close() noexcept;

Unmaps the file. Safe to call when nothing is open.

function

IsOpen

#
bool IsOpen() const noexcept

true while data is open.

function

Get

#
bool Get( CLocale * pOut ) const noexcept;

Builds the locale of this data, with the plural rules of its tag.

pOut
Receives the locale.
false when nothing is open.
function

Tag

#
std::string_view Tag() const noexcept;

Tag stored in the file. Empty when nothing is open.

i18n_message.hpp

#include <ethereal/cpp/i18n/i18n_message.hpp>

Messages and compiled catalogs for the I18n module.

The compiled catalog (.etlcat) is mapped, not parsed: a header, a dense index (id -> text) and the texts, in UTF-8. Opening it maps the file and checks the bounds. Looking up an id is an index into an array. The ids come from the header the 'etl-i18n' tool generates (constexpr std::uint32_t), so a wrong name is a compile error instead of a missing message in a customer's screenshot.

The message syntax is a subset of ICU MessageFormat (the one translation platforms know):

"{count} files in {folder}" Placeholders have names, so the translator can reorder the sentence. "{n, plural, one {# file} other {# files}}" Plural (also =0, =1..., offset:n). "{n, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}" "{g, select, male {he} female {she} other {they}}" "{x, number}", "{x, number, integer|percent}", "{d, date, short|medium|long|full}", "{d, time, short|...}" '' is a quote. '{...}' between quotes is text.

Fallback: the catalog of the requested locale, then the one of the base language (pt-BR -> pt), then the source language, and last the key itself. Never an empty text. Each fallback calls the missing message hook, so the gaps show up in CI.

struct

SArg

Ethereal::I18n#
C++
struct   SArg
{
    /// @brief  Kind of value the argument holds.
    ///
    enum class   EType : std::uint8_t { eInt, eReal, eText, eDate };

    /// @brief  Name of the placeholder.
    std::string_view                sName;

    /// @brief  Kind of value.
    EType                            eType = EType::eText;

    /// @brief  Value when eType is eInt.
    std::int64_t                    iValue = 0;

    /// @brief  Value when eType is eReal.
    double                            dValue = 0.0;

    /// @brief  Value when eType is eText.
    std::string_view                sValue;

    /// @brief  Value when eType is eDate. The caller keeps the date alive.
    const Time::SCivilTime *        pDate = nullptr;

    /// @brief  Constructors, one per value type.
    SArg( std::string_view n, int v ) noexcept : sName( n ), eType( EType::eInt ), iValue( v ) {}
    SArg( std::string_view n, long v ) noexcept : sName( n ), eType( EType::eInt ), iValue( v ) {}
    SArg( std::string_view n, long long v ) noexcept : sName( n ), eType( EType::eInt ), iValue( v ) {}
    SArg( std::string_view n, unsigned v ) noexcept : sName( n ), eType( EType::eInt ), iValue( v ) {}
    SArg( std::string_view n, double v ) noexcept : sName( n ), eType( EType::eReal ), dValue( v ) {}
    SArg( std::string_view n, std::string_view v ) noexcept : sName( n ), eType( EType::eText ), sValue( v ) {}
    SArg( std::string_view n, const char * v ) noexcept : sName( n ), eType( EType::eText ), sValue( v != nullptr ? v : "" ) {}
    SArg( std::string_view n, const Time::SCivilTime & v ) noexcept : sName( n ), eType( EType::eDate ), pDate( &v ) {}
};

A message argument, by name.

function

FormatMessageText

Ethereal::I18n#
EI18nStatus FormatMessageText( const CLocale & L, std::string_view sMessage, std::span< char > Out, std::size_t * pWritten, std::span< const SArg > Args = {} ) noexcept;

Formats a message (as text) with its arguments, by the locale.

A missing argument is written as "{name}". The name is not FormatMessage because that is a macro in <windows.h>.

L
The locale.
sMessage
The message.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
Args
The arguments.
eOk, eBufferTooSmall, or eBadPattern when the message does not parse.
enum

EArgKind

Ethereal::I18n#
C++
enum class   EArgKind : std::uint8_t
{
    /// @brief  "{x}".
    eAny,

    /// @brief  "{x, number}", plural, selectordinal.
    eNumber,

    /// @brief  "{x, select, ...}".
    eSelect,

    /// @brief  "{x, date}" or "{x, time}".
    eDate,
};

Kind of a placeholder, from how the message uses it.

struct

SPlaceholder

Ethereal::I18n#
C++
struct   SPlaceholder
{
    /// @brief  Name of the placeholder.
    std::string_view    sName;

    /// @brief  Kind, from how the message uses it.
    EArgKind            eKind = EArgKind::eAny;

    /// @brief  Categories with a branch in the plurals (cardinal) of this
    ///             placeholder, one bit per EPluralCategory. With two plurals of
    ///             the same placeholder, the ones in all of them (a category
    ///             missing in one is missing).
    std::uint8_t        uCardinalForms = 0;

    /// @brief  Same as uCardinalForms, for the selectordinal branches.
    std::uint8_t        uOrdinalForms = 0;
};

A placeholder found by InspectMessage.

function

InspectMessage

Ethereal::I18n#
EI18nStatus InspectMessage( std::string_view sMessage, std::span< SPlaceholder > Out, std::size_t * pCount, std::size_t * pErrorOffset ) noexcept;

Lists the placeholders of a message, for translation validators.

No repeats, in the order they appear. Rejects bad syntax, a plural or select without 'other', the same name with two kinds, and a plural branch that is neither a CLDR category nor "=N" (formatting accepts it and never picks it, validation rejects it).

sMessage
The message.
Out
Receives as many placeholders as fit.
pCount
Receives the number found, which may be more than Out holds.
pErrorOffset
Receives the byte of the problem on eBadPattern.
eOk or eBadPattern.
function

PseudoLocalize

Ethereal::I18n#
EI18nStatus PseudoLocalize( std::string_view sMessage, std::span< char > Out, std::size_t * pWritten ) noexcept;

Pseudo-localizes a message.

Accents on the ASCII letters and the text 40% longer, between "[" and "]". Placeholders and syntax stay intact. Finds layout bugs before a translation is ordered.

sMessage
The message.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
eOk, eBufferTooSmall or eBadPattern.
class

CCatalog

Ethereal::I18n#

A mapped '.etlcat' catalog.

class CCatalog

Moving transfers the mapping. Destroying (or Close) unmaps it and the memory goes back to what it was.

constructor

CCatalog

#
CCatalog() noexcept;

Default constructor. Nothing is open.

constructor

CCatalog

#
CCatalog( CCatalog && o ) noexcept;

Move constructor. Takes over the mapping.

operator

operator=

#
CCatalog & operator=( CCatalog && o ) noexcept;

Move assignment. Closes the current catalog and takes over the mapping.

function

Open

#
static EI18nStatus Open( const char * pPath, CCatalog * pOut ) noexcept;

Maps a file and checks all of it (bounds, table).

pPath
Path in UTF-8.
pOut
Receives the catalog.
eOk, eNotFound, eIoError, eInvalidArgument or eBadCatalog. Also eNotLicensed when the library is not licensed (see Bundle::UnlockBundle).
function

FromMemory

#
static EI18nStatus FromMemory( std::span< const std::uint8_t > Blob, CCatalog * pOut ) noexcept;

Uses a catalog in memory that the caller keeps (embedded, for example). No copy is made.

Blob
The catalog.
pOut
Receives the catalog.
eOk, eInvalidArgument or eBadCatalog. Also eNotLicensed when the library is not licensed (see Bundle::UnlockBundle).
function

Close

#
void Close() noexcept;

Unmaps the file. Safe to call when nothing is open.

function

IsOpen

#
bool IsOpen() const noexcept

true while a catalog is open.

function

Locale

#
std::string_view Locale() const noexcept;

Locale tag of the catalog. Empty when nothing is open.

function

Count

#
std::uint32_t Count() const noexcept;

Number of messages.

function

SourceHash

#
std::uint64_t SourceHash() const noexcept;

Hash of the source keys. Catalogs of the same project have the same one.

function

Text

#
bool Text( const std::uint32_t uId, std::string_view * pOut ) const noexcept

Returns the text of a message.

Inline: this is the path of every Get (one multiply and two reads). Open already checked that everything fits in the blob.

uId
The message id.
pOut
Receives the text.
false when this locale did not translate it, or the id does not exist.
function

Find

#
bool Find( std::string_view sKey, std::uint32_t * pId ) const noexcept;

Finds the id of a key. The slow path: a hash table in the catalog.

sKey
The key.
pId
Receives the id. May be null.
false when the key is not in the catalog.
function

Key

#
std::string_view Key( std::uint32_t uId ) const noexcept;

Key of a message. Empty when the id does not exist.

function

PluralRuleId

#
std::uint16_t PluralRuleId() const noexcept;

Plural rules the catalog was compiled with: the cardinal categories (bits 0-7) and the ordinal ones (bits 8-15), one bit per EPluralCategory.

struct

SCatalogEntry

Ethereal::I18n#
C++
struct   SCatalogEntry
{
    /// @brief  The key.
    std::string_view    sKey;

    /// @brief  The translated text.
    std::string_view    sText;

    /// @brief  false (or an empty text): this locale did not translate it.
    bool                bPresent = true;
};

A message for BuildCatalog.

function

CatalogSourceHash

Ethereal::I18n#
std::uint64_t CatalogSourceHash( std::span< const std::string_view > Keys ) noexcept;

Hash (64 bit FNV-1a) of the keys, in order. It is the SourceHash of a catalog with these keys.

Keys
The keys, in id order.
The hash.
function

BuildCatalog

Ethereal::I18n#
EI18nStatus BuildCatalog( std::string_view sLocale, std::span< const SCatalogEntry > Entries, std::vector< std::uint8_t > * pOut );

Builds a '.etlcat'. This is what the 'etl-i18n' tool writes.

The order of Entries gives the ids. An empty text stays untranslated (as in gettext, Get never returns empty). Allocates the blob.

sLocale
Locale tag, up to 19 bytes.
Entries
The messages, in id order.
pOut
Receives the blob.
eOk, or eInvalidArgument for an empty or repeated key, a tag longer than 19 bytes, or a blob over 4 GB.
alias

void

Ethereal::I18n#
using MissingMessageHook = void ( * )( void * pUser, std::uint32_t uId, std::string_view sKey, std::string_view sLocale ) noexcept;

Missing message hook. Called on each fallback (the requested locale did not have the message). sLocale is the one of the requested catalog.

sKey and sLocale may be empty, but their data() is never null (since 2026-10-16), so copying them with memcpy is safe.

class

CMessages

Ethereal::I18n#

The messages of an application: the catalog chain, the locale of numbers and dates, and the hook.

class CMessages

Does not own the catalogs. The caller keeps them.

function

SetCatalogs

#
EI18nStatus SetCatalogs( const CCatalog * pRequested, const CCatalog * pBase, const CCatalog * pSource ) noexcept;

Sets the chain. Any of the three may be null.

pRequested
Catalog of the requested locale.
pBase
Catalog of the base language.
pSource
Catalog of the source language.
eOk, or eBadCatalog when a catalog is closed or the catalogs are not from the same project (another SourceHash or another number of messages).
function

SetLocale

#
void SetLocale( const CLocale & L ) noexcept

Sets the locale of numbers and dates.

function

Locale

#
const CLocale & Locale() const noexcept

Locale of numbers and dates.

function

SetMissingHook

#
void SetMissingHook( MissingMessageHook pHook, void * pUser ) noexcept

Sets the missing message hook and its user pointer.

function

Get

#
std::string_view Get( const std::uint32_t uId ) const noexcept

Returns a message through the fallback.

Never empty: the key comes last, and an id that does not exist gives "?". The everyday path (the requested catalog has the message) is inline.

uId
The message id.
The text.
function

Get

#
std::string_view Get( std::string_view sKey ) const noexcept;

Returns a message by key. The slow path.

sKey
The key.
The text. A key that does not exist returns itself.
function

Format

#
EI18nStatus Format( std::uint32_t uId, std::span< char > Out, std::size_t * pWritten, std::span< const SArg > Args = {} ) const noexcept;

Formats a message with its arguments.

uId
The message id.
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
Args
The arguments.
eOk or eBufferTooSmall. A translation that does not parse falls back to the next catalog, and last to the key.
function

Plural

#
EI18nStatus Plural( std::uint32_t uId, std::int64_t iCount, std::span< char > Out, std::size_t * pWritten, std::span< const SArg > Args = {} ) const noexcept;

Format with the argument "count" = iCount ahead of the others.

uId
The message id.
iCount
Value of "count".
Out
Output buffer.
pWritten
Receives the size of the whole result. May be null.
Args
The other arguments.
Same as Format.
function

FormatToString

#
std::string FormatToString( std::uint32_t uId, std::span< const SArg > Args = {} ) const;

The convenience that allocates (hence the name): the result in a std::string.

uId
The message id.
Args
The arguments.
The text. Empty when the message does not parse.

i18n_plural.hpp

#include <ethereal/cpp/i18n/i18n_plural.hpp>

CLDR plural rules for the I18n module.

Code that writes 'n == 1 ? singular : plural' is wrong for most of the world. English has two forms, Polish four, Arabic six, Japanese one. The rules come from the CLDR (the version is in the README) and cover every locale in it, not only the ones with formatting data. Each rule set is a generated function of integer comparisons, never an interpreted expression.

The number counts as it is written: "1" and "1.0" can be different plurals (in English, "1 file" but "1.0 files"), because the rules look at the visible digits. That is why there is a version that takes the number as text ("1.50", "-3", "1.2c6" in compact notation).

enum

EPluralCategory

Ethereal::I18n#
C++
enum class   EPluralCategory : std::uint8_t
{
    /// @brief  "zero".
    eZero = 0,

    /// @brief  "one".
    eOne,

    /// @brief  "two".
    eTwo,

    /// @brief  "few".
    eFew,

    /// @brief  "many".
    eMany,

    /// @brief  "other". Every locale has it.
    eOther,
};

A CLDR plural category.

function

PluralCategoryName

Ethereal::I18n#
const char * PluralCategoryName( EPluralCategory e ) noexcept;

Returns the CLDR name of a category.

e
The category.
"zero", "one", "two", "few", "many" or "other".
function

PluralCategoryFromName

Ethereal::I18n#
bool PluralCategoryFromName( std::string_view sName, EPluralCategory * pOut ) noexcept;

Finds a category by its CLDR name ("one" and so on).

sName
The name.
pOut
Receives the category. May be null.
false when the name is not one of the six.
class

CPluralRules

Ethereal::I18n#

The plural rules (cardinal and ordinal) of a locale.

class CPluralRules

Copied like an integer: it is two indices into the generated tables. Look it up once and keep it. Evaluating is one call and a few comparisons.

function

Find

#
static bool Find( std::string_view sTag, CPluralRules * pOut ) noexcept;

Finds the rules of a locale.

Falls back the way the CLDR says: with no rules for "pt-PT-x" it tries "pt-PT", then "pt", then the root.

sTag
BCP 47 tag ("pt-BR", "sr-Latn"). '_' is accepted.
pOut
Receives the rules.
false when only the root matched, or the tag does not parse.
function

Cardinal

#
EPluralCategory Cardinal( std::int64_t iValue ) const noexcept;

Cardinal category ("3 files") of an integer.

iValue
The number.
The category.
function

Cardinal

#
EPluralCategory Cardinal( std::string_view sNumber ) const noexcept;

Cardinal category of a number written as text.

Accepts "1.50", "-2", "1.2c6" (compact) or "1.2e6". The visible digits count, so "1.0" is not "1".

sNumber
The number as text.
The category. Text that is not a number gives eOther.
function

Ordinal

#
EPluralCategory Ordinal( std::int64_t iValue ) const noexcept;

Ordinal category ("3rd place") of an integer.

iValue
The number.
The category.
function

CardinalCategories

#
std::uint8_t CardinalCategories() const noexcept;

Cardinal categories the locale uses, one bit per EPluralCategory. 'other' is always set. Message authors use it to know which forms they must provide.

function

OrdinalCategories

#
std::uint8_t OrdinalCategories() const noexcept;

Ordinal categories the locale uses, one bit per EPluralCategory.