We have known this feature for a long time. Now, because a project needs to use this technology, we have gone through some comprehensive searches. Record it by yourself for later use.
Creating Web applications in multiple languages is a complex task before ASP. NET 2.0. If you use resource files (RESX) and ResourceManager, You need to manually separate the localized elements and execute your own resource loading process, this requires a lot of effort and a lot of code. ASP. NET 2.0 greatly simplifies this process and adds many functions, such:
- Automatically checks the HTTP request header fields sent by the client browser using the Accept-Language
- Use a declarative resource expression to connect a control or its attributes to a resource
- Access resources and strongly typed resources through programs
- Automatically compile the RESX or RESOURCE file and link it to the sub-assembly of the Runtime Library
- Further Design Support for Resource Creation
- Provides a fully scalable model for the RESX model to be exchanged.
At the Page level (<% @ Page UICulture = "auto" Culture = "auto" %>) or the whole Portal level (in the Web. <globalization uiCulture = "auto" culture = "auto"/>) in config to define a specificUICulture
AndCulture
Attribute. The values of both attributes are auto. This indicates that ASP. NET performs page Detection Based on the preferred culture of the client browser and sets the current thread and UI culture. If it is not auto, you can specify a specific language for it.
If the information to be read from the database is used as the language selection standard, you need to modify Thread. CurrentThread. CurrentCulture and Thread. CurrentThread. CurrentUICulture.InitializeCulture()
Method, because the automatic detection of the preferred browser language occurs early in the page lifetime.
You can use the background code or directly raise the content in the resource source file on the page.
There are two types of resource expressions: explicit and implicit.
Resource expression format |
Description |
Explicit |
<%$ Resources:[filename prefix,]resource-key %>
An explicit expression is used to define the value of a control attribute in declarative syntax,Resource-key(Required) is used to map values to resources.Filename prefixThe parameter is optional,FilenameSpecifyGlobal ResourcesThe name of the resource file in the folder. |
Implicit |
<asp:Label ID="Label1" runat="server" meta:resourcekey="resource-key-prefix" />
An implicit expression is used as a property of a control or object in declarative syntax and is definedResource-key-prefixIs used to allocate multiple properties for the control. Resource files contain many common formsResource-key-prefix.PropertyFor example, Label1KeyPrefix. Text and Label1KeyPrefix. Font-name. All resources can only beLocal resources. The expression can be considered as a short format representation to map one to multiple control properties without explicitly defining properties on the page. |
Search resource values programmatically:
You can use declarative syntax to set the attribute value of an ASP. NET Server control to a resource value, or programmatically retrieve the resource value. If the resource value is unknown during design or you need to set the resource value according to runtime conditions, this may be done.
You can obtain resource values from local and global resource files. The methods used by these files return an object that can be forcibly converted to the appropriate type. Because ASP. NET uses a strong type to compile global resources, you can also use a strong type member to obtain global resources.
CallGetLocalResourceObjectOrGetGlobalResourceObjectTo read specific resources from the global resource file or local resource file respectively. These overload methods are provided in the HttpContext and TemplateControl classes.
GetGlobalResourceObjectThe method uses the resource class name and resource ID. This class name is based on the. resx file name. For example, the file WebResources. resx and all associated localization files are named by the class name.WebResourcesReference.
GetLocalResourceObjectThe method indicates the Resource Name of the ResourceKey attribute.
The following code example shows how to obtain resource values from local resource files and global resource files. These methods return an object; therefore, the resource must be forcibly converted to an 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 on the Default. aspx page, the resource file must be named Default. aspx. resx. In this example, a file namedButton1.TextString resource, and the resource has"Found Resources"Value.
In addition, in this example, the default global resource file stored in the App_GlobalResources special folder is named WebResourcesGlobal. resx. Added a file namedLogoUrlAnd the resource carries a http://go.microsoft.com/fwlink? LinkId = 49295 or URL of other images
<% @ Page Language = "C #" %>
<Script runat = "server">
Protected void button#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 = "button#click"
Text = "Get Resources"/>
<Asp: Image ID = "Image1" runat = "server"
Visible = "false"/>
</Div>
</Form>
</Body>
</Html> use a strong type to retrieve Global Resources
-
Use the following syntax to obtain resources:
Resources.Class.Resource
The resource will be compiled to the namespaceResourcesAnd every default resource will beResourcesClass member. For example, if the default resource file WebResources. resx is created and the file contains a file namedWelcomeTextYou can reference the resource in the code, as shown in the following code example:
String welcome;
Welcome = Resources. WebResources. WelcomeText;