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

Source: Internet
Author: User

From http://www.cnblogs.com/think8848/archive/2011/03/20/1989376.html

 

I thought I could use the previous multi-language scheme for programming. I didn't expect the first official page to be stuck. Why? Because htmlhelper can only find the currently requested
Which view is used? If layout or partial is used, the menu will be closed. Through some attempts, we found that
The htmlhelper. viewdatacontainer attribute obtains a webviewpage object. The virtualpath object in this object indicates that
The physical file (. cshtml) of the page that is being excute before.CodeAs follows:

Public Static Class Localizationhelper
{
Public Static String Lang (This Htmlhelper,String Key)
{
VaR viewpath = (htmlhelper. viewdatacontainerAs Webviewpage). virtualpath;
VaR viewname = viewpath. substring (viewpath. lastindexof ('/'), Viewpath. Length-viewpath. lastindexof ('/'). Trimstart ('/');
VaR filepath = htmlhelper. viewcontext. httpcontext. server. mappath (viewpath. substring (0, viewpath. lastindexof ('/') + 1) +"App_localresources";
VaR Langs = htmlhelper. viewcontext. httpcontext. Request. userages. Union <String> (New String[] {"" });
 
Ienumerable <dictionaryentry> resxs =Null;
 
Foreach (VAR LangIn Langs)
{
VaR resxkey =
String. Isnullorwhitespace (Lang )?String. Format (@ "{0} \ {1}. resx", Filepath, viewname ):String. Format (@ "{0} \ {1}. {2}. resx", Filepath, viewname, Lang );
 
Resxs = getresx (resxkey );
 
If (Resxs! =Null){Break;}
}
 
Return (String) Resxs. firstordefault <dictionaryentry> (x => X. Key. tostring () = key). value;
}
 
Private Static Ienumerable <dictionaryentry> getresx (String Resxkey)
{
Objectcache cache = memorycache. default;
 
Ienumerable <dictionaryentry> resxs =Null;
 
If (Cache. Contains (resxkey ))
{
Resxs = cache. getcacheitem (resxkey). ValueAs Ienumerable <dictionaryentry>;
}
Else
{
If (File. exists (resxkey ))
{
Resxs =New Resxresourcereader (resxkey). Cast <dictionaryentry> ();
Cache. Add (resxkey, resxs,New Cacheitempolicy () {priority = cacheitempriority. notremovable });
}
}
 
Return Resxs;
}
}
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.