Globalization based on ASP.NET

zhaozj2021-02-16  90

Based on ASP.NET to achieve globalization due to project relationships, 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));} ...} Please pay attention to this line of code in this code above, in VS.NET 2002, if you use this.gettype (). namespace and this.gettype (). Assembly .Ffullname is actually ASP and 6FQPC_HH, VERSION = 0.0.0.0, Culture = neutral, publickeyToken = NULL, the obtained Assembly is obviously random production. But in the C # class file and WinForm are normal, it is unclear. As a result, it is certainly not to say, it is definitely not to find the corresponding resource file. In addition, it is necessary to emphasize that this uses form1.gettype (). Namespace ".string", if the resource file is not placed in a project root directory, please pay attention to modifying The default namespace of the project. Because the resource file is compiled, there will be the following line in the project's resource file test.resources.dll's manifest (view), .mResource public 'testm.string.zh-cn.resources' or .mResource public' Testrm.String.en-US.Resources'.

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

New Post(0)