2009-07-18 62 views
2

我創建了一個ASP.NET MVC應用程序,並在項目中爲about.aspx頁面添加了2個資源文件。它看起來像這樣:在ASP.NET MVC應用程序中訪問本地化字符串的問題

alt folder structure http://i30.tinypic.com/2lxczth.png

然後我修改了About.aspx頁面如下:

<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server"> 
    <h2><%= GetLocalResourceObject ("About")%></h2> 
    <p> 
     <%= GetLocalResourceObject ("PutContentHere")%> 
    </p> 
</asp:Content> 

我試圖changing the firefox locale到HI-IN後運行有關網頁,但它仍然顯示默認文字(英文)。請你能發現問題嗎?

回答

3

CurrentCultureCurrentUICulture不會根據瀏覽器報告的內容自動更改。試圖將其分配到Thread.CurrentCulture

protected override void OnInit(EventArgs e) 
{ 
    try 
    { 
     System.Threading.Thread.CurrentThread.CurrentUICulture = 
          CultureInfo.GetCultureInfo(Request.UserLanguages[0]); 
     System.Threading.Thread.CurrentThread.CurrentCulture = 
          System.Threading.Thread.CurrentThread.CurrentUICulture; 
    } 
    catch (Exception ex) 
    { 
     // handle the exception 
    } 
    base.OnInit(e); 
} 

你應該注意到,一些你可以選擇語言(「EN」爲實例)的,將導致異常,因爲它沒有:您將需要指定允許所謂的「中立」文化。簡而言之,中立文化只能確定一種語言,而不是地理區域。您可以在the documentation for the CultureInfo class中閱讀更多。

+0

我的母版頁中也沒有資源字符串。我是否必須在所有頁面的資源文件中定義所有這些字符串? – Hemant 2009-07-18 11:37:42