Bilingual website resource files

Source: Internet
Author: User

Link: http://songwenjie12.blog.163.com/blog/static/131224420200910685249410/

 

Abstract: With the development of computer networks, some commercial organizations have begun to enter the international market. Therefore, the websites of these commercial organizations have begun to target users all over the world and require the website to be available in multiple languages for localization. In. under the net1.1 framework, if you want to implement localization, you may need to use some plug-ins, or even write a series of code. under the net2.0 framework, you will find everything is so simple ~~!

Main content:

1. Simple Example

2. Further understanding of Localization

3. language conversion

4. Solution

1. Simple Example

The following is a simple example to illustrate how to use localization to implement localization. First, we open Visual Studio 2005 and create a project named localization (the name is irrelevant, as you like ~~), As follows:

Then we put some controls on the default. aspx page. For example, we put a button on the page, a label, and a text box. After adding the buttons, the page is like the following:

The next step is to generate local resources. Click "Tools> generate local resources (r )",

Therefore, a folder app_localresources is added to our project, which contains the default. aspx. resx file, which is used to edit and save some text resources we want to display,

For example, enter the following information for the control we added:

After completing the above steps, we can see the effect.

In the above steps, we did not manually write any code, but the binding process was automatically implemented by. net2.0. The difference between pages and the past is that the HTML code has changed:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. CS "inherits =" _ default "Culture =" Auto "meta: resourcekey =" pageresource1 "uiculture =" Auto "%>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Title> No title page </title>

</Head>

<Body>

<Form ID = "form1" runat = "server">

<Div>

<Asp: button id = "button1" runat = "server" meta: resourcekey = "button1resource1" text = "button"/>

<Asp: textbox id = "textbox1" runat = "server" meta: resourcekey = "textbox1resource1"> </ASP: textbox>

<Asp: Label id = "label1" runat = "server" meta: resourcekey = "label1resource1" text = "label"> </ASP: Label> </div>

</Form>

</Body>

</Html>

In addition to adding attributes such as culture = "Auto" meta: resourcekey = "pageresource1" uiculture = "Auto" to the page, the controls we added also include meta: resourcekey, and. net2.0 is based on these attributes for language binding.

In the preceding simple project, we only provide one language and have not met the localization requirements. Therefore, we manually add a resource file default to the app_localresources directory. aspx. the en-us.resx file (where the English [us] language code is "En-us"), we add some key/value pairs to it, as shown below:

Then we will modify the Internet option language of IE, add "English (US) [en-US]", and move it to the top:

Run our project again and you will find that simple Localization has been implemented,

Now, through a simple example, we have a preliminary understanding of how. net2.0 uses localization to implement localization. However, if we want to build an excellent international website or software, we need to further understand localization.

Ii. Further understanding of Localization

Here, you will find that each page corresponds to some of its own resource files. If we want to use the same resource file for multiple or all pages, what should we do? You can add a folder named app_globalresources to the project, which is the default directory for storing global resources. For example, we add two files in the Global Directory: Global. resx and Global. en-us.resx,

Then, we enter the corresponding key/value values in them. There are two ways to bind our global resources to the page:

One is to bind it to the HTML code. The expression is <% $ resources: [applicationkey], resourcekey, [Default] %>. For example, in default. put one more button in aspx, And the HTML code is:

<Asp: button id = "button2" runat = "server" text = "<% $ resources: Global, string1 %>

As a result, we can see that the resource has been bound to the control.

Another method is to bind the program code, because. the net2.0 framework will compile the global resource file into a class, that is, the global. the resx file will be compiled into a class global, and all keys in the resource file will be attributes of the class for access in the program. For example, we can write in a program as follows:

This. button2.text = resources. Global. string1;

You can download the attachment later and try again to see if the effect is the same ~~~~

In addition to strings, resource files can also store images, audios, and files,

For example, if we add some image resources to the resource file, you can set the image to be linked during compilation or embedded into the resource file,

In the program code, what we get is system. drawing. for bitmap type variables, as for adding files of other types, we will not talk about them here.

Iii. Language settings

