Skip to content

Commit

Permalink
Merge pull request #1879 from WebFreak001/i18n-lang-array
Browse files Browse the repository at this point in the history
Added support for i18n language arrays
  • Loading branch information
s-ludwig authored Nov 5, 2017
2 parents af01069 + 88aaa6a commit cd46fb4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 42 deletions.
21 changes: 12 additions & 9 deletions web/vibe/web/i18n.d
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,19 @@ mixin template translationModule(string FILENAME)
{
import std.string : tr;
enum NAME = FILENAME.tr(`/.-\`, "____");
private mixin template file_mixin(size_t i) {
static if (i < languages.length) {
enum components = extractDeclStrings(import(FILENAME~"."~languages[i]~".po"));
mixin("enum "~languages[i]~"_"~NAME~" = components;");
//mixin decls_mixin!(languages[i], 0);
mixin file_mixin!(i+1);
}
private static string file_mixins() {
string ret;
foreach (language; languages)
ret ~= "enum "~language~"_"~NAME~" = extractDeclStrings(import(`"~FILENAME~"."~language~".po`));\n";
return ret;
}

mixin file_mixin!0;
mixin(file_mixins);
}

template languageSeq(CTX) {
static if (is(typeof([CTX.languages]) : string[])) alias languageSeq = CTX.languages;
else alias languageSeq = aliasSeqOf!(CTX.languages);
}

/**
Expand All @@ -174,7 +177,7 @@ template tr(CTX, string LANG)

string tr(string key, string key_plural, int n, string context = null)
{
static assert([CTX.languages].canFind(LANG), "Unknown language: "~LANG);
static assert([languageSeq!CTX].canFind(LANG), "Unknown language: "~LANG);

foreach (i, mname; __traits(allMembers, CTX)) {
static if (mname.startsWith(LANG~"_")) {
Expand Down
56 changes: 23 additions & 33 deletions web/vibe/web/web.d
Original file line number Diff line number Diff line change
Expand Up @@ -318,24 +318,19 @@ template render(string diet_file, ALIASES...) {
}
}

static if (is(TranslateContext) && TranslateContext.languages.length) {
static if (TranslateContext.languages.length > 1) {
switch (s_requestContext.language) {
default: {
TranslateCTX!(TranslateContext.languages[0]) renderctx;
renderctx.render();
return;
}
foreach (lang; TranslateContext.languages[1 .. $])
case lang: {
TranslateCTX!lang renderctx;
static if (is(TranslateContext) && languageSeq!TranslateContext.length) {
switch (s_requestContext.language) {
default:
mixin({
string ret;
foreach (lang; TranslateContext.languages)
ret ~= "case `" ~ lang ~ "`: {
TranslateCTX!`" ~ lang ~ "` renderctx;
renderctx.render();
return;
}
}
} else {
TranslateCTX!(TranslateContext.languages[0]) renderctx;
renderctx.render();
}";
return ret;
}());
}
} else {
vibe.http.server.render!(diet_file, req, ALIASES)(s_requestContext.res);
Expand Down Expand Up @@ -1039,7 +1034,6 @@ private void handleRequest(string M, alias overload, C, ERROR...)(HTTPServerRequ
}
}


private RequestContext createRequestContext(alias handler)(HTTPServerRequest req, HTTPServerResponse res)
{
RequestContext ret;
Expand All @@ -1056,23 +1050,19 @@ private RequestContext createRequestContext(alias handler)(HTTPServerRequest req
static if (FUNCTRANS.found) alias TranslateContext = FUNCTRANS.value.Context;
else static if (PARENTTRANS.found) alias TranslateContext = PARENTTRANS.value.Context;

static if (is(TranslateContext) && TranslateContext.languages.length) {
static if (TranslateContext.languages.length > 1) {
switch (ret.language) {
default:
ret.tr = &tr!(TranslateContext, TranslateContext.languages[0]);
ret.tr_plural = &tr!(TranslateContext, TranslateContext.languages[0]);
break;
foreach (lang; TranslateContext.languages[1 .. $]) {
case lang:
ret.tr = &tr!(TranslateContext, lang);
ret.tr_plural = &tr!(TranslateContext, lang);
break;
static if (is(TranslateContext) && languageSeq!TranslateContext.length) {
switch (ret.language) {
default:
mixin({
string ret;
foreach (lang; TranslateContext.languages) {
ret ~= "case `" ~ lang ~ "`:
ret.tr = &tr!(TranslateContext, `" ~ lang ~ "`);
ret.tr_plural = &tr!(TranslateContext, `" ~ lang ~ "`);
break;";
}
}
} else {
ret.tr = &tr!(TranslateContext, TranslateContext.languages[0]);
ret.tr_plural = &tr!(TranslateContext, TranslateContext.languages[0]);
return ret;
}());
}
} else {
ret.tr = (t,c) => t;
Expand Down

0 comments on commit cd46fb4

Please sign in to comment.