0

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..

Vinoth Narayan
  • 275
  • 2
  • 15

2 Answers2

1

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.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • How to detect the user location – Vinoth Narayan Mar 29 '16 at 02:16
  • You "detect" the user location by what they request in the URL. Give the user the option to switch languages by changing the URL. Do not use `UserLanguages`, cookies, or session state for this purpose. See my linked answer for the solution. – NightOwl888 Mar 29 '16 at 03:03
  • I think i started up in a wrong way...my need in getting the user host ip address and region... i am getting the ip address by Request.UserHostAddress..ToString() ...Now i need to find user region so i tried the culture...is there any other way to find the user region – Vinoth Narayan Mar 29 '16 at 04:58
  • You don't seem to be getting the point. Don't use the region *at all*. Give the user a choice instead. If the user finds your site through a search engine, it will be though a search in their own language. When they click through to your site, the language/culture will be in the URL. From that point MVC will automatically pass the URL to `ActionLink` because it is in the context of the request. If you hard code some logic to select the culture for the user, you will no doubt lose users because your logic won't work for everyone in that region. – NightOwl888 Mar 29 '16 at 05:06
  • Actually i want to save the User ip and user region in my db...suggest me how to get the user region – Vinoth Narayan Mar 29 '16 at 05:09
0

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");
        }
    }
sagivasan
  • 97
  • 7
  • Depends on the user location the culture should change..how to detect the user location – Vinoth Narayan Mar 29 '16 at 02:15
  • you want to get user ip and region. this may help you out to get. [http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net] (get client ip) [http://stackoverflow.com/questions/4327629/get-user-location-by-ip-address](get location by ip) – sagivasan Mar 29 '16 at 05:34
  • i didnt use of those link. i just searched and post for your reference – sagivasan Mar 29 '16 at 17:31