A specialized string is a type with a text representation. For example, user-visible text is a string, but not every string is user-visible text. When adding internationalization (often shortened to i18n) to an application, using a string subtype can help ensure that all messages are available to translators. Moreover, we'll use D's templates and compile-time string imports to efficiently load the translation file.
Execute the following steps to create a subtyped string for i18n:
Create a struct with a private string member and a public getter property for that string.
Add
alias this
to the string getting property.Write a private function that loads the translation table and returns the string wrapped in our struct.
Write a template that takes a key, file, and line number that assigns the result of the translation function to a member with the same name. You may also use
pragma(msg)
to print out the whole list of translatable strings.When you...