turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 156 forks source link

How to use RedirectToAction with i18n? #218

Closed asapostolov closed 8 years ago

asapostolov commented 8 years ago

I'm using UrlLocalizationScheme.Scheme2 to localize the urls. I see that the url-s in the html are updated with the proper language value but when I do

return RedirectToAction("test");

from a localized url(/en/something) I got redirected to the base language url(/some-result) instead of the localized one(/en/some-result).

Which is the proper way to use RedirectToAction with the localization of the urls?

turquoiseowl commented 8 years ago

You say you are redirected to /some-result. Does i18n then redirect you again to /en/some-result?

Please also look at the example SetLanguage action which illustrates how to go about setting the langtag in the url: https://github.com/turquoiseowl/i18n#explicit-user-language-selection

asapostolov commented 8 years ago

You say you are redirected to /some-result. Does i18n then redirect you again to /en/some-result?

No, after I get redirected to /some-result the language is changed to the default language of the application and I'm seeing the proper result page but in the wrong language - interface, links etc.

The "Explicit User Language Selection" explains how to change the current language to a new one. What I'm observing is an automatic change of the language that is not intended after using RedirectToAction(actionName); method.

To simplify the question - do i18n support ASP.NET's RedirectToAction() method or I need to use UrlLocalizerForApp.SetLangTagInUrlPath() combined with the base Redirect(url) method everywhere?

turquoiseowl commented 8 years ago

As far as I can see, RedirectToAction 'should' work. Presuming it just does a normal 301/302 HTTP redirect, then i18n is designed to pick up the Location header in the response and patch the url in there with the current langtag.

This happens in EarlyUrlLocalizer.ProcessOutgoing.

Are you able to put a breakpoint in there to see what's going on?

asapostolov commented 8 years ago

I did look at the ProcessOutgoing method but could not understand what's happening.

There is a POST request to /en/subscribe which gets redirected to /subscribe which calls the RedirectToAction method.

Can you look at the simple test project that I made? You can download it from here

You need to add a reference to i18n to the project and start it. When you start it type /en/subscribe as a URL and submit the form to see the undesired redirection.

Thanks for the help!

asapostolov commented 8 years ago

After further debugging and investigation it seems like indeed the RedirectToAction method is not working out of the box with the localized URLs. I created this method to work around the limitation.

public ActionResult RedirectToLocalizedAction( string actionName, object routeValues = null ) {
    return Redirect(
        i18n.LocalizedApplication.Current.UrlLocalizerForApp.SetLangTagInUrlPath( 
            HttpContext, 
            Url.Action( actionName, routeValues ),
            UriKind.RelativeOrAbsolute,
            HttpContext.GetPrincipalAppLanguageForRequest().GetLanguage() )
        );
}