Solution description
This scheme uses a language-based resource file method to effectively improve the maintainability of resource files. VS. NET provides a WebForm resource file solution, which cannot meet the need for unified maintenance.
Steps
1. Prepare resource files
Resource files include. resx and. resources. The former is an xml file and the latter is a binary file. This scheme is used. resources is used as a resource file. Therefore, a third-party resource editor is required to edit the resource file. I use Resourcer to edit the resource file and save it. resources file software. Add some string resources to the resource file. Generally, only the string resources are used. Some projects may require global image resources, which are the same in principle. This article does not cover these resources.
Resource file names are specified. In this solution, I set all resource file names to business. [culture]. resources. You can change the business name to any name you like. The [culture] part must be used.. net, such as zh-cn, ja-jp, and en-us.
2. Put the resource file into the project
To facilitate management, I put all the resource files in the first-level directory Resources of the website. Because the physical location of the resource file is used to read the resource file, we recommend that you put the Resources directory directly under wwwroot, otherwise it will become very troublesome due to Path Problems.
3. added the default language settings.
Add <add key = "cultureName" value = "zh-cn"/> in web. config to indicate that zh-cn is used as the default language culture by default.
4. Set the current language through Session
We use a Session variable -- Session ["cultureName"] To tell each page which language should be used for initialization. When Session ["cultureName"] = null, it indicates that the variable is not set, and the default language culture is used directly, that is, the setting value in web. config.
To change the current interface language, you only need to set Session ["cultureName"] to the specified cultural abbreviation.
Global auxiliary Resource Management
With this class, you can complete global initialization and resource reading without learning how to use ResourceManager. This class needs to input a Page object during initialization. this object will be used to obtain the Session variable value. Generally, you only need to use the GetString () method of the class.
/** // <Summary>
/// Global auxiliary Resource Management
/// </Summary>
Public class ResourceHelper
{
Private ResourceManager _ rm = null;
Private Page _ page = null;
Public ResourceHelper (Page page)
{
_ Page = page;
PrepareResource ();
}
/** // <Summary>
/// Obtain the current culture from Session ["cultureName "]
/// </Summary>
Protected CultureInfo GetCurrentCulture
{
Get
{
If (_ page. Session ["cultureName"]! = Null)
Return CultureInfo. CreateSpecificCulture (_ page. Session ["cultureName"]. ToString ());
Else // if the Session is not set, read the default settings directly from web. config.
Return CultureInfo. CreateSpecificCulture (System. Configuration. ConfigurationSettings. deleettings ["cultureName"]);
}
}
/** // <Summary>
/// Initialize Resource Management
/// </Summary>
Public void PrepareResource ()
{
_ Rm = ResourceManager. CreateFileBasedResourceManager ("business", _ page. Server. MapPath ("resources") + System. IO. Path. DirectorySeparatorChar, null );
}
/** // <Summary>
/// Obtain the resource pointer
/// </Summary>
Protected ResourceManager resource
{
Get
{
If (_ rm = null)
PrepareResource ();
Return _ rm;
}
}
/** // <Summary>
/// Obtain the resource string
/// </Summary>
/// <Param name = "ResourceID"> resource ID </param>
/// <Returns> </returns>
Public string GetString (string ResourceID)
{
Return this. resource. GetString (ResourceID, this. GetCurrentCulture );
}
}
A Demo is provided for your reference to facilitate your learning.
Demo description:
1. This demo is successfully debugged in the Windows XP Professional + VS. NET2003 Environment
2. the demo provides resource files in three languages: business. zh-cn.resources, business. en-us.resources, business. ja-jp.resources
They are all stored in the Resources directory. Each resource file contains three string Resources: 00000001, 00000002, and 00000003, indicating the user name, password, and birthday respectively.
3. Select the language in the drop-down list and click "OK" to switch the language.