Question

I need to make a legacy program (its huge) support another language. Despite how badly I'd love to send them my single language project files and ask them to do the rest of the work, the translation agencies tend to put up a fight when I do that.

SO I am looking to generate a list of all UI controls in a project and their default text that is currently displayed. I'll then send that out to the translator and I'll generate a .resx file with the results. As far as I understand, as long as I name the resx files correctly for the language i am targeting and the localizable able property is set to true, once the culture changes it will switch over all of the text on my controls. Let me know if i am off on this, I spend my time in the embedded world and for some reason the higher ups decided I should be the one to tackle this.

I figure i can parse through the designer file to get the list, but I was hoping someone has a more elegant solution or knows of one that someone has already built. This seems like it would be a pretty common task.

Was it helpful?

Solution

Well, since I don't know some of the really important parameteres I can't be sure if e.g. going for a commercial tool is an option; also I don't know how many forms you have or how many forms and controls are created dynamically..

But for a simple control dumping function maybe something like this is a start:

private void dumpControls(Control ctl, string parents)
{
  if (parents == "") parents = ctl.Name; else parents += "." + ctl.Name;
  if (ctl.Name != "" && ctl.Text != "") Console.WriteLine(parents  + "=" + ctl.Text);
  foreach (Control ct in ctl.Controls) dumpControls(ct, parents);
}

You would put this into a form's constructor:

public Form1()
{
  InitializeComponent();
  if (dumpMode) dumpControls(this, "");
  ..
}

If you don't want the full parent container list you can easily modify the code, for example like this:

 if (parents == "") parents = this.Name + "." + ctl.Name; else parents += "." + ctl.Name;
 if (ctl.Name != "" && ctl.Text != "") Console.WriteLine(parents  + "=" + ctl.Text);
 foreach (Control ct in ctl.Controls) dumpControls(ct, "")

You probably will want the form prepended to make the control refences unique across forms.

Obviously you would not only have to add this to each form, but also open each form. I don't know if that will be a problem, but I'm sure the real work will be going through your code and changing all hard coded literals that need translation to something else. I've been there and done that and used a dictionary; worked well but was quite a lot of work and also amazing, as I had no idea just how many literals has sneaked into my (own) code..

I load the strings on form load or when the user changes languages. In addition to the built-in resources; I can also read an external language file into the dictionary..

Instead of dumping the data to the Console you can stuff it into a StringBuilder and write that to a file, of course..

OTHER TIPS

You can get the controls which is in your form using this code:

foreach (Control control in this.Controls)
{
    MessageBox.Show(control.Name + "\n" + control.Text);
}

But it shows only controls which is on your form. If you have a groupbox, you should use same code your groupbox too. Or if you have tabcontrol, you should this do separately for it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top