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) |
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>; |
If (File. exists (resxkey )) |
Resxs =New Resxresourcereader (resxkey). Cast <dictionaryentry> (); |
Cache. Add (resxkey, resxs,New Cacheitempolicy () {priority = cacheitempriority. notremovable }); |