[Original] ASP. net mvc 3 razor multi-language reference solution

Source: Internet
Author: User

Sorting in April March 22: For practical examples, see:Here

Multi-language support is basically a feature that must be supported by mature products. However, when I want to use this feature, I find that I cannot find a solution (surprised, is it really not provided ?), So I searched the internet for a long time and found two preferred solutions:

1: ASP. net mvc multi-language Solution

2: ASP. net mvc-localization helpers

Both solutions are for webformviewengine. For me who want to use razor, modification is inevitable.

Let's talk about the second method. The core of this method seems to be the httpcontext. getlocalresourceobject () method. However, the problem is that when using the getlocalresourceobject () method, you need to provide the virtualpath parameter, which takes a long time to think about failure. The key is that the above sentenceCodeAlways throw an exception:

 
Resourceexpressionfields fields = (resourceexpressionfields) builder. parseexpression (expression, typeof (string), context );

Expression is always incorrect. I guess it should be because the binding expression in webformviewengine cannot be used in razor, But I secretly simulate a webform binding expression, still cannot be passed. In addition, the official website does not provide a complete example, and I do not want to study the time relationship. If someone has successfully tried it on razor, leave a message.

Again, I personally feel like this is the same except for the others. Which is the same? rexgen must be used for compilation. resx file to make it. resources file, and then use it. In the past, webpage was not used in this step. It was not convenient to use multiple steps. Therefore, you can open msdn to check whether a type is resxresourcereader, the type name is a good thing, but the type cannot be found during code writing? Take a closer look, the original type is actually defined in the system. Windows. Forms namespace, in the WebProgramAdding System. Windows. Forms. DLL to the reference is always strange, but think about it as a standard class library of. net. It should not have a big impact in addition, and I still don't know whether it can be used.

Therefore, a helper class is made based on the first solution:

Public static class localizationhelpers {public static string Lang (this htmlhelper, string key) {var viewpath = (htmlhelper. viewcontext. view as buildmanagercompiledview ). viewpath; 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; string resxpath = string. format (@ "{0} \ {1 }. resx ", filepath, viewname); foreach (VAR Lang in Langs) {If (file. exists (string. format (@ "{0} \ {1 }. {2 }. resx ", filepath, viewname, Lang) {resxpath = string. format (@ "{0} \ {1 }. {2 }. resx ", filepath, viewname, Lang); break ;}} string R Esult = ""; resxresourcereader reader = new resxresourcereader (resxpath); var entry = reader. cast <dictionaryentry> (). firstordefault <dictionaryentry> (x => X. key. tostring () = Key); If (entry. value! = NULL) {result = (string) entry. value;} return result ;}}

This solution can only be regarded as a reference prototype, and there are many improvements, such as the absence of cultureinfo in the method, because our company's general solution is that the language is set with the browser's language, to test the simplicity, I just determined the language to display according to the userages in the request. In addition, whether a new reader has a great impact on the performance is not tested every time. However, the most basic function is available. Use this solution, you can use resource files as easily as in webpage.

By the way, I almost forgot the important content. How can I use this method:

@ Html. Lang ("test ")

"Test" is the resource key.

The localizationhelpers type namespace must be system. Web. MVC.

If you have a better solution, please let me know. Thank you.

Modify: in the sample source code, the localizatioinhelpers line 16th is incorrect,

VaR filepath = htmlhelper. viewcontext. httpcontext. server. mappath (viewpath. substring (0, viewpath. Length-viewpath. lastindexof ('/') + "app_localresources ";

The modification is as follows:

VaR filepath = htmlhelper. viewcontext. httpcontext. server. mappath (viewpath. substring (0, viewpath. lastindexof ('/') + 1) +"App_localresources";

Sample Code

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.