localization in asp.net web forms with one global resource

柔情痞子 提交于 2019-12-31 02:55:12

问题


I want to have one some resource files like this:

Mui.resx
Mui.fr.resx
Mui.es.resx
...

and I want to be able to do in my codebehind something like this:

Label1.Text = Mui.Hello;

and in my aspx something like this:

<%=Mui.Hello %>

anybody knows how to do this ? is it possible ?


回答1:


Yes it is possible. You can add your resource file from File-New- Resource File and there you can add their own Resource key and their values as you want either in English or other language.,

Check these How to: Retrieve Resource Values Programmatically and ASP.NET Web Page Resources Overview

To retrieve global resources using strong typing

  • Get the resource using the following syntax:

    Resources.Class.Resource

Example:
String welcome;
welcome = Resources.WebResources.WelcomeText;

But if you using Localization Have you check this MSDN resouce:
Walkthrough: Using Resources for Localization with ASP.NET

Check the section Explicit Localization with ASP.NET on the link above.

Example:

label's text attribute now has an explicit expression stating the base file from which to retrieve the resource and the key to select.

<asp:Label ID="Label2" Runat="server" Text="<%$ Resources:LocalizedText, Msg1 %>">

And on your page: Simple call with full path of resource key.

Check this great article - Resources and Localization that will explain you more about this.

Edit- Web.Config Culture Settings:
Add this
<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true"/> in web.config under <system.web>

If you want it on some pages then use <@Page> directive's Culture and UICulture attributes.




回答2:


Add the Resources namespace.

Label1.Text = Resources.Mui.Hello;

and

<%=Resources.Mui.Hello %>



回答3:


I've the same problem and finally discovered the solution.

In the browser if you choose the browser language French [fr] ASP loads the resource.fr.resx

But, if you you choose the browser language French (France)[fr-FR] ASP loads the resource.fr-FR.resx



来源:https://stackoverflow.com/questions/8382769/localization-in-asp-net-web-forms-with-one-global-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!