Start your first multilingual version of the dotnet application (WinForm article)

Source: Internet
Author: User
Tags dotnet resource thread versions
The program's multilingual version of the application was once a hurdle, and the past means it can face more users. A localized version of a business process could mean opening up a larger market. Chinese version has also been our hearts about Chinese software, a complex and expectations. I hope dotnet can bring us some different views and changes, let us look at each of our own software applications. Here I avoid two other topics: localizing and globalization, one of which is a big topic, and the other is that they have become an important part of the software industry, as well as a set of professional processes and practices.

The resource in dotnet is a whole new change, and perhaps it is the change that makes the multilingual version of the application simple and feasible. However, there are still some bugs in the current vs.net Beta2, and sometimes there are some inexplicable mistakes, and I believe the next version of the Vs.net IDE will be more stable in this respect.

Well, let's start with the simplest of places.



1. Prepare a simple prototyping application.

Here will be the simplest prototype for an application, without much logic and instructions, to build a Windows Application project in Vs.net, drag some controls onto a form, and then create a main menu for the form. Because I am using the English version of the vs.net so the current prototype is in English.



2. Add additional resource required to the application



This step, I add a picture resource to an external resource file, this picture will be displayed in a PictureBox, actually directly at design time set the control image can be, but I would like to assume that this picture in different language versions of different pictures, Of course, in this resource file I also added a string resource to display the usage.

