ASP. net mvc 3 razor multi-language reference solution

Source: Internet
Author: User

From http://www.cnblogs.com/think8848/archive/2011/03/15/1984793.html

 

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. This parameter takes a long time to think about failure. The key is that the above sentenceCode
Always throw an exception:

Resourceexpressionfields fields = (resourceexpressionfields) builder. parseexpression (expression,Typeof(String), Context );

Expression is always incorrect. I guess it is the reason why the binding expression in webformviewengine cannot be used in razor, But I steal
It simulates a webform binding expression and still cannot pass. In addition, the official website does not provide a complete example, and I do not want to study the time relationship. If someone tries it on razor
The verification is successful. Leave a message.

The first one, I personally feel like this is the same except for the others. Which is the same? We must use rexgen to compile the. resx file to make it
Is the. Resources file, and then use it. In the past, this step was not taken when using webpage, and it was not convenient to use multiple steps. Therefore, you can open msdn to see it.
Now it is a type: resxresourcereader. It is a good thing from the name of this type, but this type cannot be found during code writing? Take a closer look, the original Class
Type is actually defined in the system. Windows. Forms namespace, in the WebProgramSystem. Windows. Forms. dll references
It is strange, but think about it as a standard class library of. net. The addition should not have a big impact, and I still don't know whether it can be used in addition.

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. ViewAs 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 LangIn 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 Result ="";
 
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 still many improvements, such as the lack of cultureinfo In the method, because our company's common solution is language.
With the browser's language settings, in order to test the simplicity, I just determined the language to Display Based on the userages in the request. In addition, each time a new
Whether or not reader has a great impact on the performance has not been tested, but the most basic functions are available. Using this solution, you can use resource files as conveniently 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

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. This parameter takes a long time to think about failure. The key is that there is a code
Always throw an exception:

Resourceexpressionfields fields = (resourceexpressionfields) builder. parseexpression (expression,Typeof(String), Context );

Expression is always incorrect. I guess it is the reason why the binding expression in webformviewengine cannot be used in razor, But I steal
It simulates a webform binding expression and still cannot pass. In addition, the official website does not provide a complete example, and I do not want to study the time relationship. If someone tries it on razor
The verification is successful. Leave a message.

The first one, I personally feel like this is the same except for the others. Which is the same? We must use rexgen to compile the. resx file to make it
Is the. Resources file, and then use it. In the past, this step was not taken when using webpage, and it was not convenient to use multiple steps. Therefore, you can open msdn to see it.
Now it is a type: resxresourcereader. It is a good thing from the name of this type, but this type cannot be found during code writing? Take a closer look, the original Class
Type is defined in the namespace of system. Windows. Forms. dll. Add system. Windows. Forms. DLL to the web program.
It is strange, but think about it as a standard class library of. net. The addition should not have a big impact, and I still don't know whether it can be used in addition.

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. ViewAs 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 LangIn 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 Result ="";
 
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 still many improvements, such as the lack of cultureinfo In the method, because our company's common solution is language.
With the browser's language settings, in order to test the simplicity, I just determined the language to Display Based on the userages in the request. In addition, each time a new
Whether or not reader has a great impact on the performance has not been tested, but the most basic functions are available. Using this solution, you can use resource files as conveniently 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.