Book Image

D Web Development

By : Kai Nacke
Book Image

D Web Development

By: Kai Nacke

Overview of this book

D is a programming language with C-like syntax and static typing. The vibe.d framework builds on powerful D concepts like template meta-programming and compile-time function execution to provide an easy-to-use environment for web applications. The combination of a feature-rich web programming framework with a language compiling to native code solves two common issues in web development today: it accelerates your development and it results in fast, native web applications. Learning the vibe.d framework before you start your application will help you to choose the right features to reach your goal. This book guides you through all aspects of web development with D and the vibe.d framework. Covering the popular operating systems today, this guide starts with the setup of your development system. From the first Hello World-style application you will move on to building static web pages with templates. The concise treatment of web forms will give you all the details about form handling and web security. Using the abstractions of the web framework you will learn how to easily validate user input. Next, you will add database access to your application, providing persistent storage for your data. Building on this foundation, you will expose your component and integrate other components via REST. Learning about the internals of vibe.d you will be able to use low-level techniques such as raw TCP access. The vibe.d concepts can also be used for GUI clients, which is the next topic that you will learn. vibe.d is supported by an active community, which adds new functionality. This comprehensive guide concludes with an overview of the most useful vibe.d extensions and where to find them. It also shows you how to integrate these extensions in your application. The concepts are always illustrated with source code, giving you an insight into how to apply them in your application.
Table of Contents (17 chapters)
D Web Development
About the Author
About the Reviewers

Localizing the web content

In Chapter 2, Using Templates for Your Web Pages, you learned how to modify templates to use different languages based on user preferences. With the web framework, the coding is reduced. You need the same TranslationContext struct as follows:

struct TranslationContext
  import std.typetuple : TypeTuple;

  alias languages = TypeTuple!("en_US", "de_DE");
  mixin translationModule!"example";

  static string determineLanguage(HTTPServerRequest req)
    import std.string : split, replace;

    auto acc_lang = "Accept-Language" in req.headers;
    if (acc_lang)
      return replace(split(*acc_lang, ",")[0], "-", "_"); 
    return null;

In order to use this context, you annotate the application class:

class NoteApplication
{ /* ... */ }

Then, you can use the trWeb() function in your application class to retrieve a translated text. The translation context is also automatically passed to your templates.

Error messages should...