To be honest, now this version of the vs.net has no visual resource tools, actually the change and display of resources in the XML Designer view, do some pure character resources can also, if the image resources I would prefer to use the SDK in the Reseditort, ( But in advance you need to compile it into an EXE before you can use it, which is typically in the \samples\tutorials\resourcesandlocalization directory under your Framework SDK installation directory. Specifically, you can do this in vs.net: Select Project->add New item->templates->assembly Resource File, First named MyResource.resx. The default Build Action property for this file is embeded Resource (the accompanying zip contains a picture of Il dasm that can be seen), saved, Start Reseditort Add a picture and a string resource, and then save the close ResEditor. Here's how to use the resource issues in this external resource file in your program. You can use the following code to do this:

Dim Res as System.Resources.ResourceManager

Res = New Resources.resourcemanager ("Englishandchinese.myresource", Me.GetType (). Assembly)



STRMSG = res.getstring ("Msghello")



Me.picBox.Image = CType (Res.getobject ("Dotnetlogo"), System.Drawing.Image)

The code above demonstrates the code you need to get resources and use picture resources and character resources. EnglishAndChinese.MyResource:MyResource is the name of the external resource file we just created, Englishandchinese is the name of our application or project.

Res.getstring ("Msghello"): Msghello is the name item in the external resource, thus obtaining a string resource.

GetObject ("Dotnetlogo") demonstrates that we will get a picture resource with name Dotnetlogo in the external resource.

Now that the F5 is running, we can see the picture in the GetObject ("Dotnetlogo") in the PictureBox, and the string of MessageBox ("Res.getstring") will be displayed in the pop-up Msghello box after pressing a button.



3. Turn our application into a different language version.

There are two steps: one is to convert the UI shown in the simple winfrom of the prototype into another language version, and the other is to convert the myresouce we just joined into another language version. For the first step, we can click the entire form, select the other language versions (chinese-simplified) in the form's Language property, and then save the Locatlizable property by selecting True, and then saving the menu on the form, The text of the control is changed from English to Chinese font. Then save the F5 you will see the Chinese version of the application displayed. You can also set the form language to (default), Locatlizable set to False, and then you can see the original English version of the interface, then F5, you will see the English version of the application. The current vs.net Beta2 is unstable when it comes to displaying design views in both English and Chinese, sometimes when a control on a form suddenly fails, or the form designer can no longer display the design form properly, in short, it is not very stable.

The second step is to MyResource also become a multilingual version; still choose Project->add New item->templates->assembly Resource File But this time name is: Myresource.zh-chs.resx, same as in the second step in the resource file, add new resources, but the resources in the name must be the same as the English language, the string is called Msghello, picture resources called Dotnetlogo, OK to save the disk. All right, the preparations are almost complete.



4. Some of the corresponding settings and tests.



Here are some of the code-related things, first in the form to write two basic small functions:

Private Function my_createresouce () as Integer

' Work on your own resource files

Dim Res as System.Resources.ResourceManager

Res = New Resources.resourcemanager ("Englishandchinese.myresource", Me.GetType (). Assembly)



STRMSG = res.getstring ("Msghello")



Me.picBox.Image = CType (Res.getobject ("Dotnetlogo"), System.Drawing.Image)



End Function



Private Function initregistry () as String



Dim Akey as RegistryKey

Dim Strcurrlanguage as String



Akey = Registry.CurrentUser.CreateSubKey (Registrykeyforccboy)



If akey.getvalue ("Language") is nothing Then

Strcurrlanguage = "en-US"

Akey.setvalue ("Language", Strcurrlanguage)

Else

Strcurrlanguage = Akey.getvalue ("Language")

End If



Initregistry = Strcurrlanguage



End Function



Then add the following code to the form's new () function

Public Sub New ()

MyBase.New ()



' This are required by the Windows Form Designer.

Dim Tmpculture as String

Struiculture = Thread.CurrentThread.CurrentUICulture.DisplayName



Tmpculture = Initregistry ()



Thread.CurrentThread.CurrentUICulture = New CultureInfo (tmpculture)



InitializeComponent ()



' Add any initialization on the InitializeComponent () call



End Sub



Don't forget to join the following namespaces:

Imports System.Globalization

Imports System.Threading

Imports Microsoft.Win32



The code above completes the selection of the original language version of the application and stores this information in the registry. Finally, use the two menu items in the main Menu menu to set the selected feature for the language version.

Private Sub Menuenglish_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Menuitem4.click

' 中文版

Dim Akey as RegistryKey

Akey = Registry.CurrentUser.OpenSubKey (Registrykeyforccboy, True)

Akey.setvalue ("Language", "en-us")



End Sub



Private Sub Menuchinese_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Menuitem5.click

' Chinese

Dim Akey as RegistryKey

Akey = Registry.CurrentUser.OpenSubKey (Registrykeyforccboy, True)

Akey.setvalue ("Language", "Zh-chs")



End Sub



Save your Project,f5 run. Then select the Chinese or English in the menu to quit before starting. The following is a screen copy of the operation:








Add:

The current vs.net Beta2 about multiple languages in the IDE is sometimes problematic, in practice you can do this, first complete an English version of the application, and then use the BETA2 The SDK tool ResGen converts form1.resx to form1.resources files (ResGen form1.resx form1.resources) and then uses the BETA2 SDK new band WinRes.exe To open the converted Form1.resources file, you can see the same form as the vs.net (some of the examples above are inconsistent, such as the menu is not available), and then modify the font of the UI and the language you want, and choose Save As. In the pop-up dialog box, select your corresponding language. Dotnet allows you to add a different language version after completing the entire application, so long as you place your language resource DLL according to the DOTNET specification, your program can theoretically become a version of any language. The resource file added to the second step above is actually a statelite Assembly cannot be shared in the GAC without strong name, so it can only be placed in the directory specified by the Dotnet resource specification. However, it is similar to the multilingual approach, you can use the above method to convert the ResX file, you can create a new resource file to do. At present, there are pros and cons to using Vs.net and manual methods, the overall point: the current dotnet for multilingual version of a very common and normative approach, this is very different from the original. And Beta2 is still not very stable in this respect (haha)








All right, here we go. Thank you for taking the time to read this article and hope it will help you. In addition, the Dotnet logo in the demo's picture is from MS website, so this logo copyright is owned by Microsoft, hereby declare.


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.