access resource text from markup page - c#

I'm creating an web application with multiple languages.
I've set the culture like this
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
I've got several language files like "en.resx" and "de.resx".
I can read them from my code behind them like this
var test = GetGlobalResourceObject(Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk");
But how about from the markup page.
I've been searching the web and most pages is suggesting something like this
<asp:Literal Text='<%$ Resources:Resource, aboutUsLnk %>' runat="server" />
That works if I have a .resx file called Resource but i that's not what I want. What did I miss?

That works if I have a .resx-file called Resource but i thats not what I want. What did I miss?
You are probably looking for local resources and meta:resourcekey attribute.
Local are defined per page (you define exactly the same name as your page for them), you use them for storing resources specific to one page. You create them by adding ASP.NET specific folder (App_LocalResources) and then inside it local resources for each of your pages:
App_LocalResources/{pagename}.resx
And then call for resource objects from resource files (AboutUs.resx, AboutUs.fr-BE.resx,...) from the page markup (AboutUs.aspx) would be something like this:
<asp:Literal Text='About Us' meta:resourcekey="aboutUsLnk" runat="server" />
Global resources which you mentioned are defined accros the whole website (usually you store here resources like "Edit", "Save", etc.) and are usually called as you showed.
Read here for more details: http://msdn.microsoft.com/en-us/magazine/cc163566.aspx
EDIT
Ahh sorry for misunderstanding, you are probably asking how to call your global resources which names differentiate per culture. You can do that in your markup code almost the same as you are doing in code behind, using GetGlobalResourceObject.
Anywhere outside of server controls you can write:
<%= GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk")%>
To call GetGlobalResourceObject inside of server control attributes you cannot use <%= %>, but you can wrap the server controls around it (in the ones that allow this, like Label for example):
<asp:Label ID="Label1" runat="server"><%= GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk")%></asp:Label>
Or, you can use the binding syntax:
<asp:Label ID="Label1" runat="server" Text='<%# GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk")%>'></asp:Label>
Note, that when using the latter you will need to bind your control:
protected void Page_Load(object sender, EventArgs e)
{
Label1.DataBind();
}
EDIT 2
You can wrap the upper code in some helper method to improve code readabilty. In code behind you declare it:
protected string GetResource(string resourceName)
{
return GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), resourceName).ToString();
}
And in markup you can call it similiar as previous:
<asp:Label ID="Label1" runat="server" Text='<%# GetResource("aboutUsLnk")%>'></asp:Label>
<asp:Label ID="Label2" runat="server"><%= GetResource("aboutUsLnk")%></asp:Label>

Related

Accessing Properties.Resources from SharePoint ASPX page

This might be a pretty silly question, but I'm new to ASP.NET and I'm not able to pull values from myProject.Properties.Resources within the ASPX-page itself.
I've tried the below but the page fails to load
<asp:Label ID="Label1" runat="server" Text="<%$ Resources: Resources, string1 %>">
If it makes any difference, this page is a part of a SharePoint project and the page is in myProject/ADMIN/myProject/mypage.aspx
Any help here would be really appreciated

Issue with asp.net expression builder

I have the same issue mentioned in this question, but the accepted answer doesn't work for me. Basically I simply would like to add translations to elements (e.g. button, label) in a webforms page.
I checked other sites too, they all point to the same solution. E.g. this this MSDN article says:
For example, when localizing content automatically, you can set the Text property of a server control using expression syntax, as in this example:
<asp:Label id="label1" runat="server" text="<%$ Resources: Messages, ThankYouLabel %>" />
In the App_GlobalResources folder, you could have resource files named Messages.resx, Messages.es.resx, Message.de.resx, and so on—a Messages resource file for each language you want to support
In my case, the translation is not picked up, I see always the neutral language (English).
I made a dummy skeleton project too with only a few lines of code, there I have 2 resource files in the App_GlobalResources folder:
MyResources.resx
MyResources.nl.resx
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("nl");
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("nl");
}
Default.aspx
<%-- this displays the translation (nl) --%>
<%= Resources.MyResource.MyKey %>
<%-- this displays the neutral language (en) --%>
<asp:Label ID="Label1" runat="server" Text="<%$ Resources: MyResource, MyKey %>" />
Any ideas what do I miss?
After several tries (and failures) I got it working. This answer and this MSDN page put me on the right track.
In short, I needed override the InitializeCulture method like this:
protected override void InitializeCulture()
{
UICulture = "nl";
Culture = "nl"
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("nl");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl");
base.InitializeCulture();
}
To make it a general solution, I had to create a new PageBase class, which is inherited by all pages, as it is nicely described here.

how to use Local resource file in client side

