2009-07-18 61 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