This library aims to facilitate native language support in applications and libraries written in D. String resources containing natural language text are read from XML documents, called catalogs, supplied at compile-time. In source code, string resources are referenced with the strings interface. The languages to use are configured automatically at the start of the program based on the running user's environment, before the main function is entered.
The primary catalog must have the following structure:
$(LESS)?xml version="1.0" encoding="utf-8"?$(GREATER) $(LESS)resources language="primary_catalog_language"$(GREATER) $(LESS)translation language="translation1"/$(GREATER) $(LESS)translation language="translation2"/$(GREATER) $(LESS)translation language="..."/$(GREATER) $(LESS)string name="id1"$(GREATER)text1$(LESS)/string$(GREATER) $(LESS)string name="id2"$(GREATER)text2$(LESS)/string$(GREATER) $(LESS)string name="..."$(GREATER)...$(LESS)/string$(GREATER) $(LESS)/resources$(GREATER)
For the primary catalog, the root element's language attribute is required and contains the language used in the primary catalog. Each translation element declares that a translation catalog for the given language is supplied and should be loaded. All language attributes are ISO-639 language codes. Each string element defines a string resource, where the name attribute is the resource identifier, and the element's content is the resource text.
The structure of translation catalogs is a subset of the structure of the primary catalog:
$(LESS)?xml version="1.0" encoding="utf-8"?$(GREATER) $(LESS)resources$(GREATER) $(LESS)string name="id1"$(GREATER)text1$(LESS)/string$(GREATER) $(LESS)string name="id2"$(GREATER)text2$(LESS)/string$(GREATER) $(LESS)string name="..."$(GREATER)...$(LESS)/string$(GREATER) $(LESS)/resources$(GREATER)
Each string element provides a translation of the string resource with the given identifier. The identifier must match the identifier of a string resource in the primary catalog.
void main() { import std.stdio, i18n.text; // Writes the string resource with the identifier "hello_world" to stdout writeln(strings.hello_world); }
gettext's advice on separating strings and translating proper names
Text translation framework.