System Multiple resource file code examples

Source: Internet
Author: User
Tags format object implement interface object object resource window visual studio
Example

(i). function
When a system has a certain size, may want to sell to other countries abroad, this time to implement a variety of resource files.
This code example will describe how to implement: a system with a simplified, traditional, English and other different resource files.
Implementation principle:
Store multiple resource files in multiple *.txt files (such as CN.txt En.txt), which are based on the current browser
Language settings read the appropriate resource file
(b) The specific steps are as follows
1. Create a resource file
A. Create a Notepad file: A.txt, and write in it: _name= name
B. Choose file-> Save as-> Select the format you want to save 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, the reading will not retrieve the resources, I experimented with the output of a: "?")
2. Compiling resource files
To open a DOS window to run the command:
C:\Program Files\Microsoft Visual Studio. NET 2003\sdk\v1.1\bin\resgen C:\Inetpub\wwwroot\ multiple resource files \resou Rce\a.txt
Note that the path should be modified to the actual path of your own file
After running, a resource file is generated under the current folder of A.txt: A.resources
At runtime, the system reads A.resources files dynamically to display different resource files, just as we are programming
Write the code for *.cs files, the computer only know *.cs compiled *.aspx.resx the same
(iii). Code
After (two) steps, you can run the code.
The main operation resource file class code is as follows:

Using System;
Using System.Resources;
Using System.Globalization;

Namespace multiple resource files
{
<summary>
Summary description of the CLASS1.
</summary>
Class Resourceclass
{
<summary> Interface Resource Object </summary>
Public ResourceManager Myresmanager;

<summary> interface Area Object Object </summary>
protected CultureInfo myculture;

protected string strpath = @ "C:\Inetpub\wwwroot\ Multiple resource file \resource"; This is to be modified to its own actual path

public string strlangstring = "ZH-CN";

  public resourceclass ()
  {  
   
   / /Create a Resource Manager instance
   this.myresmanager = ResourceManager.CreateFileBasedResourceManager ("A", This.strpath,null);
&NBSP;&NBSP;&NBSP
   //Establish a zone instance
   this.myculture = new CultureInfo ( this.strlangstring);
  }
  public string GetResource (String strkey)
  {
   string strvalue = "";
   strvalue = myresmanager.getstring (strkey,myculture);
   return strvalue;
  }
  public static string Getbrowserdefaultlanguage (String strlangstring)  //"zh-cn,zh-tw;q=0.5"
  {
   try
   {
    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"); Chinese Simplified
}

}
}
}

(iv). Set Browser Current zone resource type
Select the menu in the browser: Tools-> options-> the General tab-> language.
When the selection is good, the program automatically reads the resource files for the current browser settings to display different files.

Only one a.txt file was created, and the reader could create several different resource files as needed.
To create more resource files.
For example: Chinese.txt file display: _name= name
English.txt file display: _name=name, etc., can even create any language resource files



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.