vibe.d beta banner
get vibe.d
0.7.30

Function translationContext

Annotates an interface method or class with translation information.

The translation context contains information about supported languages and the translated strings. Any translations will be automatically applied to Diet templates, as well as strings passed to vibe.web.web.trWeb.

By default, the "Accept-Language" header of the incoming request will be used to determine the language used. To override this behavior, add a static method determineLanguage to the translation context, which takes the request and returns a language string (see also the second example).

Prototype

TranslationContextAttribute!CONTEXT translationContext(CONTEXT)() @property;

Example

struct TranslationContext {
	import std.typetuple;
	alias languages = TypeTuple!("en_US", "de_DE", "fr_FR");
	//mixin translationModule!"app";
	//mixin translationModule!"somelib";
}

@translationContext!TranslationContext
class MyWebInterface {
	void getHome()
	{
		//render!("home.dt")
	}
}

Example

Defining a custom function for determining the language.

import vibe.http.server;

struct TranslationContext {
	import std.typetuple;
	alias languages = TypeTuple!("en_US", "de_DE", "fr_FR");
	//mixin translationModule!"app";
	//mixin translationModule!"somelib";

	// use language settings from the session instead of using the
	// "Accept-Language" header
	static string determineLanguage(scope HTTPServerRequest req)
	{
		if (!req.session) return null; // use default language
		return req.session.get("language", "");
	}
}

@translationContext!TranslationContext
class MyWebInterface {
	void getHome()
	{
		//render!("home.dt")
	}
}
Authors

Sönke Ludwig

Copyright

© 2014-2015 RejectedSoftware e.K.

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.