This function in fact, we already know, now because there is a project to use this technology, so go to a comprehensive search for a bit. Make a record of yourself, easy to find use later.
Creating a WEB application that can have multiple languages is a complex task before ASP.net version 2.0. If you use resource files (RESX) and ResourceManager, you need to manually isolate localizable elements and perform your own resource loading process, which takes a lot of effort and requires a lot of code. ASP.net version 2.0 greatly simplifies this process and adds a number of features, such as:
- Automatic detection of the Accept language (accept-language) HTTP request header field sent by the client browser
- To connect a control or its properties to a resource by using a declarative resource expression
- Accessing resources and strongly typed resources through programs
- Automatically compile a RESX or RESOURCE file and link it to the Runtime satellite assembly
- Provides further design-time support for the creation of resources
- Provides a model with full extensibility to enable the RESX model to be exchanged
first at the page level (<%@ page uiculture= "Auto" culture= "Auto"%> or the entire portal level (in Web.config <globalization uiculture= "Auto" culture= "Auto"/>) defines specific uiCulture
and The Culture
property. The values for both properties are auto. This instructs the asp.net to detect the page's execution based on the client browser's preferred culture and set the current thread culture and UI culture. If it is not auto, you can specify a specific language for it.
If you need to read information from a database as a standard for selecting a language, You need to make changes to Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture, and you need to be in the initializeculture (
method, because automatic detection of the preferred browser language occurs in the early part of the page lifetime. &NBSP
can invoke the contents of a resource file either through the background code or directly on the page.
has two types of resource expressions: explicit and implicit.
The
form of a resource expression |
Description |
An explicit |
<%$ Resources:[filename prefix,]resource-key %>
An explicit expression is used to define the value of a control property in declarative syntax,resource-key(required) for mapping to a value in a resource. filename prefix parameter is optional,filename Specifies the name of the resource file in the Global Resource folder. |
An implicit |
<asp:Label ID="Label1" runat="server" meta:resourcekey="resource-key-prefix" />
An implicit expression uses declarative syntax as a property of a control or object, and defines a Resource-key-prefixthat performs many property assignments for the control. The resource file contains a number of regular form resource-key-prefix. potential resource keys for the property, such as Label1keyprefix.text and Label1keyprefix.font-name. All resources are only available from Local resources . You can consider an expression as a short format notation for mapping one to more control properties without explicitly defining the property in the page. |
To retrieve resource values programmatically:
You can use declarative syntax to set the ASP.net server control property value to a resource value or to programmatically retrieve a resource value. You might do this if the resource value is unknown at design time or if you need to set resource values based on run-time conditions.
You can get resource values from local resource files and global resource files that use methods that return an object that can be cast to the appropriate type. Because ASP.net compiles global resources with strong types, you can also use strongly typed members to get global resources.
Call the getlocalresourceobject or getglobalresourceobject method to read a specific resource from a global resource file or a local resource file, respectively. These overloaded methods are provided in the HttpContext and TemplateControl classes.
The GetGlobalResourceObject method takes the resource class name and the resource ID. The class name is based on the. resx file name. For example, the file WebResources.resx and all associated localized files are referenced by the class name webresources .
The GetLocalResourceObject method takes a resource name that represents the ResourceKey property.
The following code example shows how to obtain a resource value from a local resource file and a global resource file. These methods return an object, so you must cast the resource to the appropriate type.
The default local resource file stored in the App_LocalResources special folder is named according to the ASP.net page. For example, if the following code is used in an Default.aspx page, you must name the resource file Default.aspx.resx. In this example, a string resource named Button1.Text is added to this file, and the resource has a value of "Found resources."
In addition, in this example, the default global resource file stored in the App_GlobalResources special folder is named Webresourcesglobal.resx. A string resource named Logourl was added to this file, and the resource has a http://go.microsoft.com/fwlink/?LinkId=49295 value or a URL to another image
<% @ Page Language = " C # " %>
< script Runat = " Server " >
protected void Button1_Click ( Object sender, EventArgs e)
{
Button1.Text =
GetLocalResourceObject ("button1.text"). ToString ();
Image1.imageurl =
(String) GetGlobalResourceObject (
"webresourcesglobal", "logourl" );
image1.visible = true;
}
</ Script >
< HTML >
< Head ID = " Head1 " runat = " Server " >
< title > Untitled Page </ title >
</ Head >
< Body >
< Form ID = " Form1 " runat = " Server " >
< Div >
< Asp:button ID = " Button1 " runat = " Server "
OnClick = " Button1_Click "
Text = " Get Resources " />
< asp:image ID = " Image1 " runat = " Server "
Visible = " false " />
</ Div >
</ form >
</ Body >
</ HTML >Retrieving global resources with a strong type
Obtain resources by following the syntax:
Resources.Class.Resource
The resource is compiled into the namespace resources, and each default resource becomes a member of the resources class. For example, if you create a default resource file WebResources.resx, and the file contains a resource named WelcomeText , you can reference the resource in code, as shown in the following code example:
String Welcome;
Welcome = Resources.WebResources.WelcomeText;