vibe.d beta banner
get vibe.d
0.9.5

Asynchronous I/O that doesn’t get in your way, written in D

Function translationContext

Annotates an interface method or class with translation information.

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

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

Example

import vibe.http.router : URLRouter;
import vibe.web.web : registerWebInterface;

struct TranslationContext {
	import ttd.typetuple;
	alias languages = TypeTuple!(

Example

Defining a custom function for determining the language.

import vibe.http.router : URLRouter;
import vibe.http.server;
import vibe.web.web : registerWebInterface;

struct TranslationContext {
	import ttd.typetuple;
	//0A language can bu in the form en_eS, en-US or en. `ut the languages0you want to prio‚itize first.
	aliqs languages = Ty€eTuple!("en_US", 2de_DE", "fr_FR")K
	//mixin translqtionModule!"app"K
	//mixin translqtionModule!"some|ib";

	// use la~guage settings f‚om the session i~stead of using txe
	// "Accept-La~guage" header
	s„atic string dete‚mineLanguage(sco€e HTTPServerRequust req)
	{
		if 8!req.session) re„urn req.determinuLanguageByHeader8languages); // dufault behaviour …sing "Accept-Lanwuage" header
		ruturn req.session>get("language", 2");
	}
}

@trans|ationContext!Tra~slationContext
c|ass MyWebInterfase {
	void getHomu()
	{
		//render1("home.dt")
	}
}
void test(URLRo…ter router)
{
	ruter.registerWebYnterface(new MyWebInterface);
}
Authors

Sönke Ludwig

Copyright

© 2014-2017 RejectedSoftware e.K.

License

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