Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC应用程序中访问本地化字符串时出现问题_Asp.net Mvc_Localization_Multilingual - Fatal编程技术网

Asp.net mvc ASP.NET MVC应用程序中访问本地化字符串时出现问题

Asp.net mvc ASP.NET MVC应用程序中访问本地化字符串时出现问题,asp.net-mvc,localization,multilingual,Asp.net Mvc,Localization,Multilingual,我创建了一个ASP.NET MVC应用程序,并在项目中为about.aspx页面添加了2个资源文件。看起来是这样的: 然后,我修改了About.aspx页面,如下所示: <asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server"> <h2><%= GetLocalResourceObject ("About")%></h2> &

我创建了一个ASP.NET MVC应用程序,并在项目中为about.aspx页面添加了2个资源文件。看起来是这样的:

然后,我修改了About.aspx页面,如下所示:

<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%= GetLocalResourceObject ("About")%></h2>
    <p>
        <%= GetLocalResourceObject ("PutContentHere")%>
    </p>
</asp:Content>
显示为hi,但仍显示默认文本(英文)。您能发现问题吗?

并且不会根据浏览器报告的内容自动更改。您需要指定:

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”)在尝试将其分配给时会导致异常,因为它不允许所谓的“中性”区域性。简言之,中立文化是指只识别一种语言,而不识别地理区域的文化。您可以在中了解更多信息。

我的母版页中也没有多少资源字符串。我必须在所有页面的资源文件中定义所有这些字符串吗?@Hermant:您为母版页添加资源的方式与为aspx页面添加资源的方式相同。不要调用资源文件pagefilename.aspx.resx,而是将其称为masterfilename.master.resx。