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
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 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")
}
}
void test(URLRouter router)
{
router .registerWebInterface(new MyWebInterface);
}
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 std .typetuple;
// A language can be in the form en_US, en-US or en. Put the languages you want to prioritize first.
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 req .determineLanguageByHeader(languages); // default behaviour using "Accept-Language" header
return req .session .get("language", "");
}
}
@translationContext!TranslationContext
class MyWebInterface {
void getHome()
{
//render!("home.dt")
}
}
void test(URLRouter router)
{
router .registerWebInterface(new MyWebInterface);
}