Skip to content
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

Deprecate EntityLocalizationMiddleware #334

Merged
merged 1 commit into from
Feb 23, 2017
Merged

Deprecate EntityLocalizationMiddleware #334

merged 1 commit into from
Feb 23, 2017

Conversation

macote
Copy link
Contributor

@macote macote commented Feb 6, 2017

Also, revert ca6e4f2 changes since HttpContext is a property in this context.

Also, revert ca6e4f2 changes since HttpContext is a property in this context.
@turquoiseowl
Copy link
Owner

Nice job, thanks.

@ajbeaven
Copy link
Contributor

ajbeaven commented Nov 7, 2017

I'm finding that Application_ReleaseRequestState is not executed so the ResponseFilter is never installed and any nuggets in the response are not translated. When is this event suppose to be triggered? Would using SignalR stop this from happening?

I also tried using the (now deprecated) EntityLocalizationMiddleware which does cause the ResponseFilter to be installed but unfortunately I get a NullReferenceException on m_stagingBuffer in the Write method :/

@ajbeaven
Copy link
Contributor

ajbeaven commented Nov 7, 2017

I'm guessing SignalR uses a different request/response lifecycle than a normal .NET request, which is why that event is never fired.

For those that come after me wanting to know how to get SignalR working with i18n, here's a custom HubPiplineModule that installs the ResponseFilter and avoids the NullReferenceException issue mentioned above:

public class i18nHubPipelineModule : HubPipelineModule
{
	private readonly i18nSettings _i18nSettings;

	public i18nHubPipelineModule(i18nSettings i18nSettings)
	{
		_i18nSettings = i18nSettings;
	}

	/// <summary>
	/// Translates the SignalR response sent back when a client sends a request
	/// </summary>
	protected override object OnAfterIncoming(object result, IHubIncomingInvokerContext context)
	{
		var requestUrl = context.Hub.Context.Request.Url.AbsoluteUri;
		LanguageTag.ExtractLangTagFromUrl(requestUrl, UriKind.Absolute, out var langTag);
		var translatedResult = TranslateResult(result, langTag);
		return base.OnAfterIncoming(translatedResult, context);
	}

	private object TranslateResult(object result, string langTag)
	{
		var translationRepository = new POTranslationRepository(_i18nSettings);
		var textLocalizer = new TextLocalizer(_i18nSettings, translationRepository);
		var nuggetLocalizer = new NuggetLocalizer(_i18nSettings, textLocalizer);
		var emailLanguageItems = new[] { new LanguageItem(new LanguageTag(langTag), LanguageItem.PalQualitySetting, 0) };

		var translatedJson = JsonConvert.SerializeObject(result);
		translatedJson = LocalizedApplication.Current.NuggetLocalizerForApp.ProcessNuggets(translatedJson, emailLanguageItems);
		return JsonConvert.DeserializeObject(translatedJson);
	}
}

It's a bit more convoluted than seems necessary but that's because HttpContext.Response doesn't exist when using websockets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants