ASP.NET MVC Database Localization

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!

+4
source share
2 answers

You can use my Griffin.MvcContrib project. It contains a ready-made MS SqlServer implementation for storing localization in a database.

Introduction: http://www.codeproject.com/Articles/352583/Localization-in-ASP-NET-MVC-with-Griffin-MvcContri

Administration

There is also an administration area that you can use to manage localizations:

enter image description here

Configure SQL Server

https://github.com/jgauffin/griffin.mvccontrib/wiki/SqlServer

Source

The project is available on github: https://github.com/jgauffin/griffin.mvccontrib

0
source

Here is a very good one: http://west-wind.com/westwind.globalization/

He offers:

  • DB Storage
  • Import / Export Resx
  • Strong Type Generation

it is quickly added to your projects through Nuget, and you have the full source code. Awesome stuff

0
source

Source: https://habr.com/ru/post/1445244/


All Articles