Overview of Hippo Technology in ASP. NET open-source framework (IV)-Implementation of multiple languages

Source: Internet
Author: User

Iv. multi-language implementation

The previous chapter describes how to connect to data. This chapter describes how to connect multiple languages.

Many companies have now launched international operations, so multi-language support has become a factor that must be considered. Currently, most systems that can truly implement multiple languages are expensive.

The multilingual support of the system is directly benefited from Asp.net itself, which is stored in UTF-8 format rather than traditional local encoding. On the Asp.net page, each control can specify different strings for multiple country character sets, but this method is not flexible, especially when many contents are pieced together from the database. For this reason, I used another method, similar to a resource file, but directly wrote the content to the Web. config, which is stored in plain text and web. after the config file is changed, the website can be automatically updated without Restarting IIS.

In web. config, multiple languages are stored as follows:

<Add key = "string_en _ content management" value = "content"/>

<Add key = "string_en _ System Management" value = "system"/>

<Add key = "string_en _ permission management" value = "Privilege"/>

<Add key = "string_ru _ content management" value = "too many threads have been written into zookeeper"/>

<Add key = "string_ru _ System Management" value = "zookeeper without any prior modification"/>

<Add key = "string_ru _ permission management" value = "zookeeper has been added when zookeeper has been added"/>

It can be seen that it is directly used for the extension of <deleetpipeline>, and there is no new segment, the key is divided into three parts:

U string

U en/ru

U Chinese to be translated

String is used to identifyProgramAnd en indicates the language to be translated. If you need other languages, you only need to specify them here. In the preceding example, two languages are specified: English and Russian, the default Chinese characters are supported in three languages. Finally, you only need to write the translation result in the value attribute.

After this file is configured, it seems that every time you write an Asp.net page, you need to write a lot of programs to translate the characters in it. In fact, this is not the case. I also defined a base class, inherit from page and name it local. In the page_load function of this page, various controls, such as text boxes, labels, buttons, and even image boxes, are automatically searched for on the page, then, based on the current specified language (which can be found in the session or viewed in the post), select the corresponding translation to implement multiple languages.

The image box is also mentioned above. In a multi-language environment, many images are solidified with local text, so images need to be replaced. In the images directory of the website, I added subdirectories based on different languages, such as images/EN images/ru. During translation, the image path will be spliced from images/a.gif to images/en/a.gif. This will automatically replace the image, provided that you have prepared enough images for replacement.

You can use the following function to retrieve all controls on the page:

Foreach (Control in parent. Controls)

{

}

In the database, there is a dedicated table zr_lang to store the languages supported by the current system. On the login interface (including the front-end and back-end), we will judge from this, and select the languages to support in multiple languages.

If the title of the page is written on the Asp.net page, you cannot use the above method for translation. Therefore, I made a special label to store the title content, then reference the title on the page as follows:

<HTML>

In this way, you can translate the correct title.

For a grid table, caption translation is not difficult, but it is difficult to translate the actual content in the grid according to different languages, I used a small trick here to implement it: Add a space before the column in the grid that needs to be translated. In actual work, a column with space is encountered, it indicates that the content needs to be translated, and then it can be translated one by one. Why do we add a space? First, it will not affect the display. In addition, it can also be treated differently without translation, you do not need to use this method to consume too much time. See the following two functions to handle this problem:

Public static void localgrid (Ref system. Web. UI. webcontrols. DataGrid grid)

{

System. Web. UI. Page page = new system. Web. UI. Page ();

If (page. session ["_ language _"]! = NULL)

{

If (page. session ["_ language _"]. tostring () = "tw" | PAGE. session ["_ language _"]. tostring () = "en ")

{

String lang = page. session ["_ language _"]. tostring ();

Foreach (system. Web. UI. webcontrols. datagridcolumn column in grid. columns)

{

String text = column. headertext;

If (system. configuration. configurationsettings. receivettings ["string _" + Lang + "_" + TEXT]! = NULL)

{

TEXT = system. configuration. configurationsettings. receivettings ["string _" + Lang + "_" + TEXT];

}

Else

{

If (lang = "tw ")

{

TEXT = big5.getbig5code (text );

}

}

Column. headertext = text;

}

}

}

}

/// <Summary>

/// Localize the content in a grid

/// </Summary>

/// <Param name = "Grid"> table </param>

Public static void localgridcontent (Ref system. Web. UI. webcontrols. DataGrid grid)

{

System. Web. UI. Page page = new system. Web. UI. Page ();

If (page. session ["_ language _"]! = NULL)

{

Arraylist Cols = new arraylist ();

Int I = 0;

Foreach (system. Web. UI. webcontrols. datagridcolumn column in grid. columns)

{

String text = column. headertext;

If (text. substring (0, 1) = "")

{

Column. headertext = Tran (column. headertext );

Cols. Add (I );

}

I ++;

}

If (cols. Count> 0)

{

Int CNT = grid. Items. count;

For (Int J = 0; j <CNT; j ++)

{

Foreach (Object Col in Cols)

{

Int colnumber = (INT) Col;

String content = grid. items [J]. cells [colnumber]. text;

Content = Tran (content );

Grid. items [J]. cells [colnumber]. Text = content;

}

}

}

}

}

At this point, we have basically discussed some implementation methods for multiple languages. In the next section, we will focus on the control structure of the system.

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.