C # Localized Windows forms

Source: Internet
Author: User

Walkthrough: Localizing a Windows form

The Visual Studio project system provides considerable support for localizing Windows forms applications. Here are two ways to generate resource files using the Visual Studio development environment:

    • Enables the project system to generate resource files for localizable user interface elements, such as text and images on a form. The resource file is then generated into the satellite assembly.

    • Add a resource file template and then use the XML Designer to edit the template. One reason to take the latter approach is to generate localizable strings that appear in dialog boxes and error messages. Then, you must write code to access these resources.

This walkthrough topic demonstrates both of these procedures in a Windows application project.

You can also convert a text file to a resource file; For more information, see Resource and Resource File Generator (Resgen.exe) in the text file format.

Make Visual Studio Generate resource files
  1. Create a new Windows application named "WindowsApplication1". For more information, see How to: Create a Windows Application project.

  2. In the Properties window, set the localizable property of the form to true.

    The Language property is already set to (default).

  3. Drag the button control from the Windows Forms tab of the Toolbox onto the form, and then set its Text property to Helloworld.

  4. Set the Language property of the form to German (Germany).

  5. Set the button's Text property to Hallo Welt.

  6. Set the Language property of the form to French (France).

  7. Set the button's Text property to Bonjour le Monde. If necessary, resize the button to accommodate a longer string.

  8. Save and build the solution.

  9. Click the Show All Files button in Solution Explorer.

    The resource file appears below Form1.vb, Form1.cs, or Form1.jsl. Form1.resx is the resource file for the default culture, which is generated into the main assembly. Form1.de-de.resx is a resource document of German spoken in Germany. Form1.fr-fr.resx is a resource file for French speaking in France.

    Additionally, you will see a file named Form1.de.resx and Form1.fr.resx. Visual Studio automatically creates these files to resolve a limitation in visual SourceSafe that visual SourceSafe has to handle actions to add new files to the project during the save operation.. resx The file is empty and does not contain any resources.

  10. Press the F5 key or choose Start from the Debug menu.

    You will now see a greeting in English, French, or German in the dialog box, depending on the UI language of the operating system.

    Attention

    The user interface language used in Windows is a feature of the "CurrentUICulture" setting. If your copy of Windows is equipped with a Multilingual User Interface Pack (MUI), you can change the UI language in Control Panel. For more information, see Windows Server 2003, Windows XP & Windows MUI site. If MUI is not installed, you can change the current UI culture programmatically, as described below.

    The following procedure shows how to set the user interface culture so that the application displays French resources. In a real-world application, the user interface culture is not hardcoded in this way. The settings for the user interface culture will depend on user settings or application settings.

Set the user interface culture to view specific resources
  1. In the Code Editor, add the following code to the Form1 declaration at the beginning of the module:

    Copy Code
    ' Visual basicimports system.globalizationimports system.threading//c#using system.globalization;using system.threading;//Visual j#import System.globalization.*;import system.threading.*;
  2. Add the following code. In Visual Basic, you should use the New function before calling the InitializeComponent function. In Visual C # and Visual J #, you should use Form1and before you call the InitializeComponent function.

    Copy Code
    ' Visual Basic ' sets the UI culture to French (France). Thread.CurrentThread.CurrentUICulture = New CultureInfo ("fr-fr")//c#//sets the UI culture to French (France). Thread.CurrentThread.CurrentUICulture = new CultureInfo ("FR-FR");//Visual j#//sets the UI culture to French (France). System.Threading.Thread.get_CurrentThread (). Set_currentuiculture (New CultureInfo ("FR-FR"));
  3. Save and build the solution.

  4. Press the F5 key or choose Start from the Debug menu.

    The form will now always appear in French. If you have already changed the size of the button to accommodate a longer French string, be aware that the size of the button is also maintained in the French resource file.

Manually add resource files to your project and edit them
  1. On the Project menu, click Add New Item.

  2. In the Template box, select the assembly resource file template. In the Name box, type the file name "Winformstrings.resx". The Winformstrings.resx file will contain a fallback resource for English. These resources are accessed whenever the application cannot find a resource that is more appropriate for the user interface culture.

    The file is added to the project in Solution Explorer and opens automatically in the Data view in the XML designer.

  3. In the Data table pane, select Data.

  4. In the Data pane, click a blank row, enter strmessagein the Name column, and enter HelloWorld in the Value column.

    You do not need to specify the type or MIME type of a string; The type descriptor retains the data type of the saved object. If the object consists of binary data, the MIME type specifier retains the base type of the stored binary information (base64).

  5. On the File menu, click Save Winformstrings.resx.

  6. Perform step 1-5 two times to create two resource files named Winformstrings.de-de.resx and Winformstrings.fr-fr.resx, and these two files have the string resources specified in the following table. The Winformstrings.de-de.resx file will contain resources specific to German spoken in Germany. The Winformstrings.fr-fr.resx file will contain resources specific to French speaking in France.

    Resource File name name value

    Winformstrings.de-de.resx

    strmessage

    Hallo Welt

    Winformstrings.fr-fr.resx

    strmessage

    Bonjour le Monde

Access to manually added resources
  1. In the Code Editor, import the system.resources namespace at the beginning of the code module.

    Copy Code
    ' Visual Basicimports system.resources//c#using system.resources;//visual J#import system.resources.*;
  2. In Design view, double-click the button to display the code for its click event handler and add the following code. The ResourceManager constructor takes two parameters. The first parameter is the root name of the resource, which is the name of the resource file without the culture and. resx suffix. The second parameter is the main assembly.

    In this walkthrough, you do not declare any namespaces, so the first parameter of the ResourceManager constructor can take the form of a projectname.resourcefilerootname . However, in the actual application, you should set the DefaultNamespace property. In that case, you will need to declare the resource manager through the fully qualified root name of the resource file (including its namespace). For example, if the default namespace is MyCompany.MyApplication.MyComponent, the first parameter of the ResourceManager constructor can be a MyCompany.MyApplication.MyComponent.WinFormStrings.

    Copy Code
     ' Visual Basic ' Declare a Resource Manager instance. Dim Locrm as New ResourceManager ("Windowsapplication1.winformstrings", GetType (Form1). Assembly) ' Assign the string for the ' strmessage ' key to a message box. MessageBox.Show (locrm.getstring ("strmessage"))//c#//Declare a Resource Manager instance. ResourceManager Locrm = new ResourceManager ("Windowsapplication1.winformstrings", typeof (Form1). Assembly)//Assign The string for the ' strmessage ' key to a message box. MessageBox.Show (locrm.getstring ("strmessage"));//Visual j#//Declare a Resource Manager instance. ResourceManager Locrm = new ResourceManager ("Windowsapplication1.winformstrings", System.Type. GetType ("Windowsapplication1.form1"). get_assembly ());//Assign The string for the ' strmessage ' key to a message box. MessageBox.Show (locrm.getstring ("strmessage")); 
    Attention

    By default,ResourceManager objects are case-sensitive. If you want to perform a case-insensitive lookup (for example, to retrieve the same resource as "txtwelcome" and "txtwelcome"), you can set the IgnoreCase property of the resource manager to true. However, to improve performance, it is best to always specify the correct case for the resource name. If you perform a case-insensitive resource lookup, you may cause performance issues.

  3. Build and run the form. Click the button.

    The message box displays a string that is appropriate for the user interface culture setting, or if it cannot find a resource for the user interface culture, it displays a string from the backing resource.

C # localizing Windows forms (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.