ASP Multi-lingual website support
(Calls to local resource files and global resource files and summary of areas to be noted)
First, Local resource file (app_localresources) :
①, generation and invocation of local resources
1. The VS Auto -generation method for local resources:
Adjust the page to Design view, and then in the VS2008 menu bar
"Tools" = "Generate local resources" to generate a resource file for this page, which can be viewed in the App_LocalResources folder. Eg:Default2.aspx. resx
2. Manual Generation method for local resources:
Select site = Add New item = and then select Resource file to create a resource file, but you can see the resource file you just created. resx is stored in the App_GlobalResources folder, currently a global resource file, and you need to create a Web site named App_ LocalResources folder (that is, the local resource folder) then drag the global resource file you just created into the current folder, and then add the meta:resourcekey= " resource key " code to the control when you call it. Specifically explained in the call.
3. Invocation of local resources:
(1), server control
1.1, the foreground Implicit use of localized resources , VS2008 automatically generated local resources in the control will automatically include the meta:resourcekey= " resource key " code, If a control is not scanned, then there is no code to identify the code that needs to be added manually
eg
<asp:label ruant= "Server" id= "lbl_msg" meta:resourcekey= "lbl_msg" ></asp:Label>
1.2, the front desk shows the use of localized resources
eg
<asp:label ruant= "Server" id= "Lbl_msg"
text= "<%$ resources:lbl_msg. Text %>"
tooltip= "<%$ resources:lbl_msg. ToolTip%>"
></asp:Label>
The blue icon and the red icon indicate the form of binding data (show/Hide), as shown in:
1.3. Use localized resources for background encoding
Method One:
The background gets the resource value through the resource key, then assigns the value to the Page object
This.txtbox1.text= (String) GetLocalresourceobject ("Resource Key");
or
This.txtbox1.text= (String) httpcontext.getLocalresourceobject ("Resource filename", "resource Key");
(2), non-server control (HTML control, input ...) :
1.1. The HTML control or input control cannot automatically generate the resource file, you need to manually add the meta:resourcekey= " resource key " tag and add it to the resource file, but you must note that the server identity attribute must be added to the control: runat= "Server"
eg
<input id= "txt_msg" type= "text" meta:resourcekey= " txt_msg"/>
Test Result: Unable to display the resource txt_msg value, modify it as follows:
<input id= "txt_msg" type= "text" meta:resourcekey= "txt_msg" runat= "server" />
Test Result: Normal display resource txt_msg value
②, Precautions:
1. Each page must contain a default local resource file (the resource file name differs from the page file name in that the resource file name is added as a. resx suffix on the page file name, including the suffix of the file)
Eg:index.aspx. resx.
Default local resource file function: When other local resource files, Eg:index.aspx.en-us.resx or ... A key is not included in the resource file, and a key is included in the default local resource file, and the value is taken from the default local resource file.
2. If the page needs to support multiple languages, you can copy the default local resource file and then modify its file name
Eg:index.aspx. en-us. resx
Then the index.aspx. The key values in en-us. resx can be modified to English
3, 1) If you need to use all the pages in the same language, you can configure the Web. config file to remove the culture= "Auto" uiculture= "Auto" from the page pages directive, or use the language form in the page first
eg
<system.web>
<!-en-us (English), ZH-CN (Chinese) ...
UICulture-controls such as buttons for the user interface display
Culture-Determines how various data types are organized, such as numbers and dates, currency
-
<globalization culture= "en-us" uiculture= "en-US"/>
</system.web>
2) If a page and other pages are in a different language, you can set the culture and UICulture in one of the pages page commands, which are used to make the page take precedence with the browser's language settings to automatically load the language's resource files, and so on.
eg
<%@ page culture= "ZH-CN" uiculture= "ZH-CN"%>
4. If you want to set the language used in the page background (that is,. cs file) by passing parameters, then
eg
Namespace reference:
Using System.Threading;
Using System.Globalization;
protected override void InitializeCulture ()
{
String culture = request.querystring["culture"]. ToString ();
if (Culture! = "")
{
Thread.CurrentThread.CurrentCulture = cultureinfo.createspecificculture (culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo (culture);
}
Else
{
Default Chinese
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture ("ZH-CN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo ("ZH-CN");
}
Base. InitializeCulture ();
}
By passing the encoding parameters, rewrite the InitializeCulture (Initialize language method) to change the way the page language is displayed.
5, the local resource file can only be called on this page, can not be called by other pages, if there are two pages index1.aspx and page 2.aspx, the following resource file
Eg:Index1.aspx.resx
Page2.aspx.resx
Index1.aspx Page cannot call the resource key value in page 2.aspx.resx;
Page 2.aspx also cannot call the resource key value in Index1.aspx.resx;
Index1.aspx page can only call the resource key value in Index1.aspx.resx;
Page 2.aspx pages can only invoke the resource key value in page 2.aspx.resx;
Second, Global resource File (App_GlobalResources) :
① , global resource generation and invocation
1 , the manual generation method for global resources:
Select site = Add New item = and then select Resource file to create a resource file, but at this point you can see the resource file you just created. resx is stored in the App_GlobalResources folder and is currently a global resource file.
2 , the call to the foreground global resource file:
1 ), add <%$ resources to the control you want to invoke : resource file name , resource key %>
eg :
<asp:label id= "Lbl_global" runat = "server" Text = "<%$ resources:lang,lbl_global%>< /c11> ">
</asp:Label>
2 ), Front desk Page Level JavaScript in Call The global resource file method:
<%= Resources . Resource File name . resource Key %>
eg :
<script>
var msg= " <%= Resources . Lang lbl_global%>"
</script>
3, the background global resource file Call:
1 ), using Resources directly in the background . Resource File name . resource key to get resource value
eg :
This. Lbl_global. text=Resources. Lang Lbl_global ;
2 ),
The background gets the resource value through the resource key, then assigns the value to the Page object
This.lbl_global. Text= (String) GetGlobalresourceobject ("Resource Key");
Or
This.lbl_global. Text= (String) httpcontext.getGlobalresourceobject ("Resource filename", "resource Key");
3 ), use the ResourceManager class in the background to get resources
ResourceManager rm = new ResourceManager (Resource1.ResourceManager.BaseName,
Assembly.getexecutingassembly ());
String str=rm. GetString ("resource Key");
② Precautions :
1, Global resource files, stored in the App_GlobalResources folder, with. resx suffix, you must first add a default global resource file
Eg:lang. resx.
The default global resource file functions: ①, when other global resource files, Eg:lang. en-us. resx or Lang. A key is not included in a resource file such as ZH-CN. resx, and a key is included in the default global resource file, the value is taken from the default global resource file, and ②, the resource is invoked only through the default resource file name, which is
Resources. lang. resource Key (correct √)
and cannot be used
Resources. Lang en- us resource key ; (Error x)
Langen-us. resx or Lang. ZH-CN. resx is called automatically depending on the language used
2 . Global resource files can be called on all pages of this website.
Other areas needing attention are similar to local resources, which are not repeated here.
The above is my understanding of local resources and global resources, if there is a mistake, please the great God to give guidance. ^.^
※ Unresolved issues:
cannot implement external Javascript in the question of calling resources, if you great God have a good solution, trouble sharing under Oh, thank you ha!
※ about the website support multi-language issues related articles:
Http://www.cnblogs.com/FredTang/archive/2013/01/23/2873231.html
Http://www.cnblogs.com/wenjl520/archive/2010/10/17/1853367.html
Http://www.cnblogs.com/lansetiankong/archive/2010/07/30/1788790.html
Http://www.cnblogs.com/winsonet/archive/2010/09/08/1821107.html
Multilingual Resource File Authoring tool:
Http://www.cnblogs.com/LifelongLearning/archive/2010/07/04/1771019.html
ASP. Web site supports multiple languages