Do you think it will be difficult to select a language every time you go to the Internet option? How do you set a language in your project? Very simple. There are usually three ways. First, we need to talk about two attributes: one is culture, which determines how data types are organized, such as numbers and dates, and the other is uiculture, which determines which localized resource to use, that is, the language used. For details, see msdn ~~~

1. Set on the specific page

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. CS "inherits =" _ default "Culture =" Auto "meta: resourcekey =" pageresource1 "uiculture =" Auto "%>

As shown above, because the uiculture and culture attributes are set to auto ,. net2.0 determines which resource files to load based on your Internet language options. Of course, you can also set both uiculture and culture to "En-us ", the page is loaded with an English resource file. However, it is very troublesome to write this way. Every page must be written, so we can set the language we want in Web. config.

2. Set in Web. config

<Configuration>

<System. Web>

<Globalization culture = "En-us" uiculture = "En-us"/>

</System. Web>

</Configuration>

With this setting, all the pages in the project will automatically load the English resource file, but note that if the uiculture and culture attributes are set on the page, it will overwrite the web. config. For example, if the two attributes of a page are set to "ZH-CN", the page displays Chinese rather than English. But I still think it is not good, because the language files to be loaded on the page are all written to death, not flexible.

3. Set in the code

Using system. Globalization;

Using system. Threading;

// Set the culture to the browser's Accept Language

Thread. currentthread. currentculture =

Cultureinfo. createspecificculture (request. userages [0]);

// Set the user interface culture to the browser's Accept Language

Thread. currentthread. currentuiculture =

New cultureinfo (request. userages [0]);

Set in the program code and reload the initializeculture event on the page. Note that the language settings in the code will overwrite the HTML attribute of the page or web. config settings. The preceding request. userages [0] is the first language code to obtain the Internet language options.

Iv. Solutions

After the preliminary and further understanding above, I believe you have understood the principle and implementation method of localization. However, we really need to implement multiple languages and localization in our system, how should we implement the architecture?

To reduce repeated code, we usually create a base page pagebase class, so that all pages can inherit the base page and override the initializeculture event on the base page, the user-selected language can be stored in session, Cookie, querystring or. pro provided by net2.0File. The following uses session as an example to build a base page. For simplicity, I just use a button to select a language. In actual implementation, a drop-down box may be used:

Home page:

Using system;

Using system. DaTa;

Using system. configuration;

Using system. Web;

Using system. Web. Security;

Using system. Web. UI;

Using system. Web. UI. webcontrols;

Using system. Web. UI. webcontrols. webparts;

Using system. Web. UI. htmlcontrols;

Using localization;

/** // <Summary>

/// Localization

/// Kenth

/// 2006.07.16

/// </Summary>

Public partial class _ default: mypagebase

{

Protected void page_load (Object sender, eventargs E)

{

This. button2.text = resources. Global. string1;

}

Protected void button2_click (Object sender, eventargs E)

{

If (session ["preferredculture"]. tostring (). toupper () = "En-us ")

Session ["preferredculture"] = "ZH-CN ";

Else if (session ["preferredculture"]. tostring (). toupper () = "ZH-CN ")

Session ["preferredculture"] = "En-us ";

// Redirect page

Response. Redirect (request. url. pathandquery );

}

}

Base page mypagebase:

Using system;

Using system. DaTa;

Using system. configuration;

Using system. Web;

Using system. Web. Security;

Using system. Web. UI;

Using system. Web. UI. webcontrols;

Using system. Web. UI. webcontrols. webparts;

Using system. Web. UI. htmlcontrols;

Using system. Threading;

Using system. Globalization;

Namespace Localization

{

/** // <Summary>

/// Implement localization of the mypagebase base class

/// Kenth

/// 2006.07.16

/// </Summary>

Public class mypagebase: system. Web. UI. Page

{

Protected override void initializeculture ()

{

// Use session to store Language Information

If (session ["preferredculture"] = NULL)

Session ["preferredculture"] = request. userages [0];

String userculture = session ["preferredculture"]. tostring ();

If (userculture! = "")

{

// Rebind the language code based on the session Value

Thread. currentthread. currentuiculture = new cultureinfo (userculture );

Thread. currentthread. currentculture = cultureinfo. createspecificculture (userculture );

}

}

}

}

 

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.