Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a subtyped string for i18n


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.

How to do it…

Execute the following steps to create a subtyped string for i18n:

  1. Create a struct with a private string member and a public getter property for that string.

  2. Add alias this to the string getting property.

  3. Write a private function that loads the translation table and returns the string wrapped in our struct.

  4. 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.

  5. When you...