AttributeRouting icon indicating copy to clipboard operation
AttributeRouting copied to clipboard

Dynamic routing based on url.

Open Cefa68000 opened this issue 11 years ago • 2 comments

I am wondering about a problem i have couple of websites in different languages same application different bindings. The domain ends with se, fi, dk

And i would like to have different translated urls for different sites. But that does not seem possible right now if i use the fluenttranslationprovider a url for se would work on fi site and dk and thats not what i want (duplicate content and stuff)

How would i go about doing something like that because i don't have access to the request.url on application start so i dont know which url to handle. And is it even possible to change translation provider on begin request event??

Do you have any solution to my particular problem?

Cefa68000 avatar Dec 12 '12 00:12 Cefa68000

Hello,

I will upload a localization project with AR to Github during this week.

There are still a few issues but it will be open for discussion.

Cheers, Miguel

mdmoura avatar Dec 12 '12 00:12 mdmoura

Sorry for the huge delay. Here's some code that might work. I am still trying to understand the problem.

public static class AttributeRouting 
{
    public static void RegisterRoutes(RouteCollection routes) 
    {    
        // See http://github.com/mccalltd/AttributeRouting/wiki for more options.
        // To debug routes locally using the built in ASP.NET development server, go to /routes.axd

        routes.MapAttributeRoutes(config =>
        {
            config.AddRoutesFromAssemblyOf<DefaultController>();
            config.AddTranslationProvider(GetTranslationProvider());

            // Inbound request handling: 
            // - Don't honor URL's for other cultures.
            // - Determine the culture by taking the last section of the URL's host.
            // See: http://attributerouting.net/#localizing-urls
            config.ConstrainTranslatedRoutesByCurrentUICulture = true;
            config.CurrentUICultureResolver = 
                (httpContext, routeData) => httpContext.Request.Url.Host.Split('.').Last();
        });
    }

    // Outbound URL generation:
    // - Uses the CurrentUICulture of the executing thread.
    // - Set the culture by reflecting upon the last section of the URL's host.
    public static void SetCurrentUICulture(HttpRequest request)
    {
        var language = request.Url.Host.Split('.').Last();
        var cultureInfo = new CultureInfo(language);
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
    }

    private static TranslationProviderBase GetTranslationProvider()
    {
        var provider = new FluentTranslationProvider();

        provider.AddTranslations()
                .ForController<DefaultController>()
                .RouteUrl(x => x.About(), new Dictionary<string, string>
                {
                    { "es", "About-es" },
                    { "fr", "About-fr" }
                });

        return provider;
    }

    public static void Start() 
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

You'll also need to do this in your global.asax:

public MvcApplication()
{
    BeginRequest += (sender, args) => AttributeRouting.SetCurrentUICulture(Request);
}

The main points are:

  • To constrain inbound routes, use the ConstrainTranslatedRoutesByCurrentUICulture and CurrentUICultureResolver properties of the configuration object.
  • To constrain outbound routes (generated routes using UrlHelper), be sure to set the thread's CurrentUICulture property appropriately when your application fires its BeginRequest event.

If this doesn't help, please let me know.

mccalltd avatar Feb 14 '13 19:02 mccalltd