(1). Functions
When a system has a certain scale and may be sold to other countries outside China, multiple resource files should be implemented.
Ben Code This example describes how to implement a system with different resource files, such as simplified, traditional, and English.
Implementation principle:
Store the multi-resource source file in multiple *. txt files (such as cn.txt en.txt, Program Based on the current Browser
Language settings
(2). The specific steps are as follows:
1. Create a resource file
A. Create a notepad file a.txt and write: _ name = Name in it
B. Select "file"-> "Save as"-> select the format to be saved in the "encoding" format bar at the bottom of the pop-up window:
Unicode or Unicode big endian or UIF-8,
Do not select the ANSI encoding format (otherwise, resources cannot be retrieved during reading, and I output the following output during the experiment :"? ")
2. Compile the resource file
Open the DOS window and run the following command:
C:/program files/Microsoft Visual Studio. NET 2003/SDK/V1.1/bin/resgen C:/inetpub/wwwroot/multiple resource files/resource/a.txt
Note: You must change the path to the actual path of your file.
A resource file is generated under the current directory a.txt at runtime: A. Resources
When the system is running, it dynamically reads the. Resources file to display different resource files.
The written code is a *. CS file. The computer only recognizes *. aspx. resx after *. CS is compiled.
(3). Code
After step 2, you can run the code.
The main operation resource file class code is as follows:
Using system;
Using system. Resources;
Using system. Globalization;
Multiple resource files in namespace
{
/// <Summary>
/// Summary of class1.
/// </Summary>
Class resourceclass
{
/// <Summary> interface resource object </Summary>
Public ResourceManager myresmanager;
/// <Summary> Interface Area object </Summary>
Protected cultureinfo myculture;
Protected string strpath = @ "C:/inetpub/wwwroot/multiple resource files/resource"; // modify it to your actual path.
Public String strlangstring = "ZH-CN ";
Public resourceclass ()
{< br>
// create an information source Manager instance
This. myresmanager = ResourceManager. createfilebasedresourcemanager ("A", this. strpath, null);
// create a region instance
This. myculture = new cultureinfo (this. strlangstring);
}< br> Public String getresource (string strkey)
{< br> string strvalue = "";
strvalue = myresmanager. getstring (strkey, myculture);
return strvalue;
}< br> Public static string getbrowserdefaultlanguage (string strlangstring) // "ZH-CN, ZH-tw; Q = 0.5 "
{< br> try
{< br> int [] intlang = new int [3];
Intlang [0] = strlangstring. indexof ("ZH-tw ");
Intlang [1] = strlangstring. indexof ("ZH-CN ");
Intlang [2] = strlangstring. indexof ("en ");
Int intmin = 0;
If (intlang [0]! =-1 & intlang [1]! =-1) {intmin = math. Min (intlang [0], intlang [1]);}
If (intlang [2]! =-1) {intmin = math. Min (intmin, intlang [2]);}
If (intmin = intlang [0]) // traditional Chinese.
{
Return ("ZH-tw ");
}
Else if (intmin = intlang [1]) // Simplified Chinese.
{
Return ("ZH-CN ");
}
Else // english.
{
Return ("en ");
}
}
Catch
{
Return ("ZH-CN"); // Simplified Chinese
}
}
}
}
(4). Set the resource type in the current area of the browser
Select the menu in the browser: "tool"-> "option"-> "general" tab-> "language", and select the language.
After the selection, the program will automatically read the resource files set by the current browser to display different files.
Only an a.txt file is created. You can create multiple resource files as needed.
To create more resource files.
For example, the chinese.txt file displays: _ name = Name
The english.txt file displays: _ name = Name, etc. You can even create any language resource file.
(5) Download Sample Code
Http://www.cnblogs.com/Files/ChengKing/ResouseExample.rar