ASP. net mvc 3 razor multi-language reference solution Supplement 2

Source: Internet
Author: User

From http://www.cnblogs.com/think8848/archive/2011/03/19/1988690.html

In the last two articles (1, 2), I mentioned how to applyProgramThe resource file usage problem in the project has not been tested in the case of calling other classlibrary Chinese source files. This morning, we also conducted a test in accordance with the previous ASP. NET project practices.

Use resource files:

Public Class Class1
{
Public String Gettt ()
{
Return Resources. Test. tt;
}
}

It is expected that, as the language options in the browser change, the system will automatically switch, and the results will not be unexpected, but only the resource item values with no culture information will be displayed.

(Important updates in March 22: added to the system. Web configuration node of Web. config<Globalization culture = "Auto" uiculture = "Auto"/>You don't need to use the following methods !!!)

By checking the designer. CS file of the resource file, it is found that there is an internal attribute definition:

[Global: system. componentmodel. editorbrowsableattribute (Global: system. componentmodel. editorbrowsablestate. Advanced)]
Internal Static Global: system. Globalization. cultureinfo culture {
Get {
Return Resourceculture;
}
Set {
Resourceculture = value;
}
}

So I guess that if this attribute value can be set, I must have ResourceManager automatically select the corresponding resource file. But where can it be added? I found that there is such an event in httpapplication:

Public Event Eventhandler prerequesthandlerexecute;

Therefore, the event processor of this event is added to global, and the following is added to its method:Code:

Protected Void Application_prerequesthandlerexecute (Object Sender, eventargs E)
{
Thread. currentthread. currentculture = cultureinfo. getcultureinfo (request. userages [0]);
Thread. currentthread. currentuiculture = thread. currentthread. currentculture;
}

F5 runs, which achieves the expected results: with the browser language option switching, the language on the page can also be automatically switched.

English:

Chinese:

From http://www.cnblogs.com/think8848/archive/2011/03/19/1988690.html

In the last two articles (1, 2), I mentioned how to use resource files in application projects. I have not tested how to call other Chinese source files in classlibrary, this morning, according to ASP. net project practices for a test

Use resource files:

Public Class Class1
{
Public String Gettt ()
{
Return Resources. Test. tt;
}
}

It is expected that, as the language options in the browser change, the system will automatically switch, and the results will not be unexpected, but only the resource item values with no culture information will be displayed.

(Important updates in March 22: added to the system. Web configuration node of Web. config<Globalization culture = "Auto" uiculture = "Auto"/>You don't need to use the following methods !!!)

By checking the designer. CS file of the resource file, it is found that there is an internal attribute definition:

[Global: system. componentmodel. editorbrowsableattribute (Global: system. componentmodel. editorbrowsablestate. Advanced)]
Internal Static Global: system. Globalization. cultureinfo culture {
Get {
Return Resourceculture;
}
Set {
Resourceculture = value;
}
}

So I guess that if this attribute value can be set, I must have ResourceManager automatically select the corresponding resource file. But where can it be added? I found that there is such an event in httpapplication:

Public Event Eventhandler prerequesthandlerexecute;

Therefore, the event processor of this event is added to global and the following code is added to its method:

Protected Void Application_prerequesthandlerexecute (Object Sender, eventargs E)
{
Thread. currentthread. currentculture = cultureinfo. getcultureinfo (request. userages [0]);
Thread. currentthread. currentuiculture = thread. currentthread. currentculture;
}

F5 runs, which achieves the expected results: with the browser language option switching, the language on the page can also be automatically switched.

English:

Chinese:

Related Article

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.