如何在 MVC3 中使用本地化服务本地化视图
我正在开发一个应用程序,该应用程序使用 ASP.NET MVC3 作为前端,通过接口与服务层进行通信。所有接口/服务都通过 ninject 和构造函数注入注入到控制器中。
我想通过 ninject 提供的位置服务来集中所有本地化。
在控制器方面很简单,我在控制器基类中添加了一个 Localize 函数,并将 ILocalizeService 添加到构造函数中,瞧,完成了。任何时候我需要本地化字符串,我都可以调用 localize 函数,该函数又调用该服务。
现在我正在尝试找出一种在视图端执行此操作的干净方法。我有一个基本 View 类,它公开了一个类似于控制器中的 Localize 函数,因此我可以在视图上传递任何静态文本,但我无法找到一种干净的方法来获取本地化服务的实例。我读过很多帖子,说 DI 到视图中是不好的做法,但在这种情况下,我认为基本视图是集中视图中文本本地化的最干净的地方(当然,我愿意接受任何其他选项)不需要我传递我的内核)
有人可以向我指出的任何示例或关于如何干净地完成此操作的建议。
I am developing an application which is using ASP.NET MVC3 for the front end which talks to service layer via interfaces. All of the interfaces/services are injected into the controllers via ninject and constructor injection.
I would like to funnel all localization through a location service which is provided by ninject.
Easy enough on the controller side, I have added a Localize function in the controller base class and added the ILocalizeService to the constructor, and voila, done. Anytime I need to localize a string, I can just call the localize function which in turns calls the service.
Now I am trying to figure out a clean way to do this on the View side. I have a base View class which exposes a Localize function like in the controller so I can pass any static text on the view, but I can't figure out a clean way to get an instance of my localization service. I have read numerous posts saying that DI into a view is bad practice, but in this case I think the base view is the cleanest place to centralize the localization of text in the view (of course, I would be open to any other option which doesn't require me passing my kernel around)
Any examples someone could point me to or advice on how to do this cleanly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ninject 允许将属性注入到视图中。但这是不好的做法。您应该在视图中尽可能少地执行操作。
我认为主要问题在于您使用的是自定义本地化服务,而不是采用每种语言使用资源文件或自定义视图的标准方法之一。请参阅 Scott Hanselman 撰写的这篇优秀博客文章
或者是否有任何特殊原因需要采用自己的方式处理本地化?
Ninject allows property injection into views. This is bad practice though. You should do as little as possible in the view.
I think the main problem lies in the fact that you are using a custom localization service instead of going one of the standard ways to use resource files or custom views per language. See this excellent blog post by Scott Hanselman
Or is there any special reason for an own way to deal with localization?