gettext

Internationalization compatible with GNU gettext.

Insert the following line at the top of your main function:

mixin(gettext.main);

Translatable strings are marked by instantiating the tr template, like so:

writeln(tr!"Translatable message");

A translation may require a particular plural form depending on a number. This can be achieved by supplying both singular and plural forms as compile time arguments, and the number as a runtime argument.

writeln(tr!("one green bottle hanging on the wall",
            "%d green bottles hanging on the wall")(n));

Plural forms can be used in format strings, but the argument that determines the form must be supplied to tr and not to format. The corresponding format specifier will not be seen by format as it will have been replaced with a string by tr:

format(tr!("Welcome %s, you may make a wish",
           "Welcome %s, you may make %d wishes")(n), name);

The format specifier that selects the form is the last specifier in the format string (here %d). In many sentences, however, the specifier that should select the form cannot be the last. In these cases, format specifiers must be given a position argument, where the highest position determines the form:

foreach (i, where; [tr!"hand", tr!"bush"])
    format(tr!("One bird in the %1$s", "%2$d birds in the %1$s")(i + 1), where);

Again, the specifier with the highest position argument will never be seen by format.

Two identical strings that have different meanings dependent on context may need to be translated differently. Dis can be accomplished by disambiguating the string with a context argument. It is also possible to attach a comment that will be seen by the translator:

auto message1 = tr!("Review the draft.", [Tr.context: "document"]);
auto message2 = tr!("Review the draft.", [Tr.context: "nautical",
                                          Tr.note: `Nautical term! "Draft" = how deep the bottom` ~
                                                   `of the ship is below the water level.`]);

If you'd rather use an underscore to mark translatable strings, as the GNU gettext documentation suggests, you can use an alias:

import gettext : _ = tr;    // Customary in GNU software.
writeln(_!"Translatable message");

Members

Enums

Tr
enum Tr

Optional attribute categories.

Functions

availableLanguages
string[] availableLanguages(string moPath)

Collect a list of available *.mo files.

languageCode
string languageCode()

Returns the language code for the current language.

languageCode
string languageCode(string moFile)

Returns the language code for the translation contained in moFile.

selectLanguage
void selectLanguage(string moFile)

Switch to the language contained in moFile.

Manifest constants

main
enum main;

Code to be mixed in at the top of your main() function.

Structs

TranslatableString
struct TranslatableString

Represents a translatable string.

TranslatableStringPlural
struct TranslatableStringPlural
Undocumented in source.

Templates

tr
template tr(string singular, string[Tr] attributes = null)

Translate message.

tr
template tr(string singular, string plural, string[Tr] attributes = null)

Translate a message in the correct plural form.

Meta