0

I have a task of translating a big WebForms App (some 300 aspx pages). I will use conventional approach on translating ASP.NET apps (using Resource files) and I will use ZetaResource to make my life easy. However, I have a small issue: What can I do with free text existent in aspx code? Take this code for instance, how can I convert the text This is a List of records available in the Database into an ASP:Label server control in order to use the embedded Translation Mechanism?

<form id="form1" runat="server">
<div>
    <br />
  This is a List of records available in the Database<br />    
    <br />
    <asp:Button ID="cmdButton" runat="server" Text="Button" meta:resourcekey="cmdButtonResource1" />
</div>
</form>
Enovator
  • 13
  • 5

2 Answers2

1

Imagine you have a resource file named YourResource and the item you want is String1, you can just do this for free text:

<%= (YourResource.String1) %>
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Thanks for the reply, I'm actually looking for a way of converting the free text in the aspx files into Server Controls. I understand what you are saying but there are many files (around 3-400 aspx files) and I would like to do the task as automatic as possible! Doing them manually will be a nightmare! – Enovator Oct 09 '17 at 18:57
  • @Enovator I see. You would need to find the free text in each doc and then take the free text and add it to a resource file and replace the free text with the resource key as I have showed in my answer. You can use [HtmlAgilityPack](https://www.nuget.org/packages/HtmlAgilityPack/) to do the parsing. This will not be easy but it is also not really complicated. Make sure to backup everything before you do this so you can revert if you have issues. – CodingYoshi Oct 09 '17 at 19:24
1

You have two choices for localization: explicit and implicit.

For explicit localization, you would put the text into a resource file and display it on the web page with code like the following:

<asp:Localize ID="Localize1" Runat="server" Text="<%$ Resources:LocalizedText, ListOfRecordsInDatabase%>" />

For implicit localization, the code would look like more like this:

<asp:Localize ID="Localize1" Runat="server" meta:resourcekey="LocalizeResource1" Text="This is a List of records available in the Database" />

For these one-off texts that exist only in a single page, I usually use implicit localization. For text that can exist on many pages (common labels, buttons, etc.), I use explicit.

The implicit localization resource files can even be auto-generated for you, once you put them into a server control, like <asp:Localize> or <asp:Label>.

There is a great walkthrough given on the MSDN site called, "Walkthrough: Using Resources for Localization with ASP.NET". It will tell you how to auto-generate the resource files.

Rob Johnston
  • 859
  • 8
  • 21
  • Thanks, a very good and documented answer. My question is basically ... is there a way of transforming the free text in the aspx into a server control in a more automated way? I do not want to do it manually. In my sample code I would like to run a script, batch, etc which will transform the free text into a Label or Localize tag. I have many files to parse and it's hard to to this manually, file by file! – Enovator Oct 09 '17 at 19:09
  • I know of nothing... but if the text follows a pattern, like it's always between two `
    ` tags, then perhaps you could use the regular expression find-and-replace functionality build into Visual Studio.
    – Rob Johnston Oct 09 '17 at 19:15
  • There are threads on extracting free text with regular expressions, like this one: https://stackoverflow.com/questions/7167279/regex-select-all-text-between-tags , but they say that I would be better with a tool like BeautifulSoup https://www.crummy.com/software/BeautifulSoup/ – Enovator Oct 10 '17 at 05:23
  • @enovator This is an old question but you can create a routine using Html Agility Pack https://html-agility-pack.net/ to parse offline files and edit as you see fit. Note: HAP can use Linq or XPATH to traverse the document nodes and update. – mark gamache May 12 '20 at 14:39