Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to URL route based on UICulture settings?

I have ASP.NET 4 project (not MVC). I need to create url route based on user input language.

Project has only two languages "he" and "en".

User can enter the site and if his culture is set to anything besides he-IL i want to re-route him to website.com/en/ otherwise to website.com/he/

Default.aspx should remain same page which uses Globalization features translate values based on user's culture settings in browser.

How can i do that? what should i do besides writing a route in Global.asax and How to write this route.

like image 397
eugeneK Avatar asked Nov 14 '22 23:11

eugeneK


1 Answers

This shouldnt be hard. Yes the Global.ascx is the best place to start.

First map the Routes,

    protected void RegisterRoutes(RouteCollection routes)
    {
        //Contact route for EN culture
        routes.MapPageRoute(
            "contactRouteEN",
            "en/contact",
            "~/Contact.aspx"
        );

        routes.MapPageRoute(
            "contactRouteHE",
            "he/contact",
            "~/Contact.aspx"
        );
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

That much establishes the routes.

The problem your describing sounds more like a Globalization issue than url routing problem. The url portion of the issue will be cosmetic to the user but won't attack the underlying issue in my view. ASP.Net provides facilities for Globalization. For example you can use LocalResources. To do this for the pages at your applications root level (not nested inside folders) Right click the website and choose Add ASP.Net Folder Choose App_LocalResources. Right click the App_LocalResources folder and choose Add Item Choose Resource File.

It is important that you name the file according to the culture you plan to target

You can create the first file to be Contact.aspx.resx to be the default resource file (maybe english?)

ASP.Net will try to find the most specific culture to match the resource files to and will resort to the default if a more specific is not provided.

The naming convention follows PageName.aspx.languageID-cultureId.resx

You could have Contact.aspx.he.resx

In a label control for example you could set it like this

<asp:Label ID="lbContactMessage" runat="server" Text="something" meta:resourcekey="yourmatchingkeyfromresourcefile"></asp:Label>

For more info see http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx

like image 133
Michael Christensen Avatar answered Dec 18 '22 06:12

Michael Christensen