简体   繁体   English

ASP.Net MVC控制器中的全球化

[英]Globalization in ASP.Net MVC Controllers

I have a feeling this is something very obvious that I'm missing, but here goes. 我感觉这很明显,我很想念,但是可以。

I'm trying to set up multiple languages in an ASP.Net MVC project (C#, MVC2, .Net4.0). 我正在尝试在ASP.Net MVC项目(C#,MVC2,.Net4.0)中设置多种语言。 However, I have some cases where I want to have phrases translated within the Controller (error/success messages, mostly). 但是,在某些情况下,我想在Controller内翻译短语(主要是错误/成功消息)。 However, it seems every method I try is either protected or internal . 但是,似乎我尝试的每种方法都是protectedinternal How do I access the resx files from within my Controller? 如何从Controller内访问resx文件?

This is a presentation specific concern, I think the best approach is to handle this at a View level. 这是一个特定于演示的问题,我认为最好的方法是在View级别进行处理。 You will have less problems if you access resources from the View. 如果从视图访问资源,则问题将更少。 Also note that accessing resources from the controllers make them less testable, because when accessing a resource (by default) the resource manager looks for a specific file which is not found when you´re testing. 还要注意,从控制器访问资源会降低它们的可测试性,因为在访问资源时(默认情况下),资源管理器会查找测试时找不到的特定文件。

Make sure the modifier on the resx class that's created for you is Public . 确保为您创建的resx类上的修饰符为Public When you're editing the resx file, you'll see a drop down with the modifier towards the top right (close to the vertical middle of your screen). 在编辑resx文件时,您会看到一个下拉列表,其修饰符朝右上方(靠近屏幕的垂直中间)。

However, consider the fact that you could have your ViewModel take care of localizing validation messages by decorating the different properties with DataAnnotations and resource files. 但是,请考虑以下事实:您可以通过用DataAnnotations和资源文件装饰不同的属性,使ViewModel负责对验证消息进行本地化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM