views:

1279

answers:

3

Hi All, Had a basic WinForm question: By default a resx file is created for every form or usercontrol (along with the designer.cs). This resx works fine for all the controls and the text added to the controls via the UI.

I was wondering if I could use the same resx to add strings which have to be used programmatically and based on conditions, attached to the controls? Will the resx get overridden in any case and this custom strings be removed? What is the best practice to follow in this case?

Thanks for any inputs.

Warm Regards, Chandra

+1  A: 

There's a strange problem with the string resources in the Resources.resx file. There's no obvious way that I ever discovered how to create a new resource table for another language with the IDE. It can be done by hand though. Follow these steps:

  1. Project + Properties, Resource tab, add the strings you want to use in your program
  2. Start Windows Explorer and navigate to your project's Properties folder
  3. Copy and paste the Resources.resx file
  4. Rename it to the culture you want to use. For example: Resources.fr-FR.resx
  5. Back to VS, click the Show All Files icon in the Solution Explorer window
  6. Expand the Properties node, the new resource file should be visible
  7. Right-click it and select "Include in project"
  8. Select it, in the Properties window set Custom Tool = "ResXFileCodeGenerator"
  9. Verify that Build Action is set to "Embedded Resource"

Build your program. You should get a new folder in your project's bin\Debug directory with the satellite assembly, named projectname.resources.dll. This satellite assembly contains both the localized strings and any localized resource from the forms. Test that it works with code like this:

public Form1() {
  System.Threading.Thread.CurrentThread.CurrentUICulture =
    System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
  InitializeComponent();
  textBox1.Text = Properties.Resources.String1;
}
Hans Passant
On your form, set `Localizable` to `true`, then change the `Language` property.
Jon Seigel
+1  A: 

Or you can try this:

Add this code in main();

using System.Resources;

ResXResourceWriter rw = new ResXResourceWriter("Resources.de-DE.resx"); rw.AddResource("String1", "de"); rw.Close();

...repeat for more files

Go to the bin directory and move the XML (resx) file(s) to perhaps the property folder.

Go to Step 5-9 above in nobugz post.

pete
Pete, what is the nobugz post you are referring to?
Chandra Mohan
+1  A: 

The auto-generated ones get overwritten (I'm using 2005), so no I would not use the same file. I would suggest creating a separate area in your project for .resx files like this. Usually I create one .resx per form, matching the name, of course.

Edit: Here is a more detailed answer I gave recently to organize the file/folder structure when localizing your app with .resx files.

Jon Seigel