How to Implement webapplication in multiple languages

Source: Internet
Author: User

The Internet has no borders, so that any webapplication with international operation goals (websites also fall into the scope of webapplication) must pay attention to the version handling problems in multiple languages. This issue must be taken into consideration at the early stage of the architecture. However, some mature open-source projects have provided us with a good reference. CS is a good example.

Generally, multi-language processing is required in the following aspects: page prompts and operation description text in the program, error information, explanation page that jumps after an error, Help Page, any navigation information in the page, etc. These can be called text resources, in view. net has powerful support for XML. You can consider storing some of the above operation prompts and error messages in XML structure files, while the help pages of the explanation pages are made into HTML pages respectively, put it in a separate folder. This constitutes a Language Pack. Similarly, you can create language packs in other languages and name them respectively.

With resources, you only need to construct a resource manager to read different resource content. This ResourceManager only reads XML files. You can refer to the configuration of Asp.net. Let's take a look at the resource. XML structure of CS.

<? XML version = "1.0" encoding = "UTF-8"?> <Root> <! -- Main navigation --> <Resource Name = "weblogs"> blog </resource> <Resource Name = "Forums"> Forum </resource> <Resource Name = "Photos"> album </resource> <Resource Name = "admin"> management </resource> <! --... --> </Root>

We can see that this structure is very simple. Similarly, it copies the error information in the format of Windows errorcode in another message. xml file. The format is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Root> <message id = "1"> <title> unauthorized logon </title> <body> you are not a super administrator. </body> </message>

The ResourceManager class implementation is used to conveniently manage these structured information. The specific implementation in the middle is not described in detail. It is nothing more than reading XML Information and returning string values. Then the encapsulated class, we can conveniently call these static methods when we need to display the prompt information.

throw new Exception(ResourceManager.GetString("exception_norssurl","zh-CN"));

Currently, you need to consider whether to dynamically switch the language based on your browser, or whether to know whether your environment is configured in Web. config. In fact, these two methods can be combined to configure the default language pack in the web. config of Asp.net, and then determine the language used based on the user's machine information.

So how can we achieve dynamic switching? You can provide another language. XML, configure the name of the folder where all the language packs are located, and then read the configuration through the program to dynamically find the required language pack address, then read the specified information in the specified Language Pack. Let's take a look at the structure of ages. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Root> <! -- Specify enabled ages here --> <language name = "U. s. english "Key =" En-us "/> <language name =" simplified Chinese "Key =" ZH-CN "/> <language name =" Traditional Chinese "Key =" zh-tw "/> <language name =" Danish "Key =" Da-DK "/> <language name =" Hellenic "Key =" El-Gr "/> </ root>

So far, the entire webapplication can be easily switched over to multiple languages. Well, now it seems that it is not very simple. Of course, there are other methods to implement the multi-language, such as directly creating resource files and then using them. the methods used to read Resources in the. NET class library are similar. However, in the multi-language processing mode such as CS, you can directly use the existing code and XML structure to any other webapplication, almost one hundred percent of reuse.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.