Dresel / RouteLocalization

RouteLocalization is an MVC and Web API package that allows localization of your attribute routes.
MIT License
67 stars 13 forks source link

subdomains for RouteLocalization #55

Closed moar closed 7 years ago

moar commented 8 years ago

I have implemented RouteLocalization, but I have different subdomains for each language and I don't know if it is possible to implement it with route localization: culture: es => es.domain.com/translatedRoute it => it.domain.com/translatedRoute The action and controller part is already implemented using RouletLocalization, and I would like to know if there's a way to handle the subdomain with RouteLocalization

Dresel commented 8 years ago

Since I'm gathering requirements for the next version - what is your exact use case? What do you want to handle on subdomain base - what does work, what does not work?

Maybe you can give me a little bit more information about your situation / code.

moar commented 8 years ago

What I want to achieve is the route to be translated along with the subdomain. If my webpage is in italian (it subdomain), spanish (es subdomain) and english (no subdomain):

mdmoura commented 8 years ago

@moar

If I understood you correctly you just need to set the culture on:

CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate = context => { 
    // Set culture here
};

You can detect which subdomain is being used by accessing

context.Request.Url.Host

Then you set the culture using:

return new CultureInfo(yourCulture);
moar commented 8 years ago

Oh, sorry but maybe I didn't explain it correctly. The aim is to register the subdomain at the same time the route translation is registered. That is to say, I don't want to translate just the controller and action part of the route, I also want the subdomain part to be registered, for example:

localization.ForCulture("fr") .ForNamedRoute("MyControllerMyActionRoute") .AddTranslation("fr.mydomain.com","MonContrôleur/MonAction");

Right now it's only possible to translate the area, controller and action parameters if I'm not wrong.

Dresel commented 8 years ago

The MVC routing framework does not have any special builtin subdomain functionality (nor does RouteLocalization yet). Normally you have to add the code for yourself (you will get some information if you google for "asp.net mvc routing subdomain").

If you have set the subdomain in the IIS bindings, then it is possible to access your website with multiple domains (or the host & application.config file for local development).

The only problem then is that you can visit every route with every subdomain. For example accessing es.webpage.com/italianRoute would also work.

To permit this you could implement an ActionFilter that redirects to the correct subdomain or return a 404. For example:

public class SubdomainActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        LocalizationRoute route = context.RequestContext.RouteData.Route as LocalizationRoute;

        // Route doesn't contain culture information so return
        if (route == null || string.IsNullOrEmpty(route.Culture))
        {
            return;
        }

        // Route contains culture information
        string cultureName = route.Culture;

        // Check if culture is part of the domain
        if (!context.RequestContext.HttpContext.Request.Url.Host.Contains(cultureName))
        {
            // If not, return 404
            context.Result = new HttpStatusCodeResult(404);
        }
    }
}