I have 2 links, one english one spanish. is there anyway i can localize that in .net? I was thinking of using sitemap
Sure, you can use resources for that, either global or local.
For local resource (pertinent to that .aspx page), switch to design mode, click Tools -> Generate Local Resource.
This will create the "meta:resoucekey" bits for you for all of your server side controls and also will generate the resource file under a "App_LocalResources folder". Copy that resource file and rename it to the spanish that you want to use.
For example If your file name was mypage.aspx, the local resource generated would be mypage.aspx.resx and your new file will be mypage.aspx.es.resx (this will use traditional spanish). Markup below:
<asp:HyperLink runat="server" ID="myLink" Text="The Link" meta:resourcekey="myLink" />
In the local resource files, make sure the key matches the id: keys should be "myLink.NavigateURL" and the values should be your link
If you go the global resource route, then just add new resource file to the App_GlobalResources folder in the solution, name it whatever you want and make a copy of it for the other language. For example the new global resource is MyGlobalResources.resx. and the spanish resource MyGlobalResources.es.resx. Now put the following in your markup:
<asp:HyperLink runat="server" ID="myLink" NavigateURL="<%$ Resources:MyGlobalResources, myLink %>" Text="The Link" />
In the global resource files, the key is whatever your want. Just make sure it matches what you put in the markup: in this case the key should be "myLink" and the value should be your url.
Now, when you switch languages, the correct resource file should pick up and the correct URL should be shown.
P.S. You can localize lots of properties from the server side controls. I hard coded the "Text" property just for simplicity.
Here are a few links that go over localization:
http://msdn.microsoft.com/en-us/library/ms227427.aspx
http://www.west-wind.com/presentations/wwDbResourceProvider/introtolocalization.aspx
http://msdn.microsoft.com/en-us/magazine/cc163566.aspx
Hope I was clear enough :)
EDIT
I don't know how I completely missed the "sitemap" factor. The answer is yes and no. In a single site map you can only localize the Title,Description and custom attributes, not the URL. However, you can create a different sitemap for each locale and add them to the web.config file. Here is a link that explains how to do that (bottom of the page): Localize SiteMap
-D
Related
I am interested in using only Global resource files in the "App_GlobalResources" folder and not having separate files in the "App_LocalResources" for web form labels.
I like the idea of centralizing all the resource strings in one place.
My resx files in App_GlobalResources have the following properties:
Build Action: Embedded Resource
Copy to Output Directory: Do not copy
Custom Tool: PublicResXFileCodeGenerator
Custom Tool Namespace: Resources
This works fine when calling a variable in markup such as:
<p><%=MyStrings.This_is_a_test_string%></p>
It also works fine in the code behind:
Dim strTest As String = MyStrings.This_is_a_test_string
The problem occurs when I attempt to bind a global resource string to a web form control such as:
<asp:Label ID="lblFullName" runat="server" Text="<%$ Resources:MyStrings, This_is_a_test_string %>" AssociatedControlID="txtFullName"></asp:Label>
The web server displays this error:
The resource object with key 'This_is_a_test_string' was not found.
I did notice if I change "Build Action" to "Content" that the resx will correctly bind to the Label control.
However, the problem is the strings will no longer work in the markup or the codebehind any longer.
So, is there a way to use ONE global resource file for the markup, codebehind, and for web form Label controls?
Or, am I forced to use two separate resource files?
At this point I'll admit that I'm thoroughly confused.
I wasn't able to figure out how to use a global .resx file that I could both use explicitly in the code behind such as:
Dim strTest As String = MyStrings.TestString
and also bind to a webform control such as:
<asp:Label ID="lblFullName" runat="server" Text="<%$ Resources:MyStrings, TestString %>" AssociatedControlID="txtFullName"></asp:Label>
Therefore, I decided the simplest solution was to use the global resx files I had and instead of binding them to web form controls I simply set the web form controls explicitly using their ID in the codebehind like so:
lblFullName.Text = MyStrings.FullName
What I did was put a placeholder in the actual control text using { } so I could see if I forgot to set any of the text such as:
<asp:Label ID="lblFullName" runat="server" Text="{THIS IS A TEST STRING}" AssociatedControlID="txtFullName"></asp:Label>
I have some text which is loaded from a resource file. Ordinarily, to get dynamic content I would use:
string.Format(GetLocalResourceObject("SomeText"), PhoneNumberAsString)
I want to do the same with a link, only the link needs to be application relative as I have URLs like mysite.com/page.aspx and mysite.com/fr/page.aspx.
I normally use an <asp:HyperLink /> tag to create the links as I can then just put a squiggle at the start NavigateUrl="~/page.aspx". However, I don't know of a way to get a dynamic HyperLink to appear as a string without adding it as a control to something.
Simply writing ToString() outputs System.Web.UI.WebControls.HyperLink..
How do I get a link out of a resource file and make it into a hyperlink using ASP.NET Webforms?
UPDATE
With some help from the answers I now have the following code on my page:
<p><%= string.Format(GetGlobalResourceObject("Resource", "MoreThan1000Users").ToString(), ResolveUrl("~/contact-us.aspx")) %></p>
and in my resource file I have:
If you would like more than 1000 users please call our sales team.
Does this seem like good practice or is there another way to achieve what I'm doing? I don't know if I should be happy or not that there is HTML inside the resource file.
Since you haven't posted code, I'm guessing somewhere you have a HyperLink WebControl object that you're hitting ToString() on. If that's the case, you can access the URL associated with it using its myHyperLinkControl.NavigateUrl property.
If you're storing the link in your resource with a squiggle/tilde (which is good) then you can replace the squiggle with your application location. If you have a control/page, then you can easily call its ResolveURL method (which takes the tilde and automatically replaces it) There's some existing solutions if you don't have a control/page reference with your context, then there's some discussion to do that here: ResolveUrl without an ASP.NET Page
I guess this is what you want:
Server.MapPath("~/page.aspx")
That will work inside your aspx and your code-behind.
I have a website that is working fine with Razor (C#) all the coding is working properly when I use my local testing (WebMatrix IIS).
When I put it "online" on my server the website is not at the root of the site it self
For example:
http:// intranet.mycompany.com/inform
That's basically the "root" of my folder structure so all my folders starts from there (css file default.cshtml... and so on)
My "_PageStart.cshtml" sees it properly cause when I access my site from the link http://intranet.mycompany.com/inform it gives me the Layout I have configured in _PageStart.cshtml (and it really show the layout + the rendered default.cshtml)
BUT nothing else is getting the proper path, for example :
<img src="~/images/logos/hdr.png" />
The img holder is there I can see it but shows that the link is broken... when I Right-Click the img holder and do properties to see where the files should be it shows me :
http:// intranet.mycompany.com/images/logos/hdr.png
So it's going to the "full" root not the relative root...
How can i fix that ?
You have to use relative paths all over your app:
~ won't work within static html code.
You can write
<img src="#Url.Content("~/images/logos/hdr.png")" />
or
<img src="../images/logos/hdr.png" />
The first approach is good for layout files where your relative path might be changing when you have different length routing urls.
EDIT
Regarding to your question about normal links:
When linking to another page in your app you don't specify the view file as the target but the action which renders a view as the result. For that you use the HtmlHelper ActionLink:
#Html.ActionLink("Linktext", "YourController", "YourAction")
That generates the right url for you automatically:
Linktext
EDIT 2
Ok, no MVC - so you have to generate your links yourself.
You have to use relative paths, too. Don't start any link with the / character!
Link
Link
Link
EDIT 3
When using Layout pages you can use the Hrefextension method to generate a relative url:
<link href="#Href("~/style.css")" ...
Use Url.Content as shown bellow:
<img src="#Url.Content("~/images/logos/hdr.png")" />
I know that '~' is added by default, but I tend to change it so that all paths are relative to my code file rather than application root, using ".." eg. "../images/logos" etc
I was recently assigned a task of changing our asp.net web site localization to use custom resource provider (using sql database) instead of the default asrx resource files. Right now I'm chalenged with replacing hundreds of meta:resourcekey="resource-key" with '<%$ Resources:[filename,]resource-key %>' in our web site too many web pages. I want to do it programmatically.
first of all I'm not able to open .aspx files using XmlDocument, then I wonder how can I read meta:resource entries inside the aspx file as meta:resource is not any regular node attribute. any thoughts or example code how to solve this.
Thx.
Note: in the inserted '<%$ Resources:[filename,]resource-key %>' filename name sould be based on the aspx file name & resource-key on the control type and the resource value.
exemple: in UserPage.aspx page <asp:Label id="uid" meta:resource="userName"> should be replaced with <asp:Label id="uid" Text='<%$ Resources:UserPage,LBL_userName_text %>'.
Html is not valid xml, so no wonder an XmlDocument didn't work. Especially with that <%$ .. %> syntax.
Why not read it as plain text and search for the string "meta:resourcekey"?
This isn't a programming answer, but a utility like PowerGREP may be a viable solution.
I just started using explicit resource files. I performed these steps:
In the root create the folder: App_GlobalResources
Add two resx files: LocalizedText.en-us.resx and LocalizedText.resx
In both files I have a value called 'InstitutionTitle'
In LocalizedText.en-us.resx the value is 'Institution' and in the LocalizedText.resx the value is 'Instelling'
In my .aspx file I have the following label:
<asp:Label ID="lblInstitution" runat="server" Text="<%$ Resources:LocalizedText, InstitutionTitle %>" />
When I run this page, I always get the dutch version. Whether I set the language in my browser (FF and IE7) or not, I always get the dutch version. When I request the browsers' language I get en-us (using: Response.Write(Request.Headers["Accept-Language"]);).
What's the issue and how can I fix it?
Setting the language preferences in the browser is not enough. You have to make sure that the current thread's Culture and UICulture properties are set accordingly in ASP.NET.
You can do this either programmatically or declaratively on your page (Culture and UICulture attributes of the <%#Page %> directive).
Or you can let ASP.NET set them automatically by setting the web.config entry shown below and setting the Culture/UICulture properties of the page/masterpage to "auto".
// web.config:
<globalization enableClientBasedCulture="true" ...>
// page/masterpage:
<%# Page ... Culture="auto" UICulture="auto" %>
Check this page for details.
#martijn:
check the browsers caching settings.
Caching should be off all the time
when developing.
Install Firebug (FF) and Fiddler(IE)
to see what's being transfered over
the wire.
Hope this helps...
signed,
A fellow countryman