Use of resource files under ASP. NET

Source: Internet
Author: User
Resource files are usually used in winform. In fact, ASP. NET can also be used to achieve multi-language conversion.
My friend asked me to help him make a small station, which is a 2-minute language version. I think that since it is a small station, I have to add some materials to it. Just learned
Click the resource file, and use it. The sample Website is http://www.donglongcf.cn as follows.
The following describes how to build a multilingual website step by step.
Step 1:
Create two resource files, named resource. resx and resource_en.resx in English.
In the aspx file on the interface, directly
1 <% = resources. Resource. string1 %>

You can bind 1 <asp: Label id = "lbl_global" runat = "server" text = "<% # resources. Resource. button1 %>"> </ASP: Label>

You can also 1 <asp: textbox id = "textbox1" runat = "server" text = "<% $ resources: resource, string1 %>"> </ASP: textbox>

You can read the value of the corresponding resource file.
However, we usually do not use resource files like this. Different languages are displayed only when the language is switched. For example
Click Chinese to display the Chinese Language Pack. The English Language Pack is displayed. So we need to set the current language environment
Step 2:
During page initialization, set the current language environment. The Chinese value is ZH-CN, and the English value is en-us. 1 thread. currentthread. currentuiculture = cultureinfo. createspecificculture ("ZH-CN ");

However, we are still not satisfied. We do not need to add such a sentence in page_load of each page, which is too cumbersome.
Step 3: override the initializeculture method of the page.
Create an initialculture
1 protected override void initializeculture ()
2 {
3 // the current language variable can be stored in the session or cookie.
4 If (session ["Lang"]! = NULL)
5 {
6 thread. currentthread. currentuiculture = cultureinfo. createspecificculture (session ["Lang"]. tostring ());
7 thread. currentthread. currentculture = cultureinfo. createspecificculture (session ["Lang"]. tostring ());
8 // base. initializeculture ();
9}
10 else
11 {
12 thread. currentthread. currentuiculture = cultureinfo. createspecificculture ("ZH-CN ");
13 thread. currentthread. currentculture = cultureinfo. createspecificculture ("ZH-CN ");
14}
15 base. initializeculture ();
16}

Then each page is re-inherited: 1 public partial class _ default: initialculture

If you are a lazy programmer, adding such a sentence to each page will certainly not satisfy you. At this time, we sniffed bad smell.
Step 4,
Create httpmoudle
Global Control through httpmodle 1 // application Initialization
2 Public void Init (httpapplication APP)
3 {
4 app. acquirerequeststate + = new eventhandler (context_acquirerequeststate );
5 // here, the delegate operation in acquirerequeststate is because I put the global language variable in the session
6 // The session is generated only in the acquirerequeststate.
7}
8
9 void context_acquirerequeststate (Object sender, eventargs E)
10 {
11 httpapplication APP = sender as httpapplication;
12 langguage (APP );
13}
14
15 private void langguage (httpapplication APP)
16 {
17
18 if (App. Context. session ["Lang"] = NULL)
19 app. Context. session ["Lang"] = "ZH-CN ";
20 thread. currentthread. currentuiculture = cultureinfo. createspecificculture (App. Context. session ["Lang"]. tostring ());
21 thread. currentthread. currentculture = cultureinfo. createspecificculture (App. Context. session ["Lang"]. tostring ());
22}
23 # endregion

In this way, our website will initially complete the use of resource files.
Source code http: // www/kobewang.cn

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.