开发者

How to localize View with a localizationService in MVC3

开发者 https://www.devze.com 2023-03-31 08:03 出处:网络
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 an

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 开发者_Python百科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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号