Globalization based on ASP.NET

zhaozj2021-02-08  288

Globalization based on ASP.NET

Because the project's relationship, these two days have been studying the globalization of ASP.NET. I know that there is a special I18N processing scheme in the Java system, and there is no possibility in .NET. Therefore, check the information on the Internet, after some "bitter", slightly smaller. I don't have a very complete solution online (may I have not found), I spent some lessons, I hope to share with you. If you have any deficiencies, please advise. In this article, the basic steps to implement globalization in ASP.NET will be described in the way in VS.NET2002, and where they need to pay attention will be described. The first step, we created a Web Application called TestRM. This project will be default - AssemblyInfo.cs - Testrm.csproj - Testrm.csproj.Webinfo - Testrm.SLN - Testrm.suo - Testrm.Vsdisco - Webform1.aspx (including AspX.cs and Asp cpx.resx) - Global.asax (Including asax.cs and asax.resx) - Web.config Step 2, you need to create a number of different language resource files for the project. In .NET, the extension of the resource file is .resx, this file is basically XML, VS.NET provides a very convenient resource file editing tool (personal feeling very like Xmlspy), there is not much to say. Here, as an example of Simplified Chinese (EN-CN) and US English (EN-US). Adding a resource file named string.en-usx and string.zh-cn.resx, adding a String.en-US.resx, adding a String.en-US.resx, adding a String.en-US.RESX to the project. A record, content is name: string001; value: Welcome, add a record in String.en-cn.resx Name: string001; Value: Welcome. The structure of the XML file is as follows: ... Welcome ... This needs to be explained here. If you don't want to switch language by modifying the configuration file, you want to switch the language by setting up Windows "Regional Settings", skip the third step. (Is there any such need ??? -_-) This, after this item is compiled, two subdirectories that appear in the bin directory, which have a DLL file. Testrm.Resources.dll. Step 3, modify the Global.asax and Web.config files to achieve dynamic switching languages.

First, we need to add the event in Global.asax Application_BeginRequest following code: protected void Application_BeginRequest (Object sender, EventArgs e) {Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture (ConfigurationSettings.AppSettings [ "DefaultCulture"]);} This is the The simplest application, of course, you can store some configuration items into cookies. Then, add the following code in Web.config: Note that the appsettings element is the same level with System.Web. Finally, in the event the WebForm1 Page_Load following code: public class WebForm1: System.Web.UI.Page {private void Page_Load (object sender, System.EventArgs e) {WebForm1 form1 = new WebForm1 (); ResourceManager resManager = new ResourceManager (. form1.GetType () Namespace ".string", form1.GetType () Assembly.); System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; Response.Write (resManager.GetString ( "String001 ", ci));

转载请注明原文地址:https://www.9cbs.com/read-1078.html

New Post(0)