justinas / gettext

Gettext for Rust. Kind of.
MIT License
56 stars 8 forks source link

pgettext/npgettext allocate #10

Open justinas opened 5 years ago

justinas commented 5 years ago

Catalog::pgettext and Catalog::npgettext allocate a String on every call due to the need to form a key {context}\x04{msgid}.

The reference implementation of gettext solves this elegantly via a macro that concatenates the string literals at compile time.

# define pgettext(Msgctxt, Msgid) \
   pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)

Possible solutions

Macros

A macro like pgettext!(catalog, "context", "msgid") could be made that would expand to catalog.gettext(concat!("context", "\x04", "msgid")). However, now we have two API styles: pgettext!(catalog, ...) and catalog.pgettext(...).

Change internal format

Index the internal translation dictionary by (msgid, context): (&str, Option<&str>). Get rid of \x04 past the parsing stage altogether. I am leaning towards this solution.