Link to Resource from a Resource file - asp.net

I have a global resource file: ContactDetails.resx. In here are addresses and contact information used around the website.
I also have a contact page listing all the contact details, but changing depending on language. So on the UK website, the UK contact details appear at the top, whereas on the French website the French details will appear at the top.
Is there a way to nest resources, referencing a resource value from within the value of a different resource?

If a resource value doesn't exist in your French resource file, but does in your Default resource file, the system will automatically get the value from the default resource file.
So if your localization is set to fr, and your call is something like:
Resources.ContactDetails.Telephone
And Telephone doesn't exist in ContactDetails.fr.resx then it will get the Telephone value from ContactDetails.resx
If all the values are to be displayed on the page, but in a different order depending on the localization I would recommend not storing these in a resource file. The resource file variations are for changes in language, not to change layout of data.
Instead I would recommend to store these in the database, and have 3 literal controls on the page like:
<asp:literal id="ltl1stTelephone" runat="server" />
<asp:literal id="ltl2ndTelephone" runat="server" />
<asp:literal id="ltl3rdTelephone" runat="server" />
Then depending on the localization, specify which values should be assigned to each control.
if (System.Threading.Thread.CurrentThread.CurrentUICulture = "fr"){
.........
}

Related

Localization of Data Pager control in Asp.Net

Is it possible to localize DataPager control in asp.net?
Among the fields NextPreviousPagerField can be localized since it offers properties such as FirstPageText and LastPageText. But I am having trouble localizing NumericPagerField. It basically produces the page numbers and I can't find any way of localizing those.
I have tried changing the culture of current thread, but it didn't work.
There are also properties PreviousPageText and NextPageText in NumericPagerField control that can be localized as follows (Assuming you are using Resources):
<asp:NumericPagerField
PreviousPageText="<%$Resources:TestSiteResources, PreviousPageText %>"
NextPageText="<%$Resources:TestSiteResources, NextPageText %>"
... />
Also note that:
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.numericpagerfield.nextpagetext#remarks
The value of this property is stored in view state. It can be saved
automatically to a resource file by using a designer tool. For more
information, see LocalizableAttribute and Globalization and
Localization.

Same piece of code works on one page & doesnt work on other Page, asp.net

nesting of image inside a LinkButton shows image on one page & doesn't show image on another page below are two sample code from two different pages in the same root director.It works fine on one page but on the other page is doesn't show any download image rather shows the text download in place of image.
I have done troubleshooting for sometime and replace the code also but it doesnt show download image for any reason on the second page..
<asp:LinkButton ID="lnkbtnDownload" runat="server" onclick="lnkbtnDownload_Click" meta:resourcekey="lnkbtnDownloadResource1">
<asp:Image ID="imgDownload" runat="server" ImageUrl="~/images/download.png" meta:resourcekey="imgDownloadResource1" />
</asp:LinkButton>
<asp:LinkButton ID="lnkbtnDownload" runat="server" onclick="lnkbtnDownload_Click" meta:resourcekey="lnkbtnDownloadResource1">
<asp:Image ID="imgDownload" runat="server" ImageUrl="~/images/download.png" meta:resourcekey="imgDownloadResource1" />
</asp:LinkButton>
HTML OUTPUT
HTML for above two code sample render as below
<img alt="Download" src="images/download.png" id="MainContent_imgDownload">
Download
Both Pages are in the same root directory...
The problem probably comes from a discrepancy in your resource files or missing a resource file completely for the second page. Obviously you have one for the first, but possibly not for the other which has different naming.
If you are using meta:resourcekey, there are some things you have to considerate.
Make sure that your local resource files meet the following criteria:
They are in an App_LocalResources folder.
The base name matches the page name.
For example, if you are working with the page named Default.aspx, the
resource files are named Default.aspx.resx (for the default
resources), Default.aspx.es.resx, Default.aspx.es-mx.resx, and so on.
The resources in the file use the naming convention
resourcekey."property". For example, key name Button1."Text".
Source: MSDN

How does a local resource file look like?

