Form resources usage for programmatic strings?

99封情书 提交于 2019-12-21 04:30:51

问题


Had a basic WinForm question: By default a resx file is created for every form or user control (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?


回答1:


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.




回答2:


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;
}



回答3:


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.



来源:https://stackoverflow.com/questions/1761340/form-resources-usage-for-programmatic-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!