ASP.NET localization translating HTML Content - asp.net

I am localizing an ASP.NET site using automatic feature , where it creates a local resource file and adds the meta keyword to asp.net controls. However I have a lot of HTML like below
<h2> Welcome to our page"</h2>
<li> Option one </li>
Is there a way to get these automatically translated using the automatic localize utility ?
I tried adding the runat="server" for these tags but to no avail.
Also instead of localizing page by page is there a way to localize bulk - it a directory or a site at one go
thanks

You need to use Localize control for static text - for example,
<h2>
<asp:Localize runat=server ID="WelcomeMessage"
Text="Welcome to our page" meta:resourcekey="WelcomeMessage" />
</h2>
Alternatively,
<h2>
<asp:Localize runat=server ID="WelcomeMessage"
Text="<%$ Resources:WebResources, WelcomeMessage %>" />
</h2>
You can also use syntax such as
<h2><%= Resources.WebResources.WelcomeMessage %></h2>
where Resources.WebResources is strongly typed resource class generated by Visual Studio resource generator. For across page resources, you can create global resources and then refer then using syntax as shown above (meta key will not work for global resources).
See MSDN for more information.

Related

Add custom markup to .NET Theme .skin file?

I am new to .NET Themes. I would like to display different static HTML content in various places using Themes of my Web Application.
My first attempt was to use the ASP Literal control as follows:
In .aspx page:
<asp:Literal runat="server" SkinID="HomeContentFooter"/>
and in my .skin file:
<asp:Literal runat="server" SkinID="HomeContentFooter">
<div>
<!-- I hoped to put some custom static markup for each theme here -->
</div>
</asp:Literal>
This produces the following error:
The control type 'System.Web.UI.WebControls.Literal' cannot be themed.
So is there another approach for this using themes?
Literal control is rendered as plain text on your html, that is, when you have <asp:Literal ID="id" runat="server">Hello World</asp:Literal> you will get just Hello World on client. Since skin files are used for setting properties of elements and literal does not have style related properties or any properties or tags at all, it makes sense that it cannot be themed.
You can use asp:Panel instead, you customize it in your skin file and it will be rendered as div.

What's the correct way to connect pages of my website?

I'm just starting web-developing now (I do have experience with C#). And I've seen a recommendation to use Web.sitemap to have a list of pages of my website. Does this help? Will I be able to use this instead of having links on my web pages somehow? And what about being accessible to Google so my website will be found by searchers – will this help?
The Web.sitemap is not primarily a control to be used for your main navigation; it's meant as a navigational overview for the user and is typically only needed in large sites. It doesn't affect your SEO in any way - Google is more interested in the quality of your content.
A site map (NOT a Web.sitemap) is a list of pages on your site, and used in conjunction with a robots.txt file, CAN help your SEO. You can use the following tool to generate a site map automatically (although you'll nearly always have to hand tweak it as well):
http://www.xml-sitemaps.com/
The normal way to build navigation for a small/medium site is to create a set of links as an unordered set (ul/li) and style them with CSS.
It is possible to use Web.Sitemap as navigation source, though probably not recommended. I've used Web.sitemap driven navigation for a couple of sites I've developed. However, it really only works well if you have a simple site with static content pages. Here's the code I used for example:
<asp:SiteMapDataSource ID="MainMenuDataSource" runat="server"
ShowStartingNode="false" StartingNodeOffset="0"
StartFromCurrentNode="false" />
<ul id="jsddm" style="padding-top:10px;">
<asp:Repeater ID="MainMenuRepeater" DataSourceID="MainMenuDataSource" runat="server">
<ItemTemplate>
<li><%#Container.DataItem.Title%>
<ul>
<asp:Repeater ID="MainMenuRepeater_2" DataSource='<%# Container.DataItem.ChildNodes %>' runat="server">
<ItemTemplate>
<%#Utilities.GetSecondaryMenuItem(Container.DataItem, False)%>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
Where Utilities.GetSecondaryMenuItem is just a helper function in our application that uses custom attributes from the sitemap nodes to control the display formatting of the secondary menu.
Links are a fundamental part of any website and there's no reason to avoid them.
A sitemap is just an XML file that defines the site structure, and it's used mainly by search engines to index sites. You can use them for other things too, like crumb trails for example, but they are not a substitute for hyperlinks.

ASP.net HTMLDecode not working for Resource entries

I have a ASP.net MVC project, which utilizes resource entries (.resx) through out the project.
Few the resources fed, have HTML in it
example: Hello <b>World!</b>
With paragraphs href and more. As the resources are stored in an XML, the entries are HTMLEncoded
i.e the above example looks like this
eg: Hello <b>World!</b>
Due to this, wherever the resources are displayed, the HTML formatting does not render, and instead the HTML is displayed as visible text.
I tried to use HttpUtility.HTMLDecode and Server.HTMLDecode, but both wont work.
What is wrong? Any other work around resources?
Both of the following work fine for me:
<%= Resource.MyResource %><br />
<asp:Label runat="server" Text="<%$ Resources:Resource, MyResource %>" /><br />
A Resource entry such as <b>Text</b> is displayed in bold by the browser.
Some Controls do automatic HTML encoding of their inputs. Could that be what's happening for you?

asp.net - Use ascx as a layout template

This is my layout template (ascx without code behind)
<%# Control Language="C#" AutoEventWireup="true" Inherits="ws.helpers.LayoutUC" %>
<div>blah blah blah</div>
<ws:Panel runat="server" ID="left"></ws:Panel>
<ws:Panel runat="server" ID="main"></ws:Panel>
<ws:Panel runat="server" ID="right"></ws:Panel>
Modules will be added into ws:Panel later.
I also allow my user create their own ascx file to custom their page layout. And because of this i do a string replace all dangerous part like script tag (runat="server"), all asp.net html tag, <%, <%#, <#.... from their custom.
Im not worry about XSS, so dont comment on it, and ask why?
I want know your thinking about this. Is is safe? Is it scalable? Is it standard or a bad way?
Have a look at the INaminingContainer Interface http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx.
<asp:YourControl>
<LeftColumn>
<asp:Literal ID="literal1" runat="server" Text="User created literal" />
</LeftColumn>
</asp:YourControl>
In the .ascx from the users, they register your control and insert asp.net code into properties. In the 'YourControl' class you create placeholders and insert the markup set to a specific property into these placeholders. (e.g. everything between <LeftColumn> and </LeftColumn> will the inserted into
<asp:Placeholder ID="PlaceholderLeftColumn" runat="server"/>
Edit: I summed some of the TemplateContainer issue up and posted it here: http://www.tomot.de/en-us/article/2/asp.net/how-to-create-an-asp.net-control-that-behaves-as-a-template-container-to-nest-content-via-markup
You are allowing user-uploaded content; this is inherently unsafe and there are whole books dedicated to best practices. Given that you are doing it anyway, as long as you make sure you scrub the input, is it scalable? You are allowing creation of user-uploaded files on your site. How many will there be? How many users? What about load-balancing? This solution will not scale for many users, files, or servers.
It sounds like you are trying to create a simple CMS. Why not use one that exists currently, or adopt parts of an open source solution?

ASP.NET: localize content with mixed HTML formatting

In my application I have paragraphs with mixed static text and HTML formatting and links. I'm looking for a good localization solution that keeps resources decoupled from markup. Let's say we have the following paragraph:
<p>Let's have a cup of coffee and get <b>energized</b>.</p>
Using the standard resx solution forces me to embed the HTML markup and the link destinations in the resx string. This is bad because it couples markup/CSS/app structure with resources.
The next best thing is to split the paragraph such that localized content never contains markup. In the above example I would have 4 para fragments:
1) "Let's have a" as plain text
2) "cup of coffee" as a link
3) "and get" as plain text
4) "energized" as bold text
The problem with this solution is that fragmentation makes maintenace of resources a complete nightmare plus it forces a certain order of the paragraph fragments which might not fit the grammar of all cultures. For instance, in the translated language the proper translation might be
<p>Let's get <b>energized</b> with a cup of coffee.</p>
I don't think I can quite get away with not embedding markup into resources and that might not be a huge deal. Using proper markup/CSS (span, div, id, class) I can create abstractions that would lessen the impact of coupling.
What do I do about the link URLs though? Thanks,
Stefan
Don't view the HTML as "formatting", but as structure, and save all of it to a resource data store (such as resx, or a database, or xml files or something). Then you can stop worrying about little bits of text inside a paragraph. Instead, you'll have some reference to a resource called "paragraph_energized_with_coffee" or something, per locale, and whatever software you use to edit the resources will determine what flexibility editors have in structuring the html inside each resource.
Do it like this:
<%= String.Format("Resources.MyResource.mydata","someURL") %>
and use this as your resource string:
<p>Let's have a cup of coffee and get <b>energized</b>.</p>
I was having the same problem. If I'm reading your question correctly this is what I was able to do to solve it:
In my resources, I added the following text:
<p>Let's get <b>energized</b> with a cup of coffee.</p>
And in my ASP.NET view (using a Razor view), I added the following:
#Html.Raw(String.Format(ShopSavvy.Retailers.Web.Resources.Home.Index.perksReachShoppersBody1, "someURL"))
Doing this gave me all the word-specific formatting I needed for the proper effect.
I'm on Rahuls side - I would consider the html/css of content...just that...content.
The main reason is because when you want to do bulk content updates to a website, you will just have to copy paste from provided html/css into the resx files. It's also a lot quicker to do the orginal templating from html/css into asp.net.
There are also some refractor tools that have "export to resx file" options when content is selected that may speed things up.
Thanks!
I came across this googling the exact same problem. It is intentional, since if you send the resources file to a translator he/she might not understand HTML and could damage your code. I don't like this either.
The default Visual Studio sample web-site contains:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>Welcome to ASP.NET!</h2>
<p>To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET
Website">www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
</asp:Content>
How would you localize these:
<h2>Welcome to ASP.NET!</h2>
<p>To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET
Website">www.asp.net</a>.</p>
<p>You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.</p>

Resources