I am developing a multilingual enterprise website and I would like to keep the localization in the database.
I read the following article, which is very good, but I personally think that this is an overhead, and I can achieve the same easily:
Extending the ASP.NET 2.0 Resource Provider Model
I have already established some basics, but I'm not sure if my approach is ok. I basically created a service that registers using DI.
public interface ILocalizedStringProvider { string GetLocalizedString(string key); string GetLocalizedString(string key, string deafultValue); }
I also created an html helper like this
public static MvcHtmlString LocalizedString(this HtmlHelper helper, string key, string defaultValue) { if (string.IsNullOrEmpty(defaultValue)) return new MvcHtmlString(""); if (string.IsNullOrEmpty(key)) return new MvcHtmlString(defaultValue); ILocalizedStringProvider localizedStringProvider = DependencyResolver.Current.GetService<ILocalizedStringProvider>(); if (localizedStringProvider == null) { return MvcHtmlString.Create(defaultValue); } string val = localizedStringProvider.GetLocalizedString(key, defaultValue); if (string.IsNullOrEmpty(val)) { return MvcHtmlString.Create(defaultValue); } return MvcHtmlString.Create(val); }
Then the helper is just called from the view.
First I want to know if this approach is good, and if it is not an anti-pattern.
Secondly, my problem is this line:
ILocalizedStringProvider localizedStringProvider = DependencyResolver.Current.GetService<ILocalizedStringProvider>();
Perhaps it is better to enable the ILocalizedStringProvider service in the controller with the constructor injector and allow the controller to populate the ViewBag with localization?
Thanks!