Multi-lingual static content in ASP.NET - asp.net

Is there a simple way of making the static content of an .aspx page multi-lingual? Resource files are nice for controls but it's quite hard and annoying to write big chunks of html in them. Any easier ways?

Make properties in resources files and use them, .NET automatically finds the correct resource file, also just make sure so that the property name is same in all the resource files, so it will replace the property value in .aspx page with the value in the resource file.
Don't code html markup in resource file, have the html markup in the .aspx page itself, just get the essential values from resource files.

Just to reiterate what Mahesh said: do NOT put markup in the resource files, just put the static content into them.
If you need to serve different views based on culture, consider doing something else. For example, if you're using MVC you could write a view engine that return the correct markup for each culture.

#ciscoheat what you say is correct, LOCALIZE control is the right thing to use for big chunk of data.

Related

Tagging HTML elements that contain content provided by the ASP resource provider

Currently I am working on an ASP.net webforms project where I would like to indicate to the user which elements on the current page contain localized resources.
In order to identify which HTML elements, contain which localized resources. I'd like to tag all rendered HTML elements that had a resource retrieved from the ResourceProvider while rendering the page. Thereby creating a mapping between HTML element and the used resource(s).
The tagging will be done by adding a data-localized attribute to the html element. The attribute will contain the resource identifier(s) used (i.e. resx file names and the resource keys), and then when the in the browser displays the page a javascript will use this information to do whatever.
So far I have replaced the default resource-provider with my own provider that retrieves resources from a database instead of the regular .resx files (as described in this article). This gives me information about all the resources used and by logging these in the HttpContext, i know all the resources used for the current page.
Now for the mapping, because the localized resource doesn't always have to occur within an HTML element (e.g. it could be just a string yielded by a Literal control). I have introduced a new CompositeControl that wraps a section containing localized resources with a <div data-localized=""> </div>.
My question: How can i map the wrapper with the resources used by its children?
Since you are going to post-process the data with JavaScript, you may think of adding what you need in form of prefix and delimiter to actual translated text, for example "Sample text" will become "##strings.resx##login.form.sample.text##Sample text".
Your client side script would then look for ## delimiters and remove them (or do whatever you want) from the page.
I know it is not what you asked for but I believe it is simpler and easier to implement.

Shorter names for /ScriptResource.axd and/or /WebResource.axd in ASP.NET?

My page loads scripts and css from some custom assembly. For now, I use the ScriptManager to add scripts to the page (its CompositeScript) and the page's ClientScript.GetWebResourceUrl() for css files. So, on the page(s) my scripts are referenced by /ScriptResource.axd and css are referenced by /WebResource.axd. Works fine except the fact that those URLs look really ugly. Is there any manageable way to replace those urls with my own fake paths without moving/hosting script/css physical files somewhere? Any help or ideas would be greatly appreciated.
Use a Filter to find all the WebResource.axd and ScriptResource.axd src'es and replace them with something you like better. You then need a IHttpHandler, as Brian comments, that understands your new url-format and returns the same content as the original axd-files would have.
You should also look into combining all those script and css resources to save requests from the browser. How do I combine WebResource.axd and ScriptResource.axd files so as to result in less requests to my ASP.NET server?

How to display XSL-transformed XML in ASP.NET page?

So far all the XML / XSLT I've worked with takes an XML document and transforms it to a standalone HTML webpage using an XSLT file.
In my web application, I'm using a web service to retrieve the XML document, which I need to render and make human-readable, and then insert that formatted content into a content placeholder in my master page.
The easiest way would be to append the XSLT to the retrieved XML file and link that to the content placeholder, but something tells me I can't just do that.
I took a look at these Stack Overflow pages, but they just want to render the straight XML whereas I want a transformed XML. Also, I need to be able to put it into my master page template.
This article shows how:
http://www.codeproject.com/Articles/37868/Beginners-Introduction-To-XSL-Transform-Rendering-XML-Data-using-XSL-Get-HTML-output.aspx
even if the spelling is as bad as mine...
Added
And here's another link that shows how, perhaps a bit more simply
http://www.aspfree.com/c/a/XML/Applying-XSLT-to-XML-Using-ASP.NET/2/

What is the best practice for using ASP.NET MVC to render lots of html or text files?

I have a lot of html pages, but I don't know how to display them through the asp.net mvc view.
I buid a view as my template and use asp.net mvc to insert html into the template and then render it.
But the question is that I must use FileStream to read the raw html-based files into memroy and then put it into view template, like ViewData["content"] = ???.
I just want to know if there are some other better ways to render static html files to the browser.
Did i describe the question clearly?
I guess you could do something like this:
using(var file = new StreamReader(htmlFileName))
{
return Content(file.ReadToEnd());
}
Note that the mime type automatically defaults to text/html, but you could optionally specify which mime type headers should be sent by supplying the type as an additional argument to the Content method.
I guess you also can point a iframe element from HTML to the target file url directly.
Alternatively you could write your own ActionResult that writes the contents of the file to Response.Output (could potentially avoid loading the entire file into memory at once albeit it might not be a big issue).

ASP.NET MVC: How to use static HTML pages in MVC applications?

In the app I am working on, I want to allow the user to upload static HTML pages to replace the default "user profile" MVC View page. Is this possible? That is, the user uploaded html pages will totally run out of MVC, and it can include its own CSS links, etc.
Ideas? Suggestions?
Obviously the .net MVC framework handles static content already for images / css / js etc. It would just be a matter of extending that (routing?) to pass .html files through straight to IIS. That coupled with a dash of rewriting to make prettier urls should do the trick.
However, I would be very, very wary of allowing User Generated Content in the form of raw HTML uploads as you're leaving a very very wide door open. At best, you're going to wind up with people's pages full of spam/porn/adverts. At the worst, you'll be providing a gateway for people to upload cross-site scripting hacks and potentially uploading malicious content to damage your site. The could easily take an existing form on your site, hardcode a load of junk into it, and exectute it from their homepage and break a whole heap of things.
At the very least you should be parsing the uploaded content to reduce it down to just a block of content, and then wrapping that in your own etc. I would personally be much more inclined to just provide users with a nice WYSIWYG editor to edit a single block of content - any editor worth it's salt should provide you with sanitisation as to what elements it includes / excludes. Then store this content fragment in your database / on disc and have the request for a homepage go through a standard MVC controller route and load up that content.
Edit - for you request for examples
You should be able to add an Ignore rule to your routing - there will probably already be examples of these already - crack open your Global.asax file - you will want to put in a call to the routes.IgnoreRoute method :
routes.IgnoreRoute("UserPages/{*path}");
Should let IIS handle all requests for yourwebsite.com/UserPages/aUser/homepage.html - you can also play about a bit more with the wild card fragments / constraints for prettier solutions
I suggest you to make your custom ViewEngine that will allow to use static html markup with custom tags in it, that will be replaced by user info.
So, your view engine may accept something like that:
<html>
<body>
<b><user:FirstName /></b>
<b><user:LastName /></b>
</body>
</html>
and produce
<html>
<body>
<b>First Name</b>
<b>Last Name</b>
</body>
</html>
This custom markup you can store in database, for example.
You can take a look at custom ViewEngine implementations at MVC Contrib project.

Resources