簡體   English   中英

CultureInfo.CurrentCulture給了我錯誤的文化

[英]CultureInfo.CurrentCulture is giving me the wrong culture

我正在嘗試獲取客戶的國家/地區,因此我使用CultureInfo.CurrentCulture。 問題是,當我的加拿大客戶使用我的網站時,他們會顯示為美國人。

看起來CultureInfo.CurrentCulture正在返回我服務器的國家而不是他們的國家。 那么我如何獲得客戶的國家?

您只需要在web.config文件中將culture屬性設置為auto

<system.web>
    <globalization culture="auto" />
<system.web>

這將自動將CurrentCulture設置為客戶端的文化。

如果您使用的是本地化資源,也可以將uiCulture設置為auto

我相信你需要編寫代碼來從傳入的瀏覽器請求中讀取用戶的文化 ,並從中設置CultureInfo。

這個人描述了他們是如何做到的 :將當前線程的顯示文化設置為來自用戶傳入的Http“request”對象的最合適的文化。

他在那里進行了很好的討論,但這基本上就是他如何做到的:

Page_Load ,他們進行此調用: UIUtilities.setCulture(Request);

這就是所謂的:

/// Set the display culture for the current thread to the most
/// appropriate culture from the user's incoming Http "request" object.
internal static void setCulture(HttpRequest request)
{
    if (request != null)
    {
      if (request.UserLanguages != null)
      {
        if (request.UserLanguages.Length > -1)
        {
          string cultureName = request.UserLanguages[0];
          UIUtilities.setCulture(cultureName);
        }
      }
        // TODO: Set to a (system-wide, or possibly user-specified) default
        // culture if the browser didn't give us any clues.
    }
}

/// Set the display culture for the current thread to a particular named culture.
/// <param name="cultureName">The name of the culture to be set 
/// for the thread</param>
private static void setCulture(string cultureName)
{
    Thread.CurrentThread.CurrentCulture = 
        CultureInfo.CreateSpecificCulture(cultureName);
    Thread.CurrentThread.CurrentUICulture = new
        CultureInfo(cultureName);
}

在我的情況下,我的機器最初安裝了英語 - 英國。 我添加了英語 - 美國語言並將其設置為默認語言。 我還驗證了美國在注冊表中設置正確。 可悲的是, System.Threading.Thread.CurrentThread.CurrentCulture仍然顯示錯誤的文化,英國。 我發現你需要設置語言選項。 下載語言包,手寫和語音。

即便如此,文化也是錯誤的。 英國將出現在整個機器上,在我安裝美國語言包后,開始菜單完全瘋了。 我放棄並重新安裝了使用英美版本的操作系統。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM