多语言 Web 应用程序 - 如何在 ASP.NET 中检测用户的语言?

发布于 2024-07-08 10:10:45 字数 449 浏览 11 评论 0原文

我正在构建一个 ASP.NET Web 应用程序,所有字符串都存储在资源文件中。 我想向我的应用程序添加第二种语言,理想情况下,我想自动检测用户的浏览器语言(或 Windows 语言)并默认为该语言,而不是让他们选择英语以外的语言。 目前,我正在手动处理所有资源填充,因此,如果我有一种简单的方法来自动确定要显示的语言,那么从我的角度来看,添加第二个资源文件和语言是微不足道的。

有没有人这样做过,或者您对我如何检索该值有任何想法吗? 由于 ASP.NET 是基于服务器的,因此我似乎无法访问特定的浏览器设置。

解决方案:这就是我最终所做的。 我使用“For Each”来浏览“HttpContext.Current.Request.UserLanguages”并搜索我支持的语言。 实际上,我只是检查左边的两个字符,因为我们还不支持任何方言 - 仅支持英语和西班牙语。 感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

云胡 2024-07-15 10:10:45

在 web.config 中尝试此操作:

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

这将导致 ASP.NET 从请求标头自动检测客户端的区域性。 您还可以通过“页面”属性在每个页面上进行设置。

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.

表情可笑 2024-07-15 10:10:45

这篇文章(链接到 archive.org,因为原始链接现已失效) 可能有助于自动检测浏览器的语言设置。

[编辑] 是的。 引用的文章没有使用 ASP.NET。 这篇文章确实如此。

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.

陪你搞怪i 2024-07-15 10:10:45

ASP.NET 4 中的 Request.UserLanguages 将此解析为字符串数组。

好信息: http://www.w3.org/Protocols/rfc2616/rfc2616- sec14.html

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

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

旧城烟雨 2024-07-15 10:10:45

这是一个很好的问题,因为 ASP.NET 中的本地化被许多开发人员忽视了。

ASP.NET 应该自动获取用户的浏览器设置,并强制将 CultureInfo.CurrentCulture 设置为用户的浏览器语言。 您可以使用 Page_OnInit() 中的一行来强制解决该问题,例如:

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

您如何测试这个? 进入浏览器上的语言面板并更改设置。

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.

霞映澄塘 2024-07-15 10:10:45

客户端一般会在 HTTP 请求头中设置 Accept-Language ,并给出量化评分列表首选语言,通常(但不一定)按最喜欢到最不喜欢的顺序排列。 您可以对其进行解析,但正如 Maxam 所指出的, ASP.NET 确实有一种机制代表您这样做。

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.

ㄟ。诗瑗 2024-07-15 10:10:45
    /// <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
        { ;}
    }
}

这篇文章的来源在这里:
如何检测浏览器语言

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文