I can't seem to find a detailed example of how a local resource file looks like and its content
I have been reading up on this link
http://msdn.microsoft.com/en-us/library/ms227427.aspx
I want to use implicit localization with local resources.
So if I have a page: Products.aspx and want to create a default resource file then I need to create Products.aspx.resx and placed in App_LocalResources folder?
What does it contain say for example I want to change the values for a button control with ID="btnSubmit" ?
Is it like:
btnSubmit.Text = "Click here" ?
Yes you would place the "Products.aspx.resx" in the App_LocalResources.
You would then create an item in the resource file where:
Name = btnSubmit.text
Value = Click Here
your aspx page would have the button with the resource key defined:
<asp:Button runat="server" ID="btnSubmit" meta:resourcekey="btnSubmit" />

Globalization difference in inline tags in ASP.NET

Is there any advantage/downfalls between using the inline write tag instead of the resource tag? Example:
<%=Resources.Site.SampleString %>
The resources tag (expression tag) as seen in any MSDN example:
<asp:Literal id="Literal1" runat="server" text="<%$ Resources:Site, SampleString %>" />
I find the first option far easier to use, and it has IntelliSense, but maybe it won't function the same?
These methods will function exactly the same. The latter simply calls first one; there is a reason why strongly-typed resources access code is being generated in the background. Therefore you can use whatever method you please.
By the way, there is also another way - by using meta:resourcekey attribute. So you should be able to write:
<asp:Literal id="Literal1" runat="server"
meta:resourcekey="SampleString" text="Default one" />
and it all should work exactly the same.
EDIT on implicit Localization.
What I forgot to mention, is that with meta:resourcekey certain conditions have to be met. The conditions are:
Values are taken from App_LocalResources, therefore related resource file need to exist
Related resource file name must be pagename.resx, for example: Default.aspx.resx, Default.aspx.es.resx
The resource file must contain keys in form of resourcekey.propertyname, for example SampleString.Text, SampleString.ID (although I wouldn't localize control ID's)
Because of how resources are generated, the key mentioned above must exist in invariant resource file (Default.aspx.resx), or it won't be localized.
I realised after some time that the <%=Resources.Site.SampleString %> method does not have designer support (which is understandable). This doesn't bother me, but it is a difference (for future readers).
So if you need or want designer support, the second option would be necessary.

Explicit localization problem

when trying to translate the confirmation message to Norwegian i get the following error:
Cannot have more than one binding on property 'OnClientClick' on 'System.Web.UI.WebControls.LinkButton'. Ensure that this property is not bound through an implicit expression, for example, using meta:resourcekey.
i use Explicit localization in the following manner:
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="<%# Resources: lnkMarkInvoicedResource.OnClientClick%>"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
here's the local resource file entry:
<data name="lnkMarkInvoicedResource.OnClientClick" xml:space="preserve">
<value>return confirm('Er du sikker?');</value>
if i remove the meta attribute i get the English text(default).
how do i get the Norwegian text appearing without resorting to using the code behind?
Update:
removing the meta attribute prevents the exception from occurring but the original problem still exists. I can't get the Norwegian text to show.
only the default English text shows.
Another Update:
I know this question is getting old but i still can't get the Norwegian text to display.
If anyone has some tips please post a response.
Looks like you're making the problem harder by inlining the onclick. Why not split it out to a separate line?
<script>
function markInvoiced()
{
return confirm('<%= Resources.SomehowGet.lnkMarkInvoicedResource.OnClientClick%>');
}
</script>
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="return markInvoiced();"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
And while we're looking at your code, you realize that you're essentially building an <a> tag, right? As such, why not just build an <a> and save yourself some grief?
And finally, next project why not ditch the built-in ASP.NET localization nighmare in favor of something sane like FairlyLocal, in which case you'd write this:
<a href="#" onclick="return confirm(<%=_"really?"%>) onserverclick="lnkMarkInvoiced_OnClick" runat="server">
<%=_("Mark Invoice")%>
</a>
Are you using the .NET resource manager and satellite assemblies to store your localized resources? It looks like you have hard-coded the alternative language in your markup, rather than storing it in a language-specific resources assembly...
.NET has some extremely rich localization and globalization capabilities. If you use them properly, localization should be a pretty automatic thing (assuming your client is providing their language code as part of the HTTP headers). Even if your client has not configured their browser with the appropriate language, it is still easy enough to manually change the UI culture via a user request (clicking a flag icon, configuring a setting, etc.)
This article might be helpful: ASP.NET Web Page Resources Overview
That meta tag is using implicit localization when you're using explicit localization in the OnClientClick. You will need to choose one method or the other. If you are to use explicit localization, you'll need to do the necessary work to set the proper culture info in your application in the code-behind.

Resources