-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ParseAndTranslate change headers. #338
Comments
Yes, I agree with your suggestion. That feature was an afterthought... I would be happy to merge a PR on this. Thanks. |
@jeancroy looks like you call ParseAndTranslate method inside environment without http context, is it correct? |
I considered it. My use case is producing emails and reports in a background process. public static string TranslateNugget(string input, ILanguageTag lt = null)
{
if (lt == null) lt = HttpContext.Current.GetPrincipalAppLanguageForRequest();
var langs = new[] { new LanguageItem(lt, LanguageItem.PalQualitySetting, 0) };
var nuggetLocalizer = LocalizedApplication.Current.NuggetLocalizerForApp;
if (nuggetLocalizer != null)
{
return LocalizedApplication.Current
.NuggetLocalizerForApp.ProcessNuggets(input, langs)
.Replace("\x5b\x5b\x5b","") // Remove any remaining [[[ ]]]
.Replace("\x5d\x5d\x5d", "");
}
return input;
} Now if you truly don't have any context the above lt==null case wont work. var oldHttpContext = HttpContext.Current;
var fakeHttpContext = new HttpContext(new HttpRequest(null, "fakeurl", null), new HttpResponse(null));
HttpContext.Current = fakeHttpContext;
var langTag = LanguageHelpers.GetMatchingAppLanguage(lang);
fakeHttpContext.SetPrincipalAppLanguageForRequest(langTag); |
@jeancroy I made the change as you initially suggested. But I don't sure if anyone's code already rely on existing side effect when calling manually HttpContext.ParseAndTranslate method. I assume it won't if i18n used by asp.net request pipeline, otherwise headers leave intact. I expect @turquoiseowl can provide any thoughts about it? |
Another reason I've made a stub is that I'm trying to render views to string, and the view engine need such stub for things like @Url.Action() to point to the proper place. I also use it to provide context to format date & numbers with user locale. |
@jeancroy just interesting, how i18n helps with date & numbers format, can you provide an example? |
@vhatuncev i18n does the following by default for a request that matches an application language:
This is in the default LocalizedApplication.SetPrincipalAppLanguageForRequestHandlers delegate. |
Apologies for slow response to the PR. I've now merged it and tested ParseAndTranslate and normal request handling, and all looks good. Suggest this can be closed. |
The recommended way to translate a segment of text is
However it does a bit more that just translate the text, it try to setup headers as a side effect.
It make a call to ProcessOutgoing which aim to rewrite url consistently with url localisation scheme. BUT that method also try to rewrite url in headers
Possible problem of those hidden side effect include error when there are no http response. (for example sending email at scheduled time)
Possible solution include splitting
ProcessOutgoing
asProcessOutgoingNuggets
andProcessOutgoingHeaders
. Then call header localization only when filtering request.The text was updated successfully, but these errors were encountered: