Multi-lingual web application - how do I detect the user's language in ASP.NET?
Asked Answered
I

6

21

I'm building an ASP.NET web application, and all of my strings are stored in a resource file. I'd like to add a second language to my application, and ideally, I'd like to auto-detect the user's browser language (or windows language) and default to that, instead of making them choose something besides English. Currently, I'm handling all the resource population manually, so adding a second resource file and language is trivial from my point of view, if I had an easy way to automatically figure out what language to display.

Has anybody done this, or do you have any thoughts about how I might retrieve that value? Since ASP.NET is server-based, I don't seem to have any access to specific browser settings.

RESOLUTION: Here's what I ended up doing. I used a "For Each" to go through "HttpContext.Current.Request.UserLanguages" and search for one I support. I'm actually just checking the left two characters, since we don't support any dialects yet - just English and Spanish. Thanks for all the help!

Inquisitorial answered 1/11, 2008 at 23:10 Comment(0)
C
6

This article (linked to archive.org as original link is now dead) might be helpful with auto detecting the browser's language setting.

[EDIT] Yes. The quoted article does not use ASP.NET. This article does.

Choir answered 1/11, 2008 at 23:19 Comment(1)
Though it's not .NET, I found the first article way more informative than the second, and it moved me in the right direction.Inquisitorial
J
21

Try this in the web.config:

<globalization culture="auto" uiCulture="auto" />

This will cause ASP.NET to auto-detect the client's culture from the request header. You can also set this on a per-page basis via the Page attribute.

Juta answered 1/11, 2008 at 23:28 Comment(4)
maxam: I wrapped your code in a formatted code block to show the <>s. "edit" the response to see how this looks, if you're interested.Nomology
This worked for me (ps: I didn't need to use the 'enableClientBasedCulture' setting)Holm
As indicated here, enableClientBasedCulture "is not in use at this time"Consistent
Thanks, Mart! Edited the post to remove the attribute.Juta
C
6

This article (linked to archive.org as original link is now dead) might be helpful with auto detecting the browser's language setting.

[EDIT] Yes. The quoted article does not use ASP.NET. This article does.

Choir answered 1/11, 2008 at 23:19 Comment(1)
Though it's not .NET, I found the first article way more informative than the second, and it moved me in the right direction.Inquisitorial
V
2

This is a great question, as localization in ASP.NET is overlooked by many developers.

ASP.NET should automatically pick up on the user's browser settings and force the CultureInfo.CurrentCulture to the user's browser language. You can force the issue with a line in Page_OnInit() like:

Thread.CurrentThread.CurrentCulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0]);

How can you test this? Enter the languages panel on our browser and change settings.

Vassaux answered 1/11, 2008 at 23:25 Comment(0)
L
2

The client generally sets Accept-Language in the HTTP request header with a quantitatively scored list of preferred language, conventionally (but not necessarily) in order of most favored to least favored. You can parse that, but as Maxam has noted, ASP.NET does have a mechanism for doing that on your behalf.

Lions answered 2/11, 2008 at 3:34 Comment(0)
T
2

Request.UserLanguages in ASP.NET 4 parses this as a string array.

Good info: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Treponema answered 23/4, 2011 at 17:23 Comment(0)
F
1
    /// <summary>
    /// Sets a user's Locale based on the browser's Locale setting. If no setting
    /// is provided the default Locale is used.
    /// </summary>

public static void SetUserLocale(string CurrencySymbol, bool SetUiCulture)
{
    HttpRequest Request = HttpContext.Current.Request;
    if (Request.UserLanguages == null)
        return;

    string Lang = Request.UserLanguages[0];
    if (Lang != null)
    {
        // *** Problems with Turkish Locale and upper/lower case
        // *** DataRow/DataTable indexes
        if (Lang.StartsWith("tr"))
            return;

        if (Lang.Length < 3)
            Lang = Lang + "-" + Lang.ToUpper();
        try
        {
            System.Globalization.CultureInfo Culture = new System.Globalization.CultureInfo(Lang);
            if (CurrencySymbol != null && CurrencySymbol != "")
                Culture.NumberFormat.CurrencySymbol = CurrencySymbol;

            System.Threading.Thread.CurrentThread.CurrentCulture = Culture;

            if (SetUiCulture)
                System.Threading.Thread.CurrentThread.CurrentUICulture = Culture;
        }
        catch
        { ;}
    }
}

The source of this article is here: How to detect browser language

Fabrice answered 8/7, 2013 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.