Globalization based on ASP.NET

xiaoxiao2021-03-06  46

Globalization based on ASP.NET

Under sharing, this method is ok, the test passes

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-40751.html

New Post(0)