Vinoth Narayan
Vinoth Narayan

Reputation: 275

Dynamically change the page culture depends on the region

I need to change cultures at run-time according to Region. I have made the below settings in web.config

<configuration>
<system.web>
<globalization culture="auto:en-US" uiCulture="fr" />
</system.web>
</configuration>

i am using this blog as references https://weblog.west-wind.com/posts/2014/Mar/27/Auto-Selecting-Cultures-for-Localization-in-ASPNET#ASP.NETNativeSupportforAutoLocaleSwitching

but i don't WebUtils... suggest me a blog and some ideas..

Upvotes: 0

Views: 1508

Answers (2)

NightOwl888
NightOwl888

Reputation: 56859

You should not hard wire the culture using UserLanguages. Instead, you should put the culture in the URL, which gives the user a choice of which culture to use.

If your site is Internet facing, this means that each one of your cultures will be indexed and be searchable in the native language. If your site depends on the UserLanguages setting of the browser, your non-default cultures will not be indexed for search.

True Story

I had my browser's home page set to MSN for several years. However, when I moved to Thailand suddenly the page was displayed in Thai. At that time, I could not read Thai.

Even worse, there was no (obvious) way to select the language from the user interface. When I tried to change the URL to en-us, it redirected me back to the page in Thai language. I hunted for quite some time, but couldn't find a solution to switch the page back to English.

That was 7 years ago. Since then, my home page has been set to Google. Recently, I tried to use the MSN site, and it still does not have a way to switch it to English.

The point: Always give the user a choice of what language to view.

Upvotes: 1

sagivasan
sagivasan

Reputation: 97

you need to add this function in Global.asax. it will get culture.

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        string culture = null;
        var request = HttpContext.Current.Request;
        string cultureName = null;

        // Attempt to read the culture cookie from Request
        var cultureCookie = request.Cookies["_culture"];

        if (cultureCookie != null)
            cultureName = cultureCookie.Value;
        else if (request.UserLanguages != null)
            cultureName = request.UserLanguages[0]; // obtain it from HTTP header AcceptLanguages

        culture = !string.IsNullOrEmpty(request.QueryString["culture"])
            ? request.QueryString["culture"]
            : cultureName;

        if (culture == null) return;

        var cultures =
            CultureInfo.GetCultures(CultureTypes.SpecificCultures);
        if (cultures.Any(cultureData => cultureData.Name == cultureName))
        {
            var cultureInfo = CultureInfo.GetCultureInfo(culture);
            Thread.CurrentThread.CurrentCulture = cultureInfo;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-IN");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-IN");
        }
    }

Upvotes: 0

Related Questions