2
votes

Background

I have an ASP.NET Web Forms app that I want to localize using .RESX files. I already know how to do this using .ASPX files. However, my application uses some .ASPX files... as well as some plain .HTML files.

(I am doing a lot of KnockoutJs, where the app retrieves the reusable HTML templates and injects them into the DOM as needed.)

I can't take the following approach in the HTML files, since the <%$ code would not be executed.

<%$ Resources:Main, WelcomeMessage %>

The Question

Is it possible to use C# code to process a plain HTML file against a RESX file to generate an HTML file that has been localized?

(I NEED A SOLUTION WITHOUT USING AN .ASPX FILE)

If this is possible, then I might be able to create a web service that will apply a RESX file to an HTML file and return the resulting HTML string.

1

1 Answers

1
votes

Answer

I think the answer.... is No.

There does not seem to be an easy way to do this with ASP.NET Web Forms. So instead I switched my project to use ASP.NET MVC.... where this problem is more readily solved.

In case you are interested... here's what I did in MVC:

I changed the HTML files to .cshtml files (which support syntax that works with .RESX files). I created a controller I call HtmlController with a GetHtml action. The GetHtml method takes a URL as a parameter, and renders the contents of the requested .cshtml file, which is returned as a PartialView.

Now right after my page loads, I can fire Ajax requests to retrieve some shared HTML templates by calling that GetHtml action on HtmlController. The HTML content of my requested .cshtml file is localized against my .RESX file and then returned as the result of my Ajax call. I can then bind it on the page.

This works particularly well for shared HTML templates that I reuse across various pages, but I still need to have them localized for the currently selected Culture.

In retrospect

I still feel moving to MVC was the right decision.

However..... I suppose after having switched to MVC.... it might have been simpler to just directly render partial views (for each of those shared templates) on each page that needs access to them.

Then I could have avoided making separate Ajax calls to pull this stuff down when the page loads.

[FacePalm]