Summary :
As more and more multinational companies need cross-language web applications, localization becomes particularly important in ASP. in. NET 1.1, we use the ResourceManager class to implement this job, while in 2.0, we provide more convenient methods and tools for implementation.
Introduction:
Nowadays, many cross-country business needs need to be implemented in multiple languages. For customers, they always want to see that the website is displayed in the languages they are familiar with. They do not pay attention to what technologies the website uses, the current development tools or development technologies are not doing well in localization. We still need to spend a lot of time and energy doing localization work.
ASP. the emergence of net2.0 greatly simplifies this process. with the new features of net2005, we can easily remove the language elements of the website and easily implement multi-version websites, many localized APIs can also help us complete this process.
Localization in. NET 1.1:
In 1.1, we use ResourceManager to implement this process. This process may be relatively simple for winform, but. net2003 does not bring any automation to the localization work under our webform. We still need to spend a lot of energy on repeated replication.Code.
Localization in. NET 2.0:
Localization in 2.0 is based on 1.1, but it provides a lot of automation work:
1. automatically generate page resource files
2. Convenient programming for resource File Access
3. automatically load the appropriate language for the page
4. automatic matching of controls and resource files
5. Strongly typed resource files
6. Resource file editor
7. Resource file expression
8. Resource Provider Model
Local resource file implementation:
Vs. NET 2005 menu-tools-generate local resource, the generated resource file will be placed under/app_localresources
It is displayed that not only the aspx file, but also the user control and master pages will automatically generate the resource file. The automatically generated resource file name will be added to the original file name. resx: All server controls on the page, user controls, and HTML controls running on the server are automatically added to the resource file, displaying a linkbutton resource file.
We can see that only the attributes related to the language test and tooltip are added to the resource file.
Switch back to the view status and you can see that some properties are marked in red on the right. This indicates that this property is bound to a local resource.IDEThe resource is automatically created for you. Let's take a look at the blue mark behind alternatetext. This indicates that this resource is a global resource.
Global Resource implementation:
There are often many repeated items in our project, such as masterpage, user controls, menus, and even some prompts. If these items are contained on multiple pages, every time we need to repeat their resource files, this is very unscientific, so we need to manually develop global resources. In this case, create a/app_globalresources directory and add a resource file below. First, check the resource file editor provided by IDE.
There are many types of resources. We do not need to consider too much for accessing global resources. For example, a resource file is flags. resx. Try to input resources in the compiler. flags, when you press again. smart sensing.
It can be seen that these resource files are compiled into strongly typed classes. For example, the image resource type we get is system. Drawing. bitmap.
Local Resource Access:
We usually use meta: resourcekey to specify the resource of the control. If the resource prefix of a control is linkbuttonresource1, the resource of its text attribute is linkbuttonresource1.text, this is automatically implemented by ide when a resource file is generated.ProgramTo access local resources, follow the example below:
Linkbutton button1 = new linkbutton ();
This. lnkselectculture = button1;
Button1.id = "lnkselectculture ";
Button1.postbackurl = "selectculture. aspx ";
Button1.text = (string)
Base. getlocalresourceobject ("linkbuttonresource1.text "));
Button1.tooltip = (string) base. getlocalresourceobject ("linkbuttonresource1.tooltip "));
Of course, if you do not want your controls to use resource files, you can follow the example below to modify them:
<Asp: linkbutton id = lnkselectculture postbackurl = "selectculture. aspx"
Runat = "server" meta: localize = "false"> Change culture Settings </ASP: linkbutton>
Global Resource Access:
Use the following format to locate Global Resources
<% $ Resources :[Applicationkey],Resourcekey%>
For example, an example of reading resources from glossary. resx is as follows:
<Asp: imagebutton id = "btnidesign" runat = "server" imageurl = "~ /Images/idesignlogo.jpg"
Alternatetext = '<% $ resources: Glossary, missionsatatement %>'
Postbackurl = "http://www.idesign.net" meta: resourcekey = "imagebuttonresource1"/>
Of course, you can also directly use the IDE expression dialog box to select
The procedure is as follows:
Imagebutton button1 = new imagebutton ();
// Other initialization code
Button1.id = "btnidesign ";
Button1.accesskey = (string) base. getlocalresourceobject ("imagebuttonresource1.accesskey ");
Button1.alternatetext = (string) base. getglobalresourceobject ("Glossary", "missionstatement ");
Button1.imageurl = (string) base. getlocalresourceobject ("imagebuttonresource1.imageurl ");
Button1.tooltip = (string) base. getlocalresourceobject ("imagebuttonresource1.tooltip ");
Button1.visible = (bool)
Base. getlocalresourceobject ("imagebuttonresource1.visible", typeof (control), "visible ");
Localized HTML controls and text:
HTML control: the HTML control cannot use the preceding two expressions to specify resources. Unless it is set to runat = server, the title of the page is special, it also automatically generates local resources as an object on the page.
<% @ Page Language = "C #" codefile = "default. aspx. cs" inherits = "_ default" meta: resourcekey = "pageresource1" %>
However, we can also create global resources for it.
<Head runat = "server">
<Title>
<Asp: literal text = '<% $ resources: Glossary, defaultpagetitle %> 'runat = "server"> </ASP: literal>
</Title>
</Head>
Localized text:
For a lot of static text in the project, we can use the localize control to include the text. In fact, this control is similar to literal, but it can modify the text content in the design mode at will, see how to access local and global resources
<Asp: localize id = "welcomecontent" runat = "server" meta: resourcekey = "welcome"> welcome! </ASP: localize>
<Asp: localize id = "welcomecontent" runat = "server" text = '<% $ resources: Glossary,
Welcometext %> '> welcome! </ASP: localize>
Multi-language implementation:
Copy a new resource file from the default resource file. You only need to add the region code before resx and edit it one by one. After the following settings are completed, the browser will automatically follow the settings (tool-Option-language) call a new resource file
<% @ Page uiculture = "Auto" Culture = "Auto">
You can also specify a language through the Web. config file.
<System. Web>
<Globalization culture = "Es-es" uiculture = "es">
</System. Web>