There are two ways to use resources: directly embed them into an assembly or load external files.
If you use an external file, you must deploy an external file (Resource) along with the Assembly and ensure that the resource file is accessible during running. If the resource file and the Set (.exe) cannot meet each other, the problem may occur.
The first method is more stable than the second method and has a lower chance of errors.
Compile external embedded resources (direct embedding)
Steps:
1. Compile resources as embedded Resources
By doing this, you have your ucted Visual Studio to embed the file into the physical image of the output assembly. exe file.
2. Method for accessing resources at runtime: *** get current Assembly object.
Dim asm As Assembly = Assembly. GetExecutingAssembly ()
'*** Load embedded resource into stream
Dim ResourceName As String = "LitwareSmartClient.LitwareLogo.png"
Dim str As Stream = asm. GetManifestResourceStream (ResourceName)
'*** Convert stream into image and load in' *** picture box
Dim img As Image = Image. FromStream (str)
PictureBox1.Image = img
Resource names are case-sensitive, even in non-sensitive languages such as vb. In addition to images, you can also easily embed text-based files, such as xml, js, and SQL
Use resource files
This is another way to use resources in. net, which is simpler in some scenarios. In addition, vs provides convenient use methods.
Use resources in non-vs mode
In. net, resource files are used to embed Resources in the Assembly. The most important benefit of using resource files is that the language and localization elements in the Assembly can be separated from the code. To implement separation, you need to create a resource file for each language that needs to be supported. The resource file is a text file containing xml and uses resx as the extension. The following is a piece of the resource file: <root>
<Data name = "MainFormCaption">
<Value> Litware Customer Manager </value>
</Data>
<Data name = "UserWelcome">
<Value> Good day </value>
</Data>
<Data name = "ErrorMessage1">
<Value> Oh no, Something went wrong! </Value>
</Data>
</Root>
You can use resgen.exe to compile the resource file into a binary image. The compilation command is as follows:
RESGEN. EXE LitwareStrings. resx LitwareStrings. resources
Compiled into *. resources.
To use resources and compile it into an assembly, run the following command:
AL. EXE/t: library
/Out: LitwareStrings. resources. dll
/Link: LitwareStrings. resources
Usage: Dim asm As Assembly = Assembly. Load ("LitwareStrings. resources ")
Dim rm As New System. Resources. ResourceManager ("LitwareStrings", asm)
Dim caption As String = rm. GetString ("MainFormCaption ")
The following code compiles resource files into strongly typedResource,
RESGEN. EXE LitwareStrings. resx LitwareStrings. resources/str: vb
In this way, you can access resources through properties. The compiled resource file contains the LitwareStrings class. This class uses ResourceManager to implement strongly-typed attributes, such As Shared ReadOnly Property MainFormCaption () As String
Get
Return ResourceManager. GetString ("MainFormCaption", resourceCulture)
End Get
End Property
Use resource files in vs2005:
Steps:
1. Add a resource file
2. vs compiles. resx to. resources, and then connects the physical image of the Assembly (physical image)
Vs will also create a strong type of resource class, which is under mynamespace. (C # Where is it ?) Instead of directly accessing the resource class, access the Code through ResourceManager:
Sub Main_Load () Sub Main_Load (sender As Object, e As EventArgs)
Handles MyBase. Load
Me. Text = _
My. Resources. LitwareStrings. MainFormCaption
Me. lblUserWelcome. Text = _
My. Resources. LitwareStrings. UserWelcome
End Sub
Project-level resource files
You can explicitly add one or more resource files to vs, but this is usually unnecessary because vs will create a project-level resource file when creating a project. This file can be accessed through the Project Properties dialog box.
You can access the text in the resource file as follows:
Private Sub LoadResources ()
'*** Load project-level resources
Me. Text = My. Resources. MainFormCaption
Me. lblWelcomeMessage. Text = My. Resources. UserWelcome
End Sub
You can see how simple it is to access strings in resource files through a strong type! Even more, you can access strongly-typed resource files, even if these resource files contain images and files with xml, SQL, and js content.
For example, if you add litwarelogo.png and resources. xml to the project-level resource files, these resources are automatically embedded into the output assembly of the project. You can access them in a strongly typed way. Code: Me. picLogo. Image = My. Resources. LitwareLogo
Dim xmlDoc As New Xml. XmlDocument
XmlDoc. LoadXml (My. Resources. MERs mers)
You can see that a strong natural resource class automatically converts the. PNG file to an Image object and can be directly loaded to the PictureBox. A strong type of resource class also automatically converts the. xml file to a string, and then the characters can be easily loaded to the XmlDocument object.
Resource settings and Localization:
Localization of software projects is a general requirement so that they can be used by people who speak different languages .. Net provides a convenient way to localize programs and Dll libraries.
First, you need to be familiar with the CultureInfo class, a CultualInfo object, tracking a Cultual name, a Cultual Name
Indicates a language.
The English cultual name is "en", France is "fr", and can also contain additional information, which specifies the region (region), such, "en-us" in American English and "en-gb" in English ". the following code creates and initializes a CultureInfo object Dim culture1 As CultureInfo = New CultureInfo ("en-US ")
Dim culture2 As CultureInfo = New CultureInfo ("en-GB ")
Dim culture3 As CultureInfo = New CultureInfo ("fr ")
Dim culture4 As CultureInfo = New CultureInfo ("fr-BE ")
Two CultureInfo objects are associated with the current thread. The first CultureINfo object lists the representation of the current Culture, and the second CultureINfo object indicates the current UI Culture ). (The first CultureInfo object listed represents the current culture while the second CultureInfo object represents the current UI culture .)
The following code lists the two Culture names of two CultureINfo objects, '*** determine current culture and current UI culture
Dim t As Thread = Thread. CurrentThread
Dim currentCulture As CultureInfo = t. CurrentCulture
Dim currentUICulture As CultureInfo = t. CurrentUICulture
'*** Display cultures in console
Console. WriteLine ("Current Culture:" & currentCulture. Name)
Console. WriteLine ("Current UI Culture:" & currentUICulture. Name)
The first CultureInfo object named CurrentCulture cannot be used to localize strings in programs. It only affects. netFramework formatting date, number, and currency. Therefore, do not modify this attribute (static it) when the localization program adapts to multiple languages ).
If you must use a program to modify CurrentCulture, you must use the Culture name that contains the region, for example, "en-us"
. If there is no region information, for example, "en", an exception is thrown, because you cannot determine how to format it during running.
The second CultureINfo object named CurrentUICulture is very important for localization. Modifying the CurrentUICulture associated with the current thread will affect how the. net framework and ResourceManager classes load the Assembly containing embedded resources.
Localization steps
1. Copy multiple resource files, one corresponding to a language to be supported. For example, create a project-wide resource file: Resources. resx and copy multiple copies.
2. Rename the copied file. The file name format is: add the Culture name before resx. For example, the resource file for the generic French localized character set (French localized strings) is named Resources. fr. resx, and the French localized character set for Belgium is named Resources. fr-BE.resx.
Satellite assembly
When compiling a project that contains a localized resource file (this project contains multiple resource files, as shown in the preceding example), vs does not compile all the resource files into an output assembly, instead, each resource file is compiled into a separate assembly. This type of Assembly contains all resources without code. This localized assembly contains only resources is called "satellite assembly ".
Each Satellite assembly is associated with a master assembly called a "neutral assembly. Neutral Assembly loads the Satellite assembly as needed to obtain localized resources.
When the Satellite assembly and Neutral assembly are deployed in the AppBase directory, the deployment must comply with the Assembly loader rules. Each Satellite assembly must be deployed in a folder named after its localized Cultual name. For example:
The appbasepackage contains the litwaresmartclient.exe neutral assembly, which must also contain the fr-BE subdirectory. This subdirectory contains the Satellite Assembly localized to Belgian French named LitwareSmartClient. resources. dll. As long as the rule. net
The Assembly loader and the auxiliary ResourceManager class can load the correct resources as needed.
Fortunately, Vs knows how to name the Satellite Assembly correctly and deploy them correctly.
Load localized Resources
When you create and edit a localized resource file and compile the project, you need to focus on how to let the program load the localized characters required by the user. One way is to obtain the reference of the current thread and set the CurrentUICulture attribute to the newly created CultureInfo object. The vb code in winform is as follows: My. Application. ChangeUICulture ("fr-BE ")
The other is to use the registration key.
Note: The net Assembly loader first precisely matches the Satellite assembly (language and region). If it fails, it will find the available assembly that only matches the language. Use the embedded resources of the Neutual assembly if no matching exists.
When compiling a Neutal assembly, you can specify the default Cultual resource for it with a special attribute. For example, add <Assembly: System. Resources. NeutralResourcesLanguage ("en")> to assemblyInfo. vb.
When this occurs, the NeutralResourcesLanguage feature notifies the Assembly loader that the default cultual resource is used when a user requests a program localized to English.
Localized form and control settings
You already know how to localize resources based on the project-wide. This technology involves copying and modifying local resource files. Vs provides additional help for localized forms and controls, but adding language support in the future is complicated. A simple method is to create resource files, you do not need to compile the Netual Assembly. This is. netframework has the most valuable features in localization programs and Dll libraries.
Conclusion
In this article, a simple windows Form-based program is localized to cover the. net Resources and the basis of localization. The next article will discuss resources and localization in asp. net2.0.
Original article address:
Http://msdn.microsoft.com/msdnmag/issues/06/05/BasicInstincts/
Resource and localization of asp2.0
Http://www.microsoft.com/china/MSDN/library/default.mspx? Mfr = true
Because it is necessary to develop an asp program in multiple languages and see the above information, the intention is to simply record the main points of the original article. The more the results are written, the whole article is translated, however, most of the content only focuses on winform.