In my application I want to use a local resource file string on client side without any jquery and javascript etc.
Currently I'm using code behind but would like to use in client side
awec.Text= Localization.GetString("ReqLodgeName.Text", LocalResourceFile);
like this. How do I use this resource file on client side for ASP control like
<asp:Label Id="awec" runat="server" Text='I want to access here' />
Say you have the key "ReqLodgeName.Text" with value 'I want to access here' in your LocalResourceFile which is in the App_LocalResources folder, you may then use the meta:resourcekey attribute: in your label as follows to retrieve the text:
<asp:Label id="awec" runat="server" meta:resourcekey="ReqLodgeName" Text='I want to access here' />
Or may explicitly localize using a different syntax instead of meta:resourcekey:
<asp:Label id="awec" Text="<%$ Resources:WebResources, ReqLodgeName %>" />
where WebResources is the name of the resx file with the resources in the App_GlobalResources folder and ReqLodgeName is the key name that has the text 'I want to access here'.
You can use the following. Add public method to code behind:
public string MethodName(string RequiredResourceker)
{
//return resource depending on RequiredResourceker parameter
}
modify you client control as follows:
Text="<%=MethodName("RequiredKey") %>"

Label/Literal control with formatted text field

I have discovered that it is possible to populate resource strings with variable information using string.format, see below:
String.Format(Resources.Temp.TempString, Resources.Contact.PhoneSales)
I can display this on my page using:
<p><%= String.Format(Resources.Temp.TempString, Resources.Contact.PhoneSales) %></p>
In some cases I have a Label or Literal (or any control) which might dynamically hide or show content. Ordinarily I would populate those using:
<asp:Literal ID="Literal1" Text="<%$ Resources:Temp,ContactUs %>" runat="server" />
I now would like the same String.Format functionality whilst still using controls. I found Display value of Resource without Label or Literal control but this doesn't actually work for me, it just writes out '<%= GetGlobalResourceObject("Messages", "ThankYouLabel") %>' on the page (not the content, that actual string).
UPDATE:
I have found a solution which works with some controls:
<asp:Label runat="server" ID="temp"><%= String.Format(Resources.Temp.TempString, Resources.Contact.PhoneSales) %></asp:Label>
However, this works doesn't work for Literal controls as they don't allow child controls. I'd prefer to keep using Literal's as they are the cleanest in terms of code generated, so still seeking a solution.
asp:Literal doesn't support <%= %> construct, and doesn't allow child controls (I mean something like <asp:Literal runat="server"><%= ... %></asp:Literal>).
But if you use data binding, your could use data-binding expresions <%# ... %>:
<asp:Label runat="server" Text="<%# string.Format(...) %>"></asp:Label>
To make this work you should ensure that either implicit or explicit data binding for your controls is used. Otherwise the control like this without binding outputs nothing.
This workaround is a little bit complex. Consider using either asp:Label control, or set the Text property from the code behind.
To solve my problem I have actually had a second look at how I am displaying content and found that a lot of times the Literals and Labels could be dropped in place of plain HTML code. I can then use my preferred method <%= ... %> to display content.
You could use an asp:PlaceHolder control instead of a Literal.
PlaceHolders can contain child controls; they also support <%= … %>-style "displaying expressions".

How do I programmatically set property of control in aspx file?

This may be a very dumb question but I can't seem to get it working. I use at many places the following syntax for dynamically binding a property of a control in aspx file to the resource entry, e.g.
<SomeFunnyControl Text="<%$ Resources : ResClass, ResEntry %>" />
I want to do a similar thing with a class containing some constants, something like
<SomeFunnyControl Text="<%= MyConstantsClass.MyStringConstant %>" />
But this doesn't seem to work, it simply sets the text to the exact expression without evaluating it. I am using ASP.NET 3.5 btw.
I have tried the databinding approach but I get an HttpParseException saying
Databinding expressions are only
supported on objects that have a
DataBinding event.
This article: The CodeExpressionBuilder might be interesting/helpful (although written for ASP.NET 2.0).
It (seems) to enable you to write ... Text="<%$ Code: DateTime.Now %>" .... That might help, no? It is quite a bit of overhead, though.
Your code should look like this:
<asp:Label ID="lblMyStringConstant" runat="server" Text='<%# MyConstantsClass.MyStringConstant %&>'></asp:Label>
You also need to call DataBinding on that control, like this:
lblMyStringConstant.DataBind();
(It is not necessary if you are calling DataBind on entire Page or parent container of this label, because it will call DataBind for all its children)
<asp:Label ID="lbl" Text="<%# SomeText %>" runat="server" />
Then call lbl.DataBind(); or databind some container of the label.
If you have it like this it should work actually:
public static class MyConstantsClass
{
public static string MyStringConstant = "Hello World!";
}
or alternatively
public class MyConstantsClass
{
public const string MyStringConstant = "Hello World!";
}
If you declare it like
<asp:Label ID="Label1" runat="server" Text="<%= MyNamespace.MyConstantsClass.MyStringConstant %>"></asp:Label>
it won't work and the output will be "<%= MyNamespace.MyConstantsClass.MyStringConstant %>".
What you could do alternatively is to write it like this:
<asp:Label ID="lblTest" runat="server"><%= MyNamespace.MyConstantsClass.MyStringConstant %></asp:Label>
This works perfectly for me, but note you have to provide the fully qualified namespace to your class in the ASPX definition. At least otherwise it didn't work for me.